1. 程式人生 > >【輪廓查詢-1】

【輪廓查詢-1】

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 240;
int max_thresh = 255;
int core = 6;
int max_core = 10;


/// Function header
void thresh_callback(int, void*);

/** @function main */
int main(int argc, char** argv)
{
	/// 載入源影象
	src = imread("778924580128721738.jpg", 1);
	//src = imread("4-0-36-29-90_10.jpg", 1);
	//src = imread("4-0-36-29-90_10.jpg", 1);
	//src = imread("4-0-36-29-90_10.jpg", 1);
	//resize(src, src, Size(512, 512));
	/// 轉為灰度圖並模糊化
	cvtColor(src, src_gray, CV_BGR2GRAY);
	bitwise_not(src_gray, src_gray);
	//blur(src_gray, src_gray, Size(3, 3));

	/// 建立窗體
	char* source_window = "Source";
	namedWindow(source_window, CV_WINDOW_AUTOSIZE);
	imshow(source_window, src);

	createTrackbar(" Threshold:", "Source", &thresh, max_thresh, thresh_callback);
	createTrackbar(" erode:", "Source", &core, max_core, thresh_callback);
	thresh_callback(0, 0);

	waitKey(0);
	return(0);
}

/** @function thresh_callback */
void thresh_callback(int, void*)
{
	Mat threshold_output;
	vector<vector<Point> > contours;
	vector<vector<Point> > maxcontours;
	vector<Vec4i> hierarchy;

	// 閾值化檢測邊界
	threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY);
	Mat element = getStructuringElement(2, Size(7, 7));	 //膨脹腐蝕核
	for (int i = 0;i<2;i++) {
		dilate(threshold_output, threshold_output, element);
		i++;
	}
	for (int i = 0;i<core;i++) {
		erode(threshold_output, threshold_output, element);
		i++;
	}
	imshow("thresh", threshold_output);
	/// 尋找輪廓
	findContours(threshold_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
	//篩選輪廓
	/*for (vector<vector<Point>>::iterator it = contours.begin();it != contours.end();it++) {
		
		if (contourArea(*it)< contourArea(*(it++))) {
			
		}

	}*/
	/// 對每個找到的輪廓建立可傾斜的邊界框和橢圓
	vector<RotatedRect> minRect(contours.size());
	

	for (int i = 0; i < contours.size(); i++)
	{
		minRect[i] = minAreaRect(Mat(contours[i]));
	
	}

	/// 繪出輪廓及其可傾斜的邊界框和邊界橢圓
	Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
	for (int i = 0; i < contours.size(); i++)
	{

		//rectPoint變數中得到了矩形的四個頂點座標	
		//RotatedRect rectPoint = minAreaRect(points);//定義一個儲存以上四個點的座標的變數		
		// Point2f fourPoint2f[4];	
		//將rectPoint變數中儲存的座標值放到 fourPoint的陣列中		
		// rectPoint.points(fourPoint2f);

		Scalar color = Scalar(255, 0, 0);

		//drawContours(drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point());


		// rotated rectangle
		Point2f center; float radius;

		minEnclosingCircle(contours[i], center, radius);



		if ((0.25*src.cols < center.x && center.x<0.38*src.cols) && (0.5*src.rows < center.y &&center.y< 0.8*src.rows)) {
			//面積篩選
			if (3.1415*(radius + 20)*(radius + 20) < 6500) {
				/*	cout << src.cols << endl;
				cout<< src.rows<<endl;
				cout << center.x << endl;
				cout << center.x / src.cols << endl;
				cout << center.y << endl;
				cout << center.y / src.rows << endl;*/
				circle(src, center, radius + 20, Scalar(0, 0, 255), 2);
				//cout << 3.1415*(radius + 20)*(radius + 20) << endl;

			}
		
		}



	}

	/// 結果在窗體中顯示
	namedWindow("Contours", CV_WINDOW_AUTOSIZE);
	imshow("標註", src);
}

相關連結:https://blog.csdn.net/abcvincent/article/details/79275429