【图像处理】变压器铭牌位置检测

应项目需求,需要检测到变压器上铭牌地位置,然后配合机械臂进行下一步工作。

原图:

程序算法具体使用Sobel算子求取梯度,由于变压器独特地纹理,所以程序中使用y方向地梯度。

        Mat srcGray;
	cvtColor(matsrc, srcGray, CV_RGB2GRAY);

	Mat grad_x, grad_y;
	Mat abs_grad_x, abs_grad_y, matdst, dst;

	//求x方向上梯度
	Sobel(srcGray, grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
	convertScaleAbs(grad_x, abs_grad_x);
	//namedWindow("X方向上Sobel", WINDOW_NORMAL);
	//imshow("X方向上Sobel",abs_grad_x);

	Sobel(srcGray, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
	convertScaleAbs(grad_y, abs_grad_y);
	//namedWindow("Y方向上Sobel", WINDOW_NORMAL);
	//imshow("Y方向上Sobel", abs_grad_y);

	//addWeighted(abs_grad_x,0.3, abs_grad_y,0.7,0, matdst);
	//namedWindow("Sobel", WINDOW_NORMAL);

	//大津算法求阈值
	int pos = otsu(srcGray);

	threshold(abs_grad_y, dst, pos, 255, CV_THRESH_BINARY);
	namedWindow("Sobel", WINDOW_NORMAL);
	imshow("Sobel", dst);

	//去噪 高斯滤波
	//Mat srcGauss;
	//GaussianBlur(dst,srcGauss,Size(15,15),0,0);
	//namedWindow("滤波",WINDOW_NORMAL);
	//imshow("滤波",srcGauss);

	//去噪 中值滤波
	Mat Median;
	medianBlur(dst, Median, 5);
	namedWindow("滤波", WINDOW_NORMAL);
	imshow("滤波", Median);

	//膨胀
	Mat element = getStructuringElement(MORPH_RECT, Size(71, 71));
	Mat out;
	dilate(Median, out, element);
	namedWindow("膨胀", WINDOW_NORMAL);
	imshow("膨胀", out);

	//腐蚀
	Mat out2;
	erode(out, out2, element);
	namedWindow("腐蚀", WINDOW_NORMAL);
	imshow("腐蚀", out2);

	Mat A = out2.clone();

	vector<vector<Point>> contours;
	vector<Vec4i> hierarchy;
	findContours(A, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);

	for (int i = 0; i < contours.size(); i++)
	{
		Mat contour(contours[i]);
		double area = contourArea(contour);
		cout << area << endl;
		if (area > 400000)  //此处应查找到二值化后的最大面积块为此处阈值,即可变成自适应
		{
			Mat d;
			Rect r = boundingRect(Mat(contours[i]));
			rectangle(matsrc, r, Scalar(0, 0, 255), 10, 8, 0);

			//将截取出来的ROI部分生成一张图片
			Rect new_rect;
			vector<Point> all_points;
			
			for (int j = 0; j < contours[i].size(); j++)
			{
				all_points.push_back(contours[i][j]);
			}
			
			new_rect = boundingRect(all_points);
			Mat result_img = matsrc(new_rect);
			namedWindow("A", WINDOW_NORMAL);
			imshow("A", result_img);

		}

	}

检测结果:

程序刚刚完成,其中还有一些需要修改地地方,我认为最重要地就是函数参数地设置,因为本程序算法,仅仅针对于那么小部分图片使用,背景比较复杂地图片,或者其他型号地变压器,本程序算法失效,所以真正上项目地时候,应该采取真实场景下图片进行处理实验,以保证工业正常稳定检测。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值