ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

方块判别

2021-04-12 22:31:46  阅读:127  来源: 互联网

标签:判别 color Image boundRect contours0 && 方块 size


实现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;
}

`

后面想到什么再补充吧

标签:判别,color,Image,boundRect,contours0,&&,方块,size
来源: https://blog.csdn.net/weixin_43427241/article/details/115644657

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有