opencv学习之路(29)、轮廓查找与绘制(八)——轮廓特征属性及应用

一、简介

HSV颜色空间(hue色调,saturation饱和度,value亮度)

二、HSV滑动条

 1 #include "opencv2/opencv.hpp"
 2 #include <iostream>
 3 using namespace cv;
 4 using namespace std;
 5 
 6 Mat srcImg, hsv_img;
 7 int h_min =0,s_min = 0,v_min = 0;
 8 int h_max = 180,s_max = 255,v_max = 46;
 9 
10 void onChange(int, void* param) {
11     Scalar hsv_min(h_min, s_min, v_min);
12     Scalar hsv_max(h_max, s_max, v_max);
13     Mat dst = Mat::zeros(srcImg.size(), srcImg.type());
14     inRange(srcImg, hsv_min, hsv_max, dst);
15     imshow("HSV", dst);
16 }
17 
18 
19 void main()
20 {
21     srcImg = imread("E://duck2.jpg");
22     imshow("src", srcImg);
23     cvtColor(srcImg, hsv_img, CV_BGR2HSV); //BGR转到HSV颜色空间
24     namedWindow("HSV", CV_WINDOW_AUTOSIZE); 
25     //创建滚动条
26     createTrackbar("h_min", "HSV", &h_min, 180, onChange, 0);
27     createTrackbar("s_min", "HSV", &s_min, 255, onChange, 0);
28     createTrackbar("v_min", "HSV", &v_min, 255, onChange, 0);
29     createTrackbar("h_max", "HSV", &h_max, 180, onChange, 0);
30     createTrackbar("s_max", "HSV", &s_max, 255, onChange, 0);
31     createTrackbar("v_max", "HSV", &v_max, 255, onChange, 0);
32     //回调函数初始化
33     onChange(h_min, 0);
34     onChange(s_min, 0);
35     onChange(v_min, 0);
36     onChange(h_max, 0);
37     onChange(s_max, 0);
38     onChange(v_max, 0);
39 
40     waitKey(0);
41 }

三、颜色识别跟踪

putText函数定义

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_SIMPLEXFONT_HERSHEY_PLAINFONT_HERSHEY_DUPLEXFONT_HERSHEY_COMPLEXFONT_HERSHEY_TRIPLEXFONT_HERSHEY_COMPLEX_SMALLFONT_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 #include "opencv2/opencv.hpp"
 2 #include <iostream>
 3 using namespace cv;
 4 using namespace std;
 5 
 6 ///green hsv min value
 7 int h_min = 35;
 8 int s_min = 110;
 9 int v_min = 106;
10 ///green hsv max value
11 int h_max = 77;
12 int s_max = 255;
13 int v_max = 255;
14 
15 void main()
16 {
17     //识别图片中颜色物体
18     Mat srcImg = imread("E://rgb.jpg");
19     imshow("src", srcImg);
20     Mat dstImg = srcImg.clone();
21     Mat hsv_img;  //存储HSV图像
22     cvtColor(srcImg,hsv_img,CV_BGR2HSV);
23 
24     Scalar hsv_min(h_min,s_min,v_min);
25     Scalar hsv_max(h_max, s_max, v_max);
26     Mat hsv_green=Mat::zeros(srcImg.size(),CV_8U);
27     inRange(hsv_img, hsv_min, hsv_max, hsv_green);
28     medianBlur(hsv_green, hsv_green, 5);//中值滤波
29     imshow("hsv_green", hsv_green);
30 
31     //找轮廓
32     vector<vector<Point>>contours;
33     vector<Vec4i>hierarcy;
34     //找外层轮廓
35     findContours(hsv_green, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
36     vector<Rect>boundRect(contours.size());
37     //遍历每个轮廓
38     for (int i = 0; i < contours.size(); i++)
39     {
40         boundRect[i] = boundingRect(Mat(contours[i]));//计算外接矩形
41         //top、left、right、bottom    tl左上    br右下
42         rectangle(dstImg,boundRect[i].tl(), boundRect[i].br(),Scalar(0,255,255),2,8);
43         //Point org = boundRect[i].tl();
44         Point org = boundRect[i].br();
45         putText(dstImg,"green",org,CV_FONT_HERSHEY_SIMPLEX,1.2f,CV_RGB(0,255,0),2,8);
46     }
47     imshow("result", dstImg);
48 
49     waitKey(0);
50 }

视频颜色跟踪

 1 #include "opencv2/opencv.hpp"
 2 using namespace cv;
 3 
 4 //设置HSV颜色区间
 5 int blue_min_h = 90;
 6 int blue_min_s = 100;
 7 int blue_min_v = 100;
 8 int blue_max_h = 120;
 9 int blue_max_s = 255;
10 int blue_max_v = 255;
11 
12 int green_min_h = 60;
13 int green_min_s = 100;
14 int green_min_v = 100;
15 int green_max_h = 75;
16 int green_max_s = 255;
17 int green_max_v = 255;
18 
19 int red_min_h = 0;
20 int red_min_s = 100;
21 int red_min_v = 100;
22 int red_max_h = 10;
23 int red_max_s = 255;
24 int red_max_v = 255;
25 
26 void main()
27 {
28     VideoCapture cap;
29     cap.open("E://1.mp4");
30     if (!cap.isOpened())//如果视频不能正常打开则返回
31         return;
32     Mat src,dst,hsv,ROI;
33 
34     while (1)
35     {
36         cap >> src;
37         if (src.empty())//如果某帧为空则退出循环
38             break;
39         //imshow("video", src);
40         dst = src.clone();
41         ROI=src(Rect(0,0,360,354));//x,y,w,h    xy坐标,宽度,高度        区分蓝色按钮和右边的蓝色区域
42         GaussianBlur(ROI,ROI,Size(15,15),0);
43         cvtColor(ROI, hsv, CV_BGR2HSV);
44         Scalar blue_min(blue_min_h, blue_min_s, blue_min_v);
45         Scalar blue_max(blue_max_h, blue_max_s, blue_max_v);
46         Scalar green_min(green_min_h, green_min_s, green_min_v);
47         Scalar green_max(green_max_h, green_max_s, green_max_v);
48         Scalar red_min(red_min_h, red_min_s, red_min_v);
49         Scalar red_max(red_max_h, red_max_s, red_max_v);
50         Mat hsv_blue = Mat::zeros(src.size(), CV_8U);
51         Mat hsv_green = Mat::zeros(src.size(), CV_8U);
52         Mat hsv_red = Mat::zeros(src.size(), CV_8U);
53         inRange(hsv, blue_min, blue_max, hsv_blue);//颜色区间范围筛选
54         inRange(hsv, green_min, green_max, hsv_green);
55         inRange(hsv, red_min, red_max, hsv_red);
56         medianBlur(hsv_blue, hsv_blue, 5);//中值滤波
57         medianBlur(hsv_green, hsv_green, 5);
58         medianBlur(hsv_red, hsv_red, 5);
59 
60         //找轮廓
61         vector<vector<Point>>contours_blue,contours_green,contours_red;
62         vector<Vec4i>hierarchy_blue,hierarchy_green,hierarchy_red;
63         //蓝色轮廓
64         findContours(hsv_blue, contours_blue, hierarchy_blue, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
65         vector<Rect>boundRect_blue(contours_blue.size());//定义外接矩形集合
66         for (int i = 0; i < contours_blue.size(); i++)
67         {
68             boundRect_blue[i] = boundingRect(Mat(contours_blue[i]));//计算外接矩形
69             rectangle(dst, boundRect_blue[i].tl(), boundRect_blue[i].br(), Scalar(0, 255, 255), 2, 8);
70             Point org = boundRect_blue[i].br();
71             putText(dst, "blue", org, CV_FONT_HERSHEY_SIMPLEX, 1.2f, CV_RGB(0,0,255), 2, 8);
72         }
73         //绿色轮廓
74         findContours(hsv_green, contours_green, hierarchy_green, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
75         vector<Rect>boundRect_green(contours_green.size());//定义外接矩形集合
76         for (int i = 0; i < contours_green.size(); i++)
77         {
78             boundRect_green[i] = boundingRect(Mat(contours_green[i]));//计算外接矩形
79             rectangle(dst, boundRect_green[i].tl(), boundRect_green[i].br(), Scalar(0, 255, 255), 2, 8);
80             Point org = boundRect_green[i].br();
81             putText(dst, "green", org, CV_FONT_HERSHEY_SIMPLEX, 1.2f, CV_RGB(0, 255, 0), 2, 8);
82         }
83         //红色轮廓
84         findContours(hsv_red, contours_red, hierarchy_red, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
85         vector<Rect>boundRect_red(contours_red.size());//定义外接矩形集合
86         for (int i = 0; i < contours_red.size(); i++)
87         {
88             boundRect_red[i] = boundingRect(Mat(contours_red[i]));//计算外接矩形
89             rectangle(dst, boundRect_red[i].tl(), boundRect_red[i].br(), Scalar(0, 255, 255), 2, 8);
90             Point org = boundRect_red[i].br();
91             putText(dst, "red", org, CV_FONT_HERSHEY_SIMPLEX, 1.2f, CV_RGB(255,0,0), 2, 8);
92         }
93 
94         imshow("result", dst);
95         waitKey(20);//每帧延时20毫秒
96     }
97     cap.release();//释放资源
98 }

 

posted @ 2017-10-05 20:29  进击的小猴子  阅读(3110)  评论(0编辑  收藏  举报