Gradient direction calculation

I am working on my task in the field of computer vision. One of the subtasks is to calculate the direction of the gradient based on the brightness of the image. I made the matrix bright [width] [height] containing the brightness values ​​for each pixel in 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 work with a simple derivative, without using the Sobel operator, because for my needs a simple derivative is enough.

The question is, what am I doing this gradient calculation correctly and what exactly should I do with border pixels (right now the function returns the value of the pixel itself, im not sure if this is accurate)? And by the way, is there any utility for calculating image gradients? I want to be sure that my program works well.

+3
source share
4 answers

Your calculations are correct. This is a simple gradient method that you use, but if it is good for your use, there is nothing wrong with that.

- , , . - .

, . , , :

1 2. 0 ( ), a- (ba).

:

  pixel1: gradient = 100
  pixel2: gradient = 80

  extrapolate using a-(b-a): 

  pixel0: gradient = 100 - (80-100)) = 120
+1

, , , , , , . .

- , , . B-. 2D- 2D B-Spline, . . B- , . . , ( ) . , (. ).

B- , ,

1/6 4/6 1/6

:

1/2 0 -1/2

:

1 -2 1

B- . 2D . x y. :

"B-Spline reconstruction" (divisor=36)
   1  4  1
   4 16  4
   1  4  1

"B-Spline differentiator in X" (divisor=12)
   1  0 -1
   4  0 -4
   1  0 -1

"B-Spline, 2nd derivative in X, 1st derivative in Y" (divisor=2)
   1 -2  1
   0  0  0
  -1  2 -1

, , , . , , . , , .

() , , .

, - , . . *.

+1

, . , , , , .

, , . , , .

​​Sobel? ?

: x-. grad_x (0, 0) .

0

:

OpenCV ([1 0 -1]).

:

Depends on the application. Here are some good ways to post your image .

0
source

Source: https://habr.com/ru/post/1771059/


All Articles