zoukankan      html  css  js  c++  java
  • 静态检测大风车初版

    #include <opencv2/opencv.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include<vector>
    #include<math.h>
    #include<iostream>
    #include<string>
    using namespace cv;
    using namespace std;
    
    /***** 求两点间距离*****/
    float getDistance(CvPoint pointO, CvPoint pointA)
    {
    	float distance;
    	distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2);
    	distance = sqrtf(distance);
    	return distance;
    }
    
    
    int main()
    {
    	namedWindow("test", WINDOW_AUTOSIZE);
    	namedWindow("hh", 2);
    	VideoCapture cap("G:\视觉题\大能量机关(红+关灯).mov");
    	/*cap.open("G:\视觉题\大能量机关(红+关灯).mov");*/
    
    	while (1)
    	{
    		/*Mat frame;
    		Mat midImage, seed;
    		cap >> frame;
    		if (frame.empty()) break;
    		vector<Mat> imgChannels;
    		split(frame, imgChannels);
    		midImage = imgChannels.at(2) - imgChannels.at(0);
    		threshold(midImage, midImage, 100, 255, CV_THRESH_BINARY);
    		floodFill(midImage, Point(5, 50), Scalar(0),0, FLOODFILL_FIXED_RANGE);
    		Mat dstImage = Mat::zeros(midImage.rows, midImage.cols, CV_8UC3);*/
    
    
    		Mat frame;
    		Mat midImage,hsv;
    		Point2f p;
    		cap >> frame;
    		if (frame.empty()) break;
    		cvtColor(frame, hsv, CV_BGR2HSV);
    		inRange(hsv, Scalar(11, 43, 46), Scalar(25, 255, 255), midImage);
    
    		
    		Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
    		morphologyEx(midImage, midImage, MORPH_CLOSE, element);
    		imshow("hh", midImage);
    		vector<vector<Point> > contours;
    		vector<Vec4i> hierarchy;
    
    		findContours(midImage, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    		for (int j = 0; j < contours.size(); j++)
    		{
    			double t = contourArea(contours[j]);
    			if (t > 700 && t < 1000)
    			{
    				RotatedRect box;
    				box = minAreaRect(contours[j]);
    				 p = box.center;
    				 /*调试*/
    				 Point2f vertex1[4];
    				 box.points(vertex1);
    				 for (int i = 0; i < 4; i++)
    					 line(frame, vertex1[i], vertex1[(i + 1) % 4], Scalar(0, 0, 225), 2, LINE_AA);
    				 cout << t << endl;
    
    			}
    		}
    		for (int j = 0; j < contours.size(); j++)
    		{
    			double t = contourArea(contours[j]);
    			if (t > 11000 && t < 12100)
    			{
    				vector<Point> result;
    				for(int i=0;i< contours[j].size();i++)
    				if(getDistance(contours[j][i],p)>230)
    				{
    					result.push_back(contours[j][i]);
    				}
    				
    				RotatedRect boxtemp2;
    				boxtemp2 = minAreaRect(result);
    				Point2f vertex1[4];
    				Point2f p0 = boxtemp2.center;
    				boxtemp2.points(vertex1);
    				for (int i = 0; i < 4; i++)
    					line(frame, vertex1[i], vertex1[(i + 1) % 4], Scalar(225, 0, 0), 2, LINE_AA);
    				circle(frame,p0,6,Scalar(0, 225, 0), -1);
    			}
    		}
    		imshow("test", frame);
    		waitKey(1);
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    友好城市, 美团笔试题
    字符串计数, 美团笔试题
    公交车, 美团笔试题
    交错序列, 美团笔试题
    题目列表, 美团笔试题, 字符串数组比较
    图的遍历, 美团笔试题
    最长全1串, 美团笔试题
    外卖满减, 美团笔试题
    种花, 美团笔试题
    考试策略, 美团笔试题
  • 原文地址:https://www.cnblogs.com/xingkongcanghai/p/11438346.html
Copyright © 2011-2022 走看看