Ask Your Question

Revision history [back]

The solution is quite straightforward. You should first split the channels, then calculate Sobel filtering in X and Y direction.

Sample code for OpenCV 3:

Mat input = imread("/path/to/image.png", IMREAD_COLOR);
vector<Mat> channels;
split(input, channels);
Mat SobelBx, SobelBy, SobelGx, SobelBy, SobelRx, SobelRy;
// Blue channel edges
Sobel(channels[0], SobelBx, CV_8U, 1, 0);
Sobel(channels[0], SobelBy, CV_8U, 0, 1);
// Green channel edges
Sobel(channels[0], SobelGx, CV_8U, 1, 0);
Sobel(channels[0], SobelGy, CV_8U, 0, 1);
// Red channel edges
Sobel(channels[0], SobelRx, CV_8U, 1, 0);
Sobel(channels[0], SobelRy, CV_8U, 0, 1);

Then you can simply apply operations on the result matrices, keeping in mind that if you are multiplying matrices with ^2, that you should change the data format at least to 16 bit integers because else you will have an overflow. Same goes for trace, eigenvalues, ... for which OpenCV has specific functions.

click to hide/show revision 2
No.2 Revision

The solution is quite straightforward. You should first split the channels, then calculate Sobel filtering in X and Y direction.

Sample code for OpenCV 3:

Mat input = imread("/path/to/image.png", IMREAD_COLOR);
vector<Mat> channels;
split(input, channels);
Mat SobelBx, SobelBy, SobelGx, SobelBy, SobelGy, SobelRx, SobelRy;
// Blue channel edges
Sobel(channels[0], SobelBx, CV_8U, 1, 0);
Sobel(channels[0], SobelBy, CV_8U, 0, 1);
// Green channel edges
Sobel(channels[0], SobelGx, CV_8U, 1, 0);
Sobel(channels[0], SobelGy, CV_8U, 0, 1);
// Red channel edges
Sobel(channels[0], SobelRx, CV_8U, 1, 0);
Sobel(channels[0], SobelRy, CV_8U, 0, 1);

Then you can simply apply operations on the result matrices, keeping in mind that if you are multiplying matrices with ^2, that you should change the data format at least to 16 bit integers because else you will have an overflow. Same goes for trace, eigenvalues, ... for which OpenCV has specific functions.

The solution is quite straightforward. You should first split the channels, then calculate Sobel filtering in X and Y direction.

Sample code for OpenCV 3:

Mat input = imread("/path/to/image.png", IMREAD_COLOR);
vector<Mat> channels;
split(input, channels);
Mat SobelBx, SobelBy, SobelGx, SobelGy, SobelRx, SobelRy;
// Blue channel edges
Sobel(channels[0], SobelBx, CV_8U, 1, 0);
Sobel(channels[0], SobelBy, CV_8U, 0, 1);
// Green channel edges
Sobel(channels[0], Sobel(channels[1], SobelGx, CV_8U, 1, 0);
Sobel(channels[0], Sobel(channels[1], SobelGy, CV_8U, 0, 1);
// Red channel edges
Sobel(channels[0], Sobel(channels[2], SobelRx, CV_8U, 1, 0);
Sobel(channels[0], Sobel(channels[2], SobelRy, CV_8U, 0, 1);

Then you can simply apply operations on the result matrices, keeping in mind that if you are multiplying matrices with ^2, that you should change the data format at least to 16 bit integers because else you will have an overflow. Same goes for trace, eigenvalues, ... for which OpenCV has specific functions.