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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM