Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hii..


I want to know how to calculate gradient of an image in c# using any filter. Please help me.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Aug-13 3:08am    
What do you mean "using any filter"? Some available filter? For what system? What should it do exactly?
—SA

This can be done with OpenCV using the Sobel function.

http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html#sobel-derivatives[^]

OpenCV has a number C# wrappers available for example:

http://www.emgu.com/wiki/index.php/Main_Page[^]

I take SA's point and add this:

http://en.wikipedia.org/wiki/Image_gradient[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Aug-13 3:31am    
It looks like good stuff and probably useful for OP, my respect for bringing it here, but, to be fair, that does not answer to OP's question, because it should be pointed out that the notion of "gradient of an image" does not make sense. I explain it in my answer.

Sobel operator is relevant to the topic, OP could see this, to understand it: http://en.wikipedia.org/wiki/Sobel_operator.

So, I don't know... I voted 4 this time...

...and I added Solution 3 to add my 5 cents to it. OP needs a .NET solution.

—SA
[no name] 17-Aug-13 3:44am    
I take your point and have added a further link.
Please see my comment to the question.

Here is the problem: even though you can create an image with any predefined gradient, easily, you cannot generally ask a question "what is a gradient of a given image?". It takes some understanding, what a gradient is. A gradient cannot be a property of an image. It is a property of some class of functions (differentiable at least in some points) at some given point of the function domain. In case of images, this is a property of one given location. Moreover, such property may or may not exist. First of all, the bitmap images are discrete, they can be considered only as approximation of some smooth functions, and only in the vicinities of some points of the image, not at any arbitrary location.

To have such approximate gradient defined, you need some smooth function of color change is some reasonably big vicinity of some point (say, not one pixel). In this vicinity, you can build some smooth function by interpolation of pixels colors. This function can have some partial derivatives of the colors on two directions. You should find out the direction where the derivative is maximum. The obtained vector will be the gradient in the point. Naturally, in some other point the gradient will be different. And in the points of sharp contrast, the notion of gradient should be considered as not making real sense.

This is pretty well explained here, and for the images, too: http://en.wikipedia.org/wiki/Gradient[^].

From the equations shown, it's not too difficult to develop the algorithm for calculation of the gradient in a given point. It's basically reduced to the simple task of numeric computation of partial derivatives by the discrete points in some point's vicinity.

Pay attention for the very first and especially the third picture in this article. They illustrate the cases when the gradient for the image cannot be defined, as it is different for every point of the image. The case when you could talk about "gradient of the image" is only the second picture, which happens to have uniform gradient оver the whole image.

—SA
 
Share this answer
 
v3
My addition to the Solution 1:

In my comment to it, I explain why this is not the solution, but this is rather your fault than the fault of pwasser.

To understand the Sobel operator, please see: http://en.wikipedia.org/wiki/Sobel_operator[^].

Refer to my explanations of the problem in Solution 2.

Practically, the issue is how to use the OpenCV with .NET. One apparent solution is: use the .NET wrapper called Emgu CV:
http://www.emgu.com/wiki/index.php/Main_Page[^].

—SA
 
Share this answer
 
Like SA nicely explained, you cannot say "gradient of an image" unless you specify other parameters.
For example, you might want to calculate the gradient along a certain path or straight line. Working with such lines is possible if you get all the pixel data along that line then analyze the data. One way to do that using Lead Tools image processing SDK is the Leadtools.ImageProcessing.Color.LineProfileCommand class.
Once you have the data, use differentiation along the line's direction (either direction) and the derivative value will give you a good measure of the gradient change.
Of course if you take a line that's perpendicular to the direction of the gradient, you will get zero derivative which means no gradient along that line. This shows the importance of choosing the direction.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900