任务描述:将小装甲板上的小正方形识别并抠出
#include<iostream>#include<opencv2/opencv.hpp>#include<highgui/highgui.hpp>#include<imgproc/imgproc.hpp>using namespace std;using namespace cv;void fun(){Mat srcImage,dstImage1,dstImage2,dstImage3;srcImage=imread(/home/ma/下载/photo.jpg);cvtColor(srcImage,dstImage1,COLOR_BGR2GRAY);threshold(dstImage1,dstImage2,180,255,THRESH_BINARY);imshow(2,dstImage2);dstImage3=Mat::zeros(srcImage.size(),srcImage.type());vector<vector<Point>>contours;vector<Vec4i>hierarchy;vector<Point>point;findContours(dstImage2,contours,hierarchy,RETR_CCOMP,CHAIN_APPROX_NONE);for(int i=0;i<contours.size();i++){approxPolyDP(contours[i], point, 1, true);Rect rect=boundingRect(contours[i]);Point pt=Point(rect.width,rect.height);Point pr=Point(-rect.width,rect.height);double x=double(MIN(rect.width,rect.height))/double(MAX(rect.width,rect.height));if((pt.x*pr.x)+(pt.y*pr.y)>0&&(pt.x*pt.x)+(pt.y*pt.y)-(pr.x*pr.x)-(pr.y*pr.y)==0&&point.size()==4&&x>0.9){drawContours(dstImage3,contours,i,Scalar(0,255,255),2);}} imshow(dst,dstImage3);waitKey(0);}int main(){fun();return 0;}
