java边缘检测_Sobel边缘检测实现

我试图从头开始实施sobel边缘检测,但我的输出似乎与OpenCV的sobel功能不匹配 . 我在s和y方向上用sobel算子对图像进行相关,然后将梯度幅度计算为x和y方向上的幅度平方和的平方根 . 我认为问题是如何为边缘检测分配阈值 .

码:

#include

#include "opencv2/core/core.hpp"

#include "opencv2/highgui/highgui.hpp"

#include "opencv2/opencv.hpp"

#include "opencv2/imgproc/imgproc.hpp"

#include

#include

using namespace cv;

using namespace std;

int main()

{

// Reading image

Mat img = imread("1.jpg");

// Displaying image

//imshow("Original Image",img);

//waitKey(0);

// Converting to grayscale

Mat img_gray,image_blur;

GaussianBlur( img, image_blur, Size(5,5), 3, 3);

cvtColor(image_blur,img_gray,CV_RGB2GRAY);

// Displaying grayscale image

//imshow("Original Image",img_gray);

//waitKey(0);

int cols = img_gray.cols;

int rows = img_gray.rows;

// Creating sobel operator in x direction

int sobel_x[3][3] = {-1,0,1,-2,0,2,-1,0,1};

// Creating sobel operator in y direction

int sobel_y[3][3] = {-1,-2,-1,0,0,0,1,2,1};

int radius = 1;

// Handle border issues

Mat _src;

copyMakeBorder(img_gray, _src, radius, radius, radius, radius, BORDER_REFLECT101);

// Create output matrix

Mat gradient_x = img_gray.clone();

Mat gradient_y = img_gray.clone();

Mat gradient_f = img_gray.clone();

int max=0;

// Correlation loop in x direction

// Iterate on image

for (int r = radius; r < _src.rows - radius; ++r)

{

for (int c = radius; c < _src.cols - radius; ++c)

{

int s = 0;

// Iterate on kernel

for (int i = -radius; i <= radius; ++i)

{

for (int j = -radius; j <= radius; ++j)

{

s += _src.at(r + i, c + j) * sobel_x[i + radius][j + radius];

}

}

gradient_x.at(r - radius, c - radius) = s/30;

/*if(s>200)

gradient.at(r - radius, c - radius) = 255;

else

gradient.at(r - radius, c - radius) = 0;

*/

}

}

Mat absGrad_x;

convertScaleAbs( gradient_x, absGrad_x );

// Conrrelation loop in y direction

// Iterate on image

for (int r = radius; r < _src.rows - radius; ++r)

{

for (int c = radius; c < _src.cols - radius; ++c)

{

int s = 0;

// Iterate on kernel

for (int i = -radius; i <= radius; ++i)

{

for (int j = -radius; j <= radius; ++j)

{

s += _src.at(r + i, c + j) * sobel_y[i + radius][j + radius];

}

}

gradient_y.at(r - radius, c - radius) = s/30;

/*if(s>200)

gradient.at(r - radius, c - radius) = 255;

else

gradient.at(r - radius, c - radius) = 0;

*/

}

}

Mat absGrad_y;

convertScaleAbs( gradient_y, absGrad_y );

//Mat absGrad;

/*for(int i=0; i

{

for(int j=0; j

{

absGrad.at(i,j) = sqrt( pow(absGrad_x.at(i,j),2) + pow(absGrad_y.at(i,j),2) );

if(absGrad.at(i,j) >240)

absGrad.at(i,j) = 100;

else

absGrad.at(i,j) = 0;

}

}

*/

//Calculating gradient magnitude

for(int i=0; i

{

for(int j=0; j

{

gradient_f.at(i,j) = sqrt( pow(gradient_x.at(i,j),2) + pow(gradient_y.at(i,j),2) );

if(gradient_f.at(i,j) >240)

gradient_f.at(i,j) = 100;

else

gradient_f.at(i,j) = 0;

}

}

/*

imshow("grad x",gradient_x);

waitKey(0);

imshow("grad y",gradient_y);

waitKey(0);

*/

imshow("grad magnitude",gradient_f);

waitKey(0);

//imshow("absolute grad magnitude",absGrad);

// waitKey(0);

//Sobel edge detection function from OpenCV

cv::Mat Gx, Gy; int ksize=3;

Mat abs_grad_x, abs_grad_y;

cv::Sobel(img_gray, Gx, CV_8U, 1, 0, ksize);

convertScaleAbs( Gx, abs_grad_x );

cv::Sobel(img_gray, Gy, CV_8U, 0, 1, ksize);

convertScaleAbs( Gy, abs_grad_y );

Mat grad;

addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

imshow("Sobel Image",grad);

waitKey(0);

return 0;

}

图片-

1.Original Image-

oBlxu.png

我对Sobel边缘检测的实施

ORQsQ.png

Opencv Sobel边缘功能输出

dowax.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值