c语⾔opencv识别颜⾊并标记,opencv如何识别图⽚上带颜⾊的
圆
本⽂实例为⼤家分享了opencv识别图⽚上带颜⾊的圆的具体代码,供⼤家参考,具体内容如下
识别带颜⾊的圆,⾸先需要先查询该颜⾊的HSV值,下图部分紫⾊归为红⾊了:
⽐如红⾊:
//红⾊的HSV值
intlow_H=0,low_S=123,low_V=100;
intHigh_H=5,High_S=255,High_V=255;
然后将图⽚从BGR转化成HSV,接着⼆值化:
cvtColor(image,src,COLOR_BGR2HSV);//从BGR->HSV
inRange(src,Scalar(low_H,low_S,low_V),Scalar(High_H,High_S,High_V),src);//⼆值化
如果有噪声可以去噪(这⼀步可以没有):
GaussianBlur(src,src,Size(5,3),2,2);
接着⽤HoughCircles来找圆,后⾯的参数可以根据需要⾃⼰设置:
HoughCircles(src,circles,CV_HOUGH_GRADIENT,1,50,15,7,10,30);
//找圆,最后两个参数是圆半径范围,20是最⼩圆半径,30是最⼤圆半径
HoughCircles介绍:
HoughCircles(image,method,dp,minDist[,circles[,param1[,param2[,minRadius[,maxRadius]]]]])
完整代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragmacomment(lib,"opencv_")
#pragmacomment(lib,"opencv_")
#pragmacomment(lib,"opencv_")
#pragmacomment(lib,"opencv_")
#pragmacomment(lib,"opencv_")
usingnamespacestd;
usingnamespacecv;
voiddrawCircle(Mat&input,constvector&circles);
intmain(){
Matimage,src;//加载进来的图⽚
vectorcircles;//识别出来的圆,每⼀⾏是⼀个圆,第⼀列是圆⼼的x坐标,第⼆列是圆⼼的y坐标,第三列是圆的半径
image=imread("D:/code/");
Matresult=imread("D:/code/");//结果图
//红⾊的HSV值
intlow_H=0,low_S=123,low_V=100;
intHigh_H=5,High_S=255,High_V=255;
cvtColor(image,src,COLOR_BGR2HSV);//从BGR->HSV
inRange(src,Scalar(low_H,low_S,low_V),Scalar(High_H,High_S,High_V),src);//⼆值化
//Reducethenoisoweavoidfalcircledetection
GaussianBlur(src,src,Size(5,3),2,2);
HoughCircles(src,circles,CV_HOUGH_GRADIENT,1,50,15,7,10,30);//找圆,最后两个参数是圆半径范围,20是最⼩圆半径,30
是最⼤圆半径
drawCircle(result,circles);//画圆
namedWindow("Displaywindow1",WINDOW_NORMAL);//展⽰结果
namedWindow("Displaywindow2",WINDOW_NORMAL);
resizeWindow("Displaywindow1",1240,680);
resizeWindow("Displaywindow2",1240,680);
imshow("Displaywindow1",image);
imshow("Displaywindow2",result);
waitKey(0);
return0;
}
voiddrawCircle(Mat&input,constvector&circles){
for(inti=0;i
Pointcenter(cvRound(circles[i][0]),cvRound(circles[i][1]));
intradius=cvRound(circles[i][2]);
circle(input,center,radius,Scalar(255,0,0),3,8,0);
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持脚本之家。
本文发布于:2023-01-04 07:26:36,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/89451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |