我正试图使用Sobel蒙版在彩色图像中找到边缘,我实现了Sobel功能,但是Opencv Sobel和我之间的输出是不同的。
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;
}
答案 0 :(得分:3)
cv::Sobel
在内部使用getDerivKernels
创建滤波器系数,该系数为两个方向的导数创建一维滤波器掩码。一阶导数的形式为[-1,0,1]。后来,它们通过outer product相乘并形成一个矩阵:
`1 0 -1
0 0 0
-1 0 1
如果将此矩阵应用于Sobel函数并在渐变计算中删除abs函数,则将获得与OpenCV相同的结果。
void Sobel2(const Mat &image, Mat &new_image) {
double gy[3][3] = { 1,0,-1,
0,0,0,
-1,0,1 };
for (int i = 1; i < image.rows - 1; i++)
for (int j = 1; j < image.cols - 1; j++) {
double 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) {
YR += gy[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[0];
YG += gy[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[1];
YB += gy[r + 1][c + 1] * image.at<Vec3b>(i + r, j + c)[2];
}
}
new_image.at<Vec3b>(i, j)[0] = (YR < 255 ? YR>0 ? YR : 0 : 255);
new_image.at<Vec3b>(i, j)[1] = (YG < 255 ? YG>0 ? YG : 0 : 255);
new_image.at<Vec3b>(i, j)[2] = (YB < 255 ? YB>0 ? YB : 0 : 255);
}
}
以下代码可帮助您了解过滤器矩阵的计算方式:
cv::Mat kx, ky;
cv::getDerivKernels(kx, ky, 1, 1, 3);
cv::Mat k = kx * ky.t(); // k is the filter matrix that is used internally in cv::Sobel function