方块判别

实现OpenCV 识别第一课参考代码后作业

  1. 转HSV图像(这里自己一个个试的阈值)
  2. 腐蚀
  3. 二值化
  4. 找边框

`

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;
using namespace std;

Mat src,Image, dstImage1, dstImage2,dst,mask,con,masktemp;
String Path = "./2.jpg";
vector<Vec4i> hierarchy;
int main()
{
	int niters = 6;int vmin = 126, vmax = 256, smin = 138;
	src = imread ("../1.png",1);
//imshow("yuantu",Image);

	GaussianBlur(src,Image,Size(5,5),1.5);

	cvtColor(Image, dstImage1, COLOR_RGB2HSV);//这个是HSV的
//cvtColor(Image, dstImage1, COLOR_BGR2GRAY);
//imshow("11",dstImage1);
//dilate(dstImage1, dst, Mat(), Point(-1,-1),1);
	erode(dstImage1,dst,Mat(),Point(-1,-1),niters);
//dilate(dst,dst,Mat(),Point(-1,-1),1);
	inRange(dst, Scalar(0, smin, MIN(vmin,vmax)),
                    Scalar(360, 255, MAX(vmin, vmax)), mask);//HSV

//threshold(dst,mask,25,255,THRESH_BINARY);

	Mat kernel = getStructuringElement(MORPH_RECT, Size(5,5), Point(-1, -1));
	morphologyEx( mask, masktemp,MORPH_CLOSE,  kernel ,Point(-1,-1),3);
	imshow("1",mask);
	vector<vector<Point> > contours0;
	findContours( masktemp, contours0, RETR_TREE, CHAIN_APPROX_SIMPLE);
	int idx = 0;

	vector<vector<Point> > contours0_poly( contours0.size() );
 	vector<Rect> boundRect( contours0.size() );
 	vector<RotatedRect> r( contours0.size() );
//for( ; idx >= 0; idx = hierarchy[idx][0] )
	//{
	vector<Moments> mu(contours0.size());
	for (int i = 0; i < contours0.size(); i++)
	{
		mu[i] = moments(contours0[i], false);
	}
///  计算中心矩:
	vector<Point2f> mc(contours0.size());
	for (int i = 0; i < contours0.size(); i++)
	{
		mc[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
	}

	 for( size_t i = 0; i < contours0.size(); i++ )
 	{
 	  approxPolyDP( contours0[i], contours0_poly[i], 3, true );
 	  boundRect[i] = boundingRect( contours0_poly[i] );
   	Scalar color = Image.at<Vec3b>(mc[i]);
  	r[i] =minAreaRect(contours0_poly[i]);
   //rectangle( Image, boundRect[i].tl(), boundRect[i].br(), color, 2 );
   //rectangle( Image, {boundRect[i].x,boundRect[i].y}, {boundRect[i].x+boundRect[i].width,boundRect[i].y+boundRect[i].height}, color, 2 );
	Mat boxPoints2f,boxPointsCov;
	boxPoints(r[i], boxPoints2f);
	boxPoints2f.assignTo(boxPointsCov,CV_32S);
	polylines(Image, boxPointsCov, true,color,10);
	if (color[0]>50&&color[0]<80&&color[1]>0&&color[1]<100&&color[2]>150&&color[2]<220)
		putText(Image,"red",Point(mc[i]),FONT_HERSHEY_SIMPLEX,2,Scalar(0,0,255),4,8);
	if (color[0]>50&&color[0]<80&&color[1]>150&&color[1]<220&&color[2]>100&&color[2]<120)	
		putText(Image,"green",Point(mc[i]),FONT_HERSHEY_SIMPLEX,2,Scalar(0,255,0),4,8);
	if (color[0]>50&&color[0]<80&&color[1]>100&&color[1]<120&&color[2]>200&&color[2]<220)
		putText(Image,"orange",Point(mc[i]),FONT_HERSHEY_SIMPLEX,2,Scalar(0,105,255),4,8);
	if (color[0]>150&&color[0]<255&&color[1]>100&&color[1]<150&&color[2]>0&&color[2]<100)
		putText(Image,"bule",Point(mc[i]),FONT_HERSHEY_SIMPLEX,2,Scalar(255,0,0),4,8);
	
//Scalar color( rand()&188, rand()&188, rand()&188 );
  		//drawContours( Image, contours0, -1, color,  3 );
	circle(Image, mc[i], 4, Scalar(0,0,0), -1, 8, 0);
 }


   
/(Image, mc[i], 4, color, -1, 8, 0);


 
//imshow("HSV",dstImage1);
imshow("HSV2",dst);
//imshow("mask",mask);
    
    imshow("resoult",Image);
//imshow("con",contours0);
waitKey(0);
return 0;
}

`

后面想到什么再补充吧

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值