前言

这篇文章中主要讲述opencv图像处理中芯片定位的应用。

一、芯片定位

//芯片定位
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
	cv::Mat dst;
	cv::Mat dstbin;
	cv::Mat dsttemp;
	cv::Mat resMat;
	cv::Mat Matstate;
	cv::Mat center;
	cv::Mat src = imread("C://Users//john//Desktop//1.jpg");
	cv::Mat srcgray = imread("C://Users//john//Desktop//1.jpg", 0);
	threshold(srcgray, dstbin, 100, 255, THRESH_OTSU);  //大津法
	cv::imshow("dstbin", dstbin);
	src.copyTo(dst);  
	//bitwise_not(dstbin, dsttemp);
	vector<vector<Point>> contours;

	vector<Vec4i> hirearchy;
	findContours(dstbin, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
	int num = contours.size();

	//cout << num << endl;
	Point2f rect[4];
	for (int i = 0; i < num; i++)
	{
		
		RotatedRect rbox = minAreaRect(contours[i]);
		///cout << rbox << endl;
		int area = contourArea(contours[i]);//计算轮廓面积
		rbox.points(rect);  //把最小外接矩形四个端点复制给rect数组
		if (fabs(rbox.size.width * 1.0 / rbox.size.height - 1) < 0.2&&area>=100)
		{
			drawContours(dst, contours, i, Scalar(255, 0, 0), -1, 8);
			
		    for (int j = 0; j<4; j++)
			{
		         line(dst, rect[j], rect[(j + 1) % 4], Scalar(255, 255, 255), 2, 8);  //绘制最小外接矩形每条边
		    }
		}
		
	}

	cv::imshow("dsttemp", dstbin);
	cv::imshow("dst", dst);

	waitKey(0);
}

原图

在这里插入图片描述
效果图
在这里插入图片描述

总结

1.代码可以直接运行,如果有不懂得请留言哦。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐