标签:dup ++ top osi 1.2 创建 ttext 滑动 绿色
1、HSV颜色空间(hue色调,saturation饱和度,value亮度)
HSV颜色空间与人眼所看色彩较接近, 故常用于颜色检测与识别。其中H(色调)、S(饱和度)、V(亮度)
H---不同的颜色(红色/绿色/蓝色)---范围: 0~360
S---颜色深浅(浅红/深红)---范围: 0.0~1.0
V---颜色亮暗(暗红/亮红)---范围: 0.0~1.0
OpenCV默认的HSV范围分别是: H: 0~180, S: 0~255, V: 0~255
2、相关知识点
颜色空间转换---cvtColor()
cvtColor(srcImg, hsv_img, CV_BGR2HSV);//转换到HSV颜色
颜色区间范围删选---inRange()
1 CV_EXPORTS_W void inRange(InputArray src, InputArray lowreb, InputArray upperb, OutputArray dst);
src: 输入原图或数组
lowerb: 低边界或者颜色阈值
upperb: 高边界或者颜色阈值
dst: 输出目标图像, 需要和原图一样的size并且类型需为CV_8U
示例:
1 Scalar hsv_min(h_min, s_min, v_min); 2 Scalar hsv_max(h_max, s_max, v_max); 3 Mat dstImg = Mat::zeros(srcImg.rows, srcImg.cols, CV_8U): 4 inRange(hsv_img, hsv_min, hsv_max, dstImg);
1 //HSV滑动条 2 #include "opencv2/opencv.hpp" 3 #include <iostream> 4 5 using namespace cv; 6 using namespace std; 7 8 Mat srcImg, hsv_img; 9 int h_min =0,s_min = 0,v_min = 0; 10 int h_max = 180,s_max = 255,v_max = 46; 11 12 void onChange(int, void* param) { 13 Scalar hsv_min(h_min, s_min, v_min); 14 Scalar hsv_max(h_max, s_max, v_max); 15 Mat dst = Mat::zeros(srcImg.size(), srcImg.type()); 16 inRange(srcImg, hsv_min, hsv_max, dst); 17 imshow("HSV", dst); 18 } 19 20 21 int main() 22 { 23 srcImg = imread("2.jpg"); 24 imshow("src", srcImg); 25 cvtColor(srcImg, hsv_img, CV_BGR2HSV); //BGR转到HSV颜色空间 26 namedWindow("HSV", CV_WINDOW_AUTOSIZE); 27 //创建滚动条 28 createTrackbar("h_min", "HSV", &h_min, 180, onChange, 0); 29 createTrackbar("s_min", "HSV", &s_min, 255, onChange, 0); 30 createTrackbar("v_min", "HSV", &v_min, 255, onChange, 0); 31 createTrackbar("h_max", "HSV", &h_max, 180, onChange, 0); 32 createTrackbar("s_max", "HSV", &s_max, 255, onChange, 0); 33 createTrackbar("v_max", "HSV", &v_max, 255, onChange, 0); 34 //回调函数初始化 35 onChange(h_min, 0); 36 onChange(s_min, 0); 37 onChange(v_min, 0); 38 onChange(h_max, 0); 39 onChange(s_max, 0); 40 onChange(v_max, 0); 41 42 waitKey(0); 43 return 0; 44 }
putText函数定义为
1 void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, intthickness=1,
int lineType=8, bool bottomLeftOrigin=false )
参数为
img – 图像矩阵
text – string型文字内容
org – 文字坐标,以左下角为原点
fontFace – 字体类型 (包括 FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX,
FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX,)
fontScale –字体大小
color – 字体颜色
thickness – 字体粗细
lineType – Line type. See the line for details.
bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
1 //颜色的识别跟踪 2 #include "opencv2/opencv.hpp" 3 #include <iostream> 4 5 using namespace cv; 6 using namespace std; 7 8 ///green hsv min value 9 int h_min = 35; 10 int s_min = 110; 11 int v_min = 106; 12 ///green hsv max value 13 int h_max = 77; 14 int s_max = 255; 15 int v_max = 255; 16 17 int main() 18 { 19 //识别图片中颜色物体 20 Mat srcImg = imread("rgb.jpg"); 21 imshow("src", srcImg); 22 Mat dstImg = srcImg.clone(); 23 Mat hsv_img; //存储HSV图像 24 cvtColor(srcImg,hsv_img,CV_BGR2HSV); 25 26 Scalar hsv_min(h_min,s_min,v_min); 27 Scalar hsv_max(h_max, s_max, v_max); 28 Mat hsv_green=Mat::zeros(srcImg.size(),CV_8U); 29 inRange(hsv_img, hsv_min, hsv_max, hsv_green); 30 medianBlur(hsv_green, hsv_green, 5);//中值滤波 31 imshow("hsv_green", hsv_green); 32 33 //找轮廓 34 vector<vector<Point>>contours; 35 vector<Vec4i>hierarcy; 36 //找外层轮廓 37 findContours(hsv_green, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); 38 vector<Rect>boundRect(contours.size()); 39 //遍历每个轮廓 40 for (int i = 0; i < contours.size(); i++) 41 { 42 boundRect[i] = boundingRect(Mat(contours[i]));//计算外接矩形 43 //top、left、right、bottom tl左上 br右下 44 rectangle(dstImg,boundRect[i].tl(), boundRect[i].br(),Scalar(0,255,255),2,8); 45 //Point org = boundRect[i].tl(); 46 Point org = boundRect[i].br(); 47 putText(dstImg,"green",org,CV_FONT_HERSHEY_SIMPLEX,1.2f,CV_RGB(0,255,0),2,8); 48 } 49 imshow("result", dstImg); 50 51 waitKey(0); 52 return 0; 53 }
30、【opencv入门】轮廓查找与绘制(8)——轮廓特征属性及应用
标签:dup ++ top osi 1.2 创建 ttext 滑动 绿色
原文地址:https://www.cnblogs.com/Long-w/p/9668962.html