Opencv边缘检测

#include 
#include 
#include 
#include 

#include 

using namespace std;
using namespace cv;

#define PARASACLAR	10
#define VIDEOPATH	("./video/demo1.mp4")
#define IMAGEPATH	("")
#define CAMERAID	0

enum VIDEOMODE {
	E_CAMERA = 0,
	E_VIDEO,
	E_IMAGE
};
void createParaWin()
{
	int thre1, thre2, area;
	thre1 = 155 * PARASACLAR;
	thre2 = 255 * PARASACLAR;
	area = 2000 * PARASACLAR;
	namedWindow("para");
	resizeWindow("para", 640, 240);
	createTrackbar("Threshold1", "para", &thre1, 255 * PARASACLAR, NULL);
	createTrackbar("Threshold2", "para", &thre2, 255 * PARASACLAR, NULL);
	createTrackbar("Area", "para", &area, 30000 * PARASACLAR, NULL);
}

void getContours(Mat imgDil, Mat imgCon)
{
	vector<vector<Point>> contours;
	vector<Point> cnt, approx;
	vector<Vec4i> hierachy;
	String str;
	double peri;
	const cv::Scalar color(255, 0, 255);
	float areaMin = 0.0f, area = 0.0f;

	cv::findContours(imgDil, contours, hierachy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
	cv::drawContours(imgCon, contours, -1, color, 2);
	cv::Rect rect;
	areaMin = (float)cv::getTrackbarPos("Area", "para") / (float)PARASACLAR;
	for (int i = 0; i < contours.size(); i++)
	{
		cnt = contours[i];
		area = (float)contourArea(cnt);
		if (area - areaMin > 0.000001)
		{

			peri = cv::arcLength(cnt, true);
			cv::approxPolyDP(cnt, approx, peri*0.02, true);
			rect = boundingRect(approx);

			cv::rectangle(imgCon, rect, cv::Scalar(0, 255, 0), 5);
			cout << "Lenght: " << approx.size() << endl;
			str = "Ponit";
			cv::putText(imgCon, str + to_string(approx.size()),
				Point(rect.x + rect.width + 20, rect.y + 20),
				cv::FONT_HERSHEY_COMPLEX, 0.7, cv::Scalar(0, 255, 0), 2);
			str = "Area";
			cv::putText(imgCon, str + to_string(area),
				Point(rect.x + rect.width + 20, rect.y + 45),
				cv::FONT_HERSHEY_COMPLEX, 0.7, cv::Scalar(0, 255, 0), 2);

		}
	}
}


void stackImage(float scalar, vector<Mat>&Vec, Mat* colVec)
{


}

void loadVideo(VideoCapture &cap, VIDEOMODE mode)
{
	if (mode == E_CAMERA)
	{
		cap.open( CAMERAID, cv::CAP_ANY);	
	}
	else if (mode = E_VIDEO)
	{
		cap.open(VIDEOPATH);
	}
	else if (mode = E_IMAGE)
	{
		cap.open(VIDEOPATH);
	}
	else
	{
		cout << "ERROR: Unable open camera!\n" << endl;
	}
	if (!cap.isOpened()) {
		cout << "ERROR: Unable open VideoCamare ....\n" << endl;
		cap.release();
		return;
	}
	cap.set(3, 640);
	cap.set(4, 240);
}

int main()
{
	Size kernel;
	Mat frame, frContour, imgBlur, imgGray, imgCanny, imgDilate, element;
	VideoCapture cap;
	
	float thre1, thre2;
	//1.初始化参数

	kernel.width = 7;
	kernel.height = 7;
	element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));

	//2.获取图像帧
	loadVideo(cap, E_VIDEO);

	//3.创建追踪参数窗口
	createParaWin();


	//边缘检测流程
	for (;;) {
		//1.逐帧读取,copy轮廓图像
		cap.read(frame);
		if (frame.empty())
		{
			cout << "ERROR: blank frame grabbed\n" << endl;
			break;
		}
		frame.copyTo(frContour);
		//2.图像高斯模糊,转为灰度图
		cv::GaussianBlur(frame, imgBlur, kernel, 1);
		cv::cvtColor(imgBlur, imgGray, cv::COLOR_BGR2GRAY);

		//3.Canny边缘检测灰度图
		thre1 = (float)cv::getTrackbarPos("Threshold1", "para") / (float)PARASACLAR;
		thre2 = (float)cv::getTrackbarPos("Threshold2", "para") / (float)PARASACLAR;
		cv::Canny(imgGray, imgCanny, thre1, thre2);

		//4.膨胀Canny边缘图像
		cv::dilate(imgCanny, imgDilate, element);
		
		//5.图像轮廓查找
		getContours(imgDilate, frContour);

		cv::imshow("Live", frContour);
		if (waitKey(5) == 'q')
			break;
	}
	return 0;
}

你可能感兴趣的:(OpenCV,opencv,计算机视觉,人工智能)