静态检测大风车初版

#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;
}

posted @ 2019-08-31 11:17  星空与沧海  阅读(162)  评论(0编辑  收藏  举报