1. 程式人生 > >基於opencv的物體定位

基於opencv的物體定位

opencv是一個很強大的機器視覺庫,利用它我們可以開發出豐富多彩的使用專案。近日,我在研究一個圖中物體定位系統。本程式用的是OpenCV2.4.9,附帶OpenCV3.0。

程式中的原圖為我隨手拍的一張圖片


圖中有三個物體,都是藍色的,我首先取原圖的藍色通道變為灰度圖


灰度圖經過中值濾波後可以得到去噪後的圖片


根據原圖的藍色通道和紅色通道的大概取值範圍,我們可得到比較滿意的二值圖


為了去掉物體中少量的黑色部分,我用閉運算


然而,圖中最上面的那個物體裡面還有一塊很大的黑色(目前我也不知道怎麼去掉,如果有大神知道望告知~~)

接下來就是找出物體的輪廓


最後找到能包圍輪廓的最小矩形


好了,佔時就這麼多了

下面是配套的程式

OpenCV2.4.9半根

#include<opencv2\opencv.hpp>
#include<iostream>
#define BIN_DIV 110

using namespace std;
using namespace cv;

int main()
{
	Mat srcImg, midImg, dstImg;
	srcImg = imread("hehe.jpg");
	Mat xianshi = srcImg.clone();
	Mat redChannel;
	namedWindow("【原圖】", WINDOW_NORMAL);
	imshow("【原圖】", srcImg);
	Mat grayImg;
	vector<Mat> channels;
	split(srcImg, channels);
	//cvtColor(srcImg,grayImg,COLOR_BGR2GRAY);
	grayImg = channels.at(0);
	redChannel = channels.at(2);
	namedWindow("【灰度圖】", WINDOW_NORMAL);
	imshow("【灰度圖】", grayImg);
	//均值濾波
	blur(grayImg, grayImg, Size(20, 20), Point(-1, -1));
	namedWindow("【均值濾波後】", WINDOW_NORMAL);
	imshow("【均值濾波後】", grayImg);
	//轉化為二值圖
	Mat midImg1 = grayImg.clone();
	int rowNumber = midImg1.rows;
	int colNumber = midImg1.cols;

	for (int i = 0; i<rowNumber; i++)
	{
		uchar* data = midImg1.ptr<uchar>(i);  //取第i行的首地址
		uchar* redData = redChannel.ptr<uchar>(i);
		for (int j = 0; j<colNumber; j++)
		{
			if (data[j]>BIN_DIV&&redData[j]<BIN_DIV *2/ 3)
				data[j] = 255;
			else
				data[j] = 0;
		}
	}
	namedWindow("【二值圖】", WINDOW_NORMAL);
	imshow("【二值圖】", midImg1);
	Mat midImg2 = midImg1.clone();
	Mat element = getStructuringElement(MORPH_RECT, Size(40, 40));
	morphologyEx(midImg1, midImg2, MORPH_CLOSE, element);
	namedWindow("【閉運算後】", WINDOW_NORMAL);
	imshow("【閉運算後】", midImg2);
	cout << "midImg1.channel=" << midImg1.channels() << endl;
	cout << "mdiImg1.depth" << midImg1.depth() << endl;
	//查詢影象輪廓
	Mat midImg3 = Mat::zeros(midImg2.rows, midImg2.cols, CV_8UC3);
	vector<vector<Point>> contours;
	vector<Vec4i> hierarchy;
	findContours(midImg2, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
	int index = 0;
	for (; index >= 0; index = hierarchy[index][0])
	{
		Scalar color(255, 255, 255);
		drawContours(midImg3, contours, index, color, NULL, 8, hierarchy);
	}
	namedWindow("【輪廓圖】", WINDOW_NORMAL);
	imshow("【輪廓圖】", midImg3);
	Mat midImg4 = midImg3.clone();
	//建立包圍輪廓的矩形邊界
	for (int i = 0; i<contours.size(); i++)
	{
		//每個輪廓
		vector<Point> points = contours[i];
		//對給定的2D點集,尋找最小面積的包圍矩形
		RotatedRect box = minAreaRect(Mat(points));
		Point2f vertex[4];
		box.points(vertex);
		//繪製出最小面積的包圍矩形
		line(xianshi, vertex[0], vertex[1], Scalar(100, 200, 211), 6, CV_AA);
		line(xianshi, vertex[1], vertex[2], Scalar(100, 200, 211), 6, CV_AA);
		line(xianshi, vertex[2], vertex[3], Scalar(100, 200, 211), 6, CV_AA);
		line(xianshi, vertex[3], vertex[0], Scalar(100, 200, 211), 6, CV_AA);
		//繪製中心的游標
		Point s1, l, r, u, d;
		s1.x = (vertex[0].x + vertex[2].x) / 2.0;
		s1.y = (vertex[0].y + vertex[2].y) / 2.0;
		l.x = s1.x - 10;
		l.y = s1.y;

		r.x = s1.x + 10;
		r.y = s1.y;

		u.x = s1.x;
		u.y = s1.y - 10;

		d.x = s1.x;
		d.y = s1.y + 10;
		line(xianshi, l, r, Scalar(100, 200, 211), 2, CV_AA);
		line(xianshi, u, d, Scalar(100, 200, 211), 2, CV_AA);
	}
	namedWindow("【繪製的最小面積矩形】", WINDOW_NORMAL);
	imshow("【繪製的最小面積矩形】", xianshi);
	waitKey(0);
	return 0;
}

OpenCV3.0版本
#include<opencv2\opencv.hpp>
#include<iostream>
#define BIN_DIV 120

using namespace std;
using namespace cv;

int main()
{
	Mat srcImg=imread("haha.jpg");
	Mat xianshi=srcImg.clone();
	Mat redChannel;
	namedWindow("【原圖】",WINDOW_NORMAL);
	imshow("【原圖】",srcImg);
	Mat grayImg;
	vector<Mat> channels;
	split(srcImg,channels);
	//cvtColor(srcImg,grayImg,COLOR_BGR2GRAY);
	grayImg=channels.at(0);
	redChannel=channels.at(2);
	namedWindow("【灰度圖】",WINDOW_NORMAL);
	imshow("【灰度圖】",grayImg);	
	//均值濾波
	blur(grayImg,grayImg,Size(20,20),Point(-1,-1));
	namedWindow("【均值濾波後】",WINDOW_NORMAL);
	imshow("【均值濾波後】",grayImg);
	//轉化為二值圖
	Mat midImg1=grayImg.clone();
	int rowNumber=midImg1.rows;
	int colNumber=midImg1.cols;

	for(int i=0;i<rowNumber;i++)
	{
		uchar* data=midImg1.ptr<uchar>(i);  //取第i行的首地址
		uchar* redData=redChannel.ptr<uchar>(i);
		for(int j=0;j<colNumber;j++)
		{
			if(data[j]>BIN_DIV&&redData[j]<BIN_DIV/2)
				data[j]=0;
			else
				data[j]=255;
		}
	}
	namedWindow("【二值圖】",WINDOW_NORMAL);
	imshow("【二值圖】",midImg1);
	Mat midImg2=midImg1.clone();
	Mat element=getStructuringElement(MORPH_RECT,Size(20,20));
	morphologyEx(midImg1,midImg2,MORPH_OPEN,element);
	namedWindow("【開運算後】",WINDOW_NORMAL);
	imshow("【開運算後】",midImg2);
	cout<<"midImg1.channel="<<midImg1.channels()<<endl;
	cout<<"mdiImg1.depth"<<midImg1.depth()<<endl;
	//查詢影象輪廓
	Mat midImg3=Mat::zeros(midImg2.rows,midImg2.cols,CV_8UC3);
	vector<vector<Point>> contours;
	vector<Vec4i> hierarchy;
	findContours(midImg2,contours,hierarchy,RETR_CCOMP,CHAIN_APPROX_SIMPLE);
	int index=0;
	for(;index>=0;index=hierarchy[index][0])
	{
		Scalar color(255,255,255);
		drawContours(midImg3,contours,index,color,NULL,8,hierarchy);
	}
	namedWindow("【輪廓圖】",WINDOW_NORMAL);
	imshow("【輪廓圖】",midImg3);
	Mat midImg4=midImg3.clone();
	//建立包圍輪廓的矩形邊界
	for(int i=0;i<contours.size();i++)
	{
		//每個輪廓
		vector<Point> points=contours[i];
		//對給定的2D點集,尋找最小面積的包圍矩形
		RotatedRect box=minAreaRect(Mat(points));
		Point2f vertex[4];
		box.points(vertex);
		//繪製出最小面積的包圍矩形
		line(xianshi,vertex[0],vertex[1],Scalar(100,200,211),6,LINE_AA);
		line(xianshi,vertex[1],vertex[2],Scalar(100,200,211),6,LINE_AA);
		line(xianshi,vertex[2],vertex[3],Scalar(100,200,211),6,LINE_AA);
		line(xianshi,vertex[3],vertex[0],Scalar(100,200,211),6,LINE_AA);
		//繪製中心的游標
		Point s1,l,r,u,d;
		s1.x=(vertex[0].x+vertex[2].x)/2.0;
		s1.y=(vertex[0].y+vertex[2].y)/2.0;
		l.x=s1.x-10;
		l.y=s1.y;

		r.x=s1.x+10;
		r.y=s1.y;

		u.x=s1.x;
		u.y=s1.y-10;

		d.x=s1.x;
		d.y=s1.y+10;
		line(xianshi,l,r,Scalar(100,200,211),2,LINE_AA);
		line(xianshi,u,d,Scalar(100,200,211),2,LINE_AA);
	}
	namedWindow("【繪製的最小面積矩形】",WINDOW_NORMAL);
	imshow("【繪製的最小面積矩形】",xianshi);
	waitKey(0);
	return 0;
}