前言
这篇文章中主要讲述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.代码可以直接运行,如果有不懂得请留言哦。
2.缺少素材图片,后续补上谢谢。