registro de aprendizaje de opencv 27: posicionamiento del chip

prefacio

Este artículo describe principalmente la aplicación del posicionamiento de chips en el procesamiento de imágenes opencv.

1. Posicionamiento de chips

//芯片定位
#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);
}

Resumir

1. El código se puede ejecutar directamente, si no lo entiende, deje un mensaje.
2. Falta la imagen material, gracias por el seguimiento.

Supongo que te gusta

Origin blog.csdn.net/taiyuezyh/article/details/122800069
Recomendado
Clasificación