I am tring to find edges in colour image using Sobel masks, i implemented the Sobel function but the outputs between Opencv Sobel and my are defferent.
void Sobel(const Mat &image, Mat &new_image) {
int gx[3][3] = { -1,0,1,
-2,0,2,
-1,0,1 };
int gy[3][3] = { 1,2,1,
0,0,0,
-1,-2,-1 };
for (int i = 1; i < image.rows - 1; i++)
for (int j = 1; j < image.cols - 1; j++) {
int XR = 0, XG = 0, XB = 0, YR = 0, YG = 0, YB = 0;
for (int r = -1; r < 2; ++r) {
for (int c = -1; c < 2; ++c) {
XR += gx[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[0];
YR += gy[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[0];
XG += gx[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[1];
YG += gy[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[1];
XB += gx[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[2];
YB += gy[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[2];
}
}
int sumR = std::abs(XR) + std::abs(YR);
int sumG = std::abs(XG) + std::abs(YG);
int sumB = std::abs(XB) + std::abs(YB);
new_image.at<Vec3b>(i, j)[0] = (sumR < 255 ? sumR>0 ? sumR : 0 : 255);
new_image.at<Vec3b>(i, j)[1] = (sumG < 255 ? sumG>0 ? sumG : 0 : 255);
new_image.at<Vec3b>(i, j)[2] = (sumB < 255 ? sumB>0 ? sumB : 0 : 255);
}
}
int main()
{
Mat image = imread("valve.png");
Mat new_image = image.clone();
//Sobel(image, new_image);
cv::Sobel(image, new_image, -1, 1, 1);
namedWindow("Original", WINDOW_NORMAL);
imshow("Original", image);
namedWindow("Sobel", WINDOW_NORMAL);
imshow("Sobel", new_image);
waitKey();
return 0;
}