灰度处理后openCV实现图像分割处理的Sobel卷积效果XY方向梯度的边缘检测算法的C++源码

源码如下:

#include <iostream>  
#include <cmath>
#include <stdio.h>
#include <tchar.h>
#include <opencv.hpp>	  
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2\imgproc\types_c.h>  
#include <cmath>

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_<double>(3, 3) << -1, 0,
		1, -2, 0, 2, -1, 0, 1);
	// 初始化垂直Y核因子
	Mat sobely = (Mat_<double>(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<uchar>(k + i, n + j) *
						sobelx.at<double>(1 + i, 1 + j);
					edgeY += srcImage.at<uchar>(k + i, n + j) *
						sobely.at<double>(1 + i, 1 + j);
				}
			}
			// 计算垂直方向上的梯度模长
			graMagX = sqrt(pow(edgeX, 2));

			// 计算水平方向上的梯度模长
			graMagY = sqrt(pow(edgeY, 2));
			// 二值化
			resultImageX.at<uchar>(k - 1, n - 1) =
				((graMagX > threshold) ? 255 : 0);

			// 二值化
			resultImageY.at<uchar>(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<uchar>(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;
	}
}

效果如图所示:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海宝7号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值