源码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
bool sobelEdge(Mat& srcImage, Mat& resultImageX, Mat& resultImageY, uchar threshold)
{ //sobel算子一阶梯度---边缘检测应用部分卷积提取源码
CV_Assert(srcImage.channels() == 1);
// 初始化水平X核因子
Mat sobelx = (Mat_(3, 3) << -1, 0,
1, -2, 0, 2, -1, 0, 1);
// 初始化垂直Y核因子
Mat sobely = (Mat_(3, 3) << -1, -2, -1,
0, 0, 0, 1, 2, 1);
resultImageX = Mat::zeros(srcImage.rows - 2,
srcImage.cols - 2, srcImage.type());
resultImageY = Mat::zeros(srcImage.rows - 2,
srcImage.cols - 2, srcImage.type());
double edgeX = 0;
double edgeY = 0;
double graMagX = 0;// 垂直方向上的梯度模长
double graMagY = 0;// 水平方向上的梯度模长
for (int k = 1; k < srcImage.rows - 1; ++k)
{
for (int n = 1; n < srcImage.cols - 1; ++n)
{
edgeX = 0;
edgeY = 0;
// 遍历计算水平与垂直梯度
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
edgeX += srcImage.at(k + i, n + j) *
sobelx.at(1 + i, 1 + j);
edgeY += srcImage.at(k + i, n + j) *
sobely.at(1 + i, 1 + j);
}
}
// 计算垂直方向上的梯度模长
graMagX = sqrt(pow(edgeX, 2));
// 计算水平方向上的梯度模长
graMagY = sqrt(pow(edgeY, 2));
// 二值化
resultImageX.at(k - 1, n - 1) =
((graMagX > threshold) ? 255 : 0);
// 二值化
resultImageY.at(k - 1, n - 1) =
((graMagY > threshold) ? 255 : 0);
}
}
return true;
}
int OTSU(Mat &srcImage)
{ //根据图像的矩阵求出最佳阈值灰度
int nRows = srcImage.rows;
int nCols = srcImage.cols;
int threshold = 0;
double max = 0.0;
double AvePix[256];
int nSumPix[256];
double nProDis[256];
double nSumProDis[256];
for (int i = 0; i < 256; i++)
{
AvePix[i] = 0.0;
nSumPix[i] = 0;
nProDis[i] = 0.0;
nSumProDis[i] = 0.0;
}
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
nSumPix[(int)srcImage.at(i, j)]++;
}
}
for (int i = 0; i < 256; i++)
{
nProDis[i] = (double)nSumPix[i] / (nRows*nCols);
}
AvePix[0] = 0;
nSumProDis[0] = nProDis[0];
for (int i = 1; i < 256; i++)
{
nSumProDis[i] = nSumProDis[i - 1] + nProDis[i];
AvePix[i] = AvePix[i - 1] + i*nProDis[i];
}
double mean = AvePix[255];
for (int k = 1; k < 256; k++)
{
double PA = nSumProDis[k];
double PB = 1 - nSumProDis[k];
double value = 0.0;
if (fabs(PA) > 0.001 && fabs(PB) > 0.001)
{
double MA = AvePix[k];//前一半的平均
double MB = (mean - PA*MA) / PB;//后一半的平均
value = value = (double)(PA * PB * pow((MA - MB), 2));//类间方差
//value = (double)(PA * PB * pow((MA-MB),2));//类间方差方法2
//pow(PA,1)* pow((MA - mean),2) + pow(PB,1)* pow((MB - mean),2)
if (value > max)
{
max = value;
threshold = k;
}
}
}
return threshold;
}
int main()
{
int width = 600, higth = 400; //定义显示的图像大小
//读取文件图片,试运行
//Mat srcImage = cv::imread("D:\\xlxl\\001.jpg");
Mat srcImage = imread("D:\\xlxl\\36.png");
if (srcImage.data == 0)
/*if (srcImage.data == 0)*/
{
cout << "文件不存在" << endl;
system("pause");
return false;
}
else
{ //开始进行图像处理
Mat srcGray;
//Mat orangess = srcImage;
namedWindow("原始图像", 0);
resizeWindow("原始图像", width, higth);
moveWindow("原始图像", 0, higth);
imshow("原始图像", srcImage);
//转换为灰度图
cvtColor(srcImage, srcGray, COLOR_BGR2GRAY);
namedWindow("灰度图像", 0);
resizeWindow("灰度图像", width, higth);
moveWindow("灰度图像", width, higth);
imshow("灰度图像", srcGray);
//计算阈值
int otsuThreshold = OTSU(srcGray);
cout << "阈值大小为" << otsuThreshold << endl;
Mat XresultImage;
Mat YresultImage;
sobelEdge(srcGray, XresultImage, YresultImage, otsuThreshold);
Mat resultImage;
//水平垂直边缘叠加
addWeighted(XresultImage, 0.5, YresultImage, 0.5, 0.0, resultImage);
namedWindow("X方向梯度", 0);
resizeWindow("X方向梯度", width, higth);
moveWindow("X方向梯度", 0, 2 * higth);
imshow("X方向梯度", XresultImage);
namedWindow("Y方向梯度", 0);
resizeWindow("Y方向梯度", width, higth);
moveWindow("Y方向梯度", width, 2 * higth);
imshow("Y方向梯度", YresultImage);
namedWindow("卷积效果", 0);
resizeWindow("卷积效果", width, higth);
moveWindow("卷积效果", 2 * width, 2 * higth);
imshow("卷积效果", resultImage);
waitKey(0);
system("pause");
return true;
}
}
效果如图所示: