基于opencv的物体定位

opencv是一个很强大的机器视觉库,利用它我们可以开发出丰富多彩的使用项目。近日,我在研究一个图中物体定位系统。本程序用的是OpenCV2.4.9,附带OpenCV3.0。

程序中的原图为我随手拍的一张图片

基于opencv的物体定位_第1张图片

图中有三个物体,都是蓝色的,我首先取原图的蓝色通道变为灰度图

基于opencv的物体定位_第2张图片

灰度图经过中值滤波后可以得到去噪后的图片

基于opencv的物体定位_第3张图片

根据原图的蓝色通道和红色通道的大概取值范围,我们可得到比较满意的二值图

基于opencv的物体定位_第4张图片

为了去掉物体中少量的黑色部分,我用闭运算

基于opencv的物体定位_第5张图片

然而,图中最上面的那个物体里面还有一块很大的黑色(目前我也不知道怎么去掉,如果有大神知道望告知~~)

接下来就是找出物体的轮廓

基于opencv的物体定位_第6张图片

最后找到能包围轮廓的最小矩形

基于opencv的物体定位_第7张图片

好了,占时就这么多了

下面是配套的程序

OpenCV2.4.9半根

#include
#include
#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 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(i);  //取第i行的首地址
		uchar* redData = redChannel.ptr(i);
		for (int j = 0; jBIN_DIV&&redData[j]> contours;
	vector 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 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
#include
#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 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(i);  //取第i行的首地址
		uchar* redData=redChannel.ptr(i);
		for(int j=0;jBIN_DIV&&redData[j]> contours;
	vector 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 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;
}


你可能感兴趣的:(opencv,opencv2.4.9,物体定位,opencv,c/c++,图像处理)