I'm working on my task in computer vision course. One of sub-tasks is gradient direction computation based on image brightness. I've made a matrix bright[width][height] containing brightness values for every pixel of the image. And i have two such functions:
double Image::grad_x(int x,int y){
if(x==width-1 || x==0) return bright[x][y];
return bright[x+1][y]-bright[x-1][y];
}
double Image::grad_y(int x,int y){
if(y==height-1 || y==0) return bright[x][y];
return bright[x][y+1]-bright[x][y-1];
}
EDIT: border check fixed
I'm working with simple derivative, without using Sobel operator 'cause simple derivative is sufficient for my needs.
The question is, am i doing this gradient computation right and what exactly do i have to do with border pixels(right now function returns value of the pixel itself, im not sure it's accurate)? And, by the way, is there any utility for computation of gradients of the image? I want to be sure my program is performing well.
Your computation is correct. It is a simple gradient method you're using, but if that's fine for your use there is nothing wrong with that.
The corner cases are a problem because you don't have enough data to calculate a gradient in the same way as the other pixels. One way to deal with them is to simply not calculate the corner cases and live with a slightly smaller image.
If this is not an option you can also extrapolate the missing data. If you assume that the gradient changes smoothly it works like this:
In your x-gradient calculations you may have calculated the derivate A for pixel 1 and B for pixel 2. If you want to extrapolate a value for pixel 0 (the corner case) the value a-(b-a) could be used.
A numerical example:
pixel1: gradient = 100
pixel2: gradient = 80
extrapolate using a-(b-a):
pixel0: gradient = 100 - (80-100)) = 120
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With