【OpenCV】超详细边缘提取算法流程(附详细代码)

在传统的计算机视觉领域,经常需要使用一些传统的图像处理算法完成对图像的边缘提取功能,通过对图像的边缘进行提取完成对目标对象的分割,目标分割技术又包括语义分割与实例分割,比较高端的鲁棒性较强的还是需要卷积神经网络算法进行相关的训练,如fcn全连接网络,mask-rcnn实例分割网络。本案例旨在采用传统的图像处理技术完成对图像的边缘检测任务,并通过膨胀腐蚀操作进行连通域的提取,之后通过连通域的填充以及掩膜操作完成目标对象的分割。

具体需要达到的目标如下:

                         

 上图所示,可以很好的将河道信息进行提取。

1. 具体算法流程

首先对采集的图片进行灰度化处理(方便进行数据处理),然后对灰度图像进行中值滤波操作去除湖面上的细小杂质,之后通过x方向和y方向上的Sobel梯度算子分别获取梯度图像,并将梯度图像转换成CV_8UC1类型,并对转换后的x,y方向上的梯度图像进行OTSU二值化操作获取二值图像,并对两幅二值图像按对应像素位置进行与运算,目的是为了去除河道上的波纹干扰。(可以继续对与运算之后的结果图进行中值滤波去除湖面上的细小杂质)。最后对二值图进行多次迭代的膨胀腐蚀操作以及小区域块的填充操作(用到findContours与drawContours接口进行轮廓查找与填充)获取河道连通区域。

2.流程图

可以通过如下流程图进行展示。

                                                   

3. 涉及到的代码

int main() {
	Mat srcImage, srcImage2, srcImage3;
	for (int j = 1; j <= 172; j++)
	{
		//白天总共172张图片。1-172;验证:1,162,146;
		//(挑选最好的效果作为模板。对比108与117进行分析)。
		//if (j == 108 || j == 117)
		//{//对比分析之后选择第108张图片作为模板图片
		char ch[4096] = { 0 };
		sprintf(ch, "..\\findriveredge\\day\\2 (%d).jpg", j);
		srcImage = imread(ch, IMREAD_ANYCOLOR);
		srcImage2 = srcImage.clone();
		srcImage3 = srcImage.clone();
		if (srcImage.empty())
		{
			return -1;
		}
		if (srcImage.channels() == 3)
		{
			cvtColor(srcImage, srcImage, COLOR_BGR2GRAY);
		}

		Mat outImage;
		medianBlur(srcImage, outImage, g_nKrenel);//首先对灰度图进行中值滤波操作,去除一些杂质。

		Mat grad_x, abs_grad_x, grayImage_x;
		Sobel(outImage, grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
		convertScaleAbs(grad_x, abs_grad_x);
		abs_grad_x.convertTo(grayImage_x, CV_8U);


		Mat grad_y, abs_grad_y, grayImage_y;
		Sobel(outImage, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
		convertScaleAbs(grad_y, abs_grad_y);
		abs_grad_y.convertTo(grayImage_y, CV_8U);


		Mat XBin, YBin;
		int n_thresh_x = myOtsu(grayImage_x);
		threshold(grayImage_x, XBin, n_thresh_x, 255, THRESH_BINARY);


		int n_thresh_y = myOtsu(grayImage_y);
		threshold(grayImage_y, YBin, n_thresh_y, 255, THRESH_BINARY);


		Mat Bin;
		bitwise_and(XBin, YBin, Bin);


		Mat outBin;
		medianBlur(Bin, outBin, 3);//去除一些杂质点。


		Mat outBin2 = outBin.clone();
		int n_iterations = 5;
		Mat element = getStructuringElement(MORPH_ELLIPSE, Size(15, 15));//膨胀变亮形成连通域。
		Mat element2 = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));//腐蚀操作断开一些连通域。
		dilate(outBin2, outBin2, element, Point(-1, -1), n_iterations);
		erode(outBin2, outBin2, element2, Point(-1, -1), 3);//腐蚀操作,断开河流区域内部的连接区域,方便后续的填充处理。


		//将河流ROI区域小块连通域填黑。
		Mat outBin3 = outBin2.clone();
		vector<vector<Point>> contours;
		vector<Vec4i> hie;
		findContours(outBin3, contours, hie, RETR_LIST, CHAIN_APPROX_SIMPLE);
		float f_area = 0.0;
		for (int i = 0; i < contours.size(); i++)
		{
			f_area = contourArea(contours[i]);
			if (f_area < 250000)
			{
				drawContours(outBin3, contours, i, Scalar(0), -1);
			}
		}


		//由于final bin2在腐蚀过程中存在部分背景区域为黑色空洞,需要将其填白。
		Mat outBin4_tmp = ~outBin3;
		Mat outBin4;
		contours.clear();
		hie.clear();
		findContours(outBin4_tmp, contours, hie, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
		for (unsigned int i = 0; i < contours.size(); i++)
		{
			f_area = contourArea(contours[i]);
			if (f_area < 250000)
			{
				drawContours(outBin4_tmp, contours, i, Scalar(0), -1);
			}
		}
		outBin4 = ~outBin4_tmp;

		//迭代腐蚀突出河流边界区域。
		erode(outBin4, outBin4, element, Point(-1, -1), 10);


		//(可以对腐蚀图在进行一次外轮廓填充)。
		Mat outBin5_tmp = ~outBin4.clone();
		Mat outBin5;
		contours.clear();
		hie.clear();
		findContours(outBin5_tmp, contours, hie, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
		for (unsigned int i = 0; i < contours.size(); i++)
		{
			f_area = contourArea(contours[i]);
			if (f_area < 100000)
			{
				drawContours(outBin5_tmp, contours, i, Scalar(0), -1);
			}
		}
		outBin5 = ~outBin5_tmp;


		//根据outBin4,对原rgb图取感兴趣区域(即河流区域)
		for (int i = 0; i < outBin5.rows; i++)
		{
			for (int j = 0; j < outBin5.cols; j++)
			{
				if (outBin5.at<uchar>(i, j) == 255)
				{
					srcImage2.at<Vec3b>(i, j)[0] = 0;
					srcImage2.at<Vec3b>(i, j)[1] = 0;
					srcImage2.at<Vec3b>(i, j)[2] = 0;
				}
			}
		}
		namedWindow("finalImage", 0);
		imshow("finalImage", srcImage2);//这里的srcImage2表示最后所需效果图


		cout << j << endl;
		waitKey(30);
	}
	return 0;
}

 

 

 

 

 

  • 14
    点赞
  • 172
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这个需求比较复杂,可能需要结合具体的数据和算法进行实现。以下是一个基本的流程,供您参考: 1. 读取声图像数据,可以使用Python中的OpenCV库进行读取和处理。 2. 对声图像进行边缘提取,可以使用Sobel、Canny等算法进行处理。以下是使用OpenCV中的Canny算法进行边缘提取的示例代码: ```python import cv2 import numpy as np # 读取声图像 img = cv2.imread('ultrasound.png', 0) # 进行Canny边缘提取 edges = cv2.Canny(img, 100, 200) # 显示结果 cv2.imshow('Edges', edges) cv2.waitKey(0) cv2.destroyAllWindows() ``` 3. 将边缘提取后的图像进行三维重建,可以使用立体匹配、体素化等算法进行处理。以下是使用OpenCV中的立体匹配算法进行三维重建的示例代码: ```python import cv2 import numpy as np # 读取左右两张声图像 imgL = cv2.imread('left.png', 0) imgR = cv2.imread('right.png', 0) # 进行SGBM立体匹配 window_size = 3 left_matcher = cv2.StereoSGBM_create( minDisparity=0, numDisparities=16*5, blockSize=window_size, P1=8*1*window_size**2, P2=32*1*window_size**2, disp12MaxDiff=1, uniquenessRatio=10, speckleWindowSize=100, speckleRange=32 ) right_matcher = cv2.ximgproc.createRightMatcher(left_matcher) displ = left_matcher.compute(imgL, imgR) dispr = right_matcher.compute(imgR, imgL) displ = np.int16(displ) dispr = np.int16(dispr) # 进行体素化 voxel_size = 0.5 # 体素大小 reproject_threshold = 1.5 # 重投影误差阈值 depth_map = cv2.reprojectImageTo3D(displ, np.eye(3)) * voxel_size # 显示结果 cv2.imshow('Depth Map', depth_map) cv2.waitKey(0) cv2.destroyAllWindows() ``` 需要注意的是,以上代码仅为示例代码,具体实现还需要结合具体的数据和算法进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值