I understand the process of using a Sobel kernel for edge detection in greyscale images. The input is a greyscale image, and the output is a greyscale image. I'm having trouble, however, figuring out how to apply the Sobel kernel to a color image.

Do I find the color gradient for each color channel (R, G, and B) then average each value to get the final color gradient value? Or do I average the color channels then find the color gradient of that image?

My goal is to render a raytraced image, find the edges, then re-render only the high-contrast regions with anti-aliasing and leave the low-contrast regions alone.

  • $\begingroup$ I would guess that you treat each color plane as it's own black-and-white image. $\endgroup$
    – Scott
    Commented Apr 6, 2014 at 4:11
  • $\begingroup$ This strikes me as a very odd way to determine high contrast regions. As you perform your ray trace, won't you be able to identify directly from your input data structures the regions of rapid change, and use that? $\endgroup$
    – John
    Commented Jun 7, 2017 at 21:23

2 Answers 2


Finding edges in a color image can be done by decomposing the image into its channels, finding the gradients separately and fusing them somehow. However, such approach doesn't incorporate the color components in a joint model. Luckily, there is a better way to do this, which is the structure tensor representation.

The color structure tensor describes the bi-dimensional first order differential structure at a certain point in the image. It is specified by:

$ S= \left[ {\begin{array}{cc} R_x^2+G_x^2+B_x^2 & R_xR_y+G_xG_y+B_xB_y \\ R_xR_y+G_xG_y+B_xB_y & R_y^2+G_y^2+B_y^2 \\ \end{array} } \right] $

where subscripts denote the partial derivatives. This is a more precise description of the local gradients. Eigen-decomposition is then applied to the structure tensor matrix $S$ to form the eigenvalues and eigenvectors. The larger eigenvalue shows the strength of the local image edges (gradient magnitude) and the corresponding eigenvector points across the edge (in gradient direction). In other words, eigenvalues encode the gradient magnitude while the eigenvectors contain the gradient orientation information. Note that the eigenvectors and eigenvalues can be computed analytically for a structure tensor.

There are many other usecases of the structure tensor, so it is good to know. Last but not least, here is a MATLAB code, which uses this information to compute the gradients:


After obtaining the gradients, you could further carry on your edge detection in a standard fashion (canny etc.).


Yes, you can use the sobel gradient for each individual color. If you are comfort in doing edge detection in a different method try this:

1- Specify clearly the color you want to obtain. Say a red color. Pure red will be something like this [1,0,0]. If you are following RGB model/color system. Where: First value: Represents how red it is. Second value: Represents how green it is. Third value: Represents how blue it is.

Now if you know the exact color you want say [0.85,0.2,0.17]. That will be fine as well.

2- Specify a range. That is up to the user to pick and mostly done by trial and error and he/she gets the exact wanted color.

If your color was [0.85,0.2,0.17]. Say your range for red color is [0.9 to 0.8]. Say your range for red color is [0.25 to 0.15]. Say your range for red color is [0.22 to 0.12].

3- Now you can easily do a for loop to search in the region of interest.

The algorithm of looping will be - Search for red values that fall in the region specified, if found, search if the green pixel value falls into the specified region, and if it was found, do lastly for the blue.

Now you do realize the places of edges, you can easily manipulate and change their colors for any different purpose.

This is an easy kinda long technique for big images, but yet simple to understand.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.