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

一、简介

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

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

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

二、HSV滑动条

 #include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std; Mat srcImg, hsv_img;
int h_min =,s_min = ,v_min = ;
int h_max = ,s_max = ,v_max = ; void onChange(int, void* param) {
Scalar hsv_min(h_min, s_min, v_min);
Scalar hsv_max(h_max, s_max, v_max);
Mat dst = Mat::zeros(srcImg.size(), srcImg.type());
inRange(srcImg, hsv_min, hsv_max, dst);
imshow("HSV", dst);
} void main()
{
srcImg = imread("E://duck2.jpg");
imshow("src", srcImg);
cvtColor(srcImg, hsv_img, CV_BGR2HSV); //BGR转到HSV颜色空间
namedWindow("HSV", CV_WINDOW_AUTOSIZE);
//创建滚动条
createTrackbar("h_min", "HSV", &h_min, , onChange, );
createTrackbar("s_min", "HSV", &s_min, , onChange, );
createTrackbar("v_min", "HSV", &v_min, , onChange, );
createTrackbar("h_max", "HSV", &h_max, , onChange, );
createTrackbar("s_max", "HSV", &s_max, , onChange, );
createTrackbar("v_max", "HSV", &v_max, , onChange, );
//回调函数初始化
onChange(h_min, );
onChange(s_min, );
onChange(v_min, );
onChange(h_max, );
onChange(s_max, );
onChange(v_max, ); waitKey();
}

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

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

三、颜色识别跟踪

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.
 #include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std; ///green hsv min value
int h_min = ;
int s_min = ;
int v_min = ;
///green hsv max value
int h_max = ;
int s_max = ;
int v_max = ; void main()
{
//识别图片中颜色物体
Mat srcImg = imread("E://rgb.jpg");
imshow("src", srcImg);
Mat dstImg = srcImg.clone();
Mat hsv_img; //存储HSV图像
cvtColor(srcImg,hsv_img,CV_BGR2HSV); Scalar hsv_min(h_min,s_min,v_min);
Scalar hsv_max(h_max, s_max, v_max);
Mat hsv_green=Mat::zeros(srcImg.size(),CV_8U);
inRange(hsv_img, hsv_min, hsv_max, hsv_green);
medianBlur(hsv_green, hsv_green, );//中值滤波
imshow("hsv_green", hsv_green); //找轮廓
vector<vector<Point>>contours;
vector<Vec4i>hierarcy;
//找外层轮廓
findContours(hsv_green, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<Rect>boundRect(contours.size());
//遍历每个轮廓
for (int i = ; i < contours.size(); i++)
{
boundRect[i] = boundingRect(Mat(contours[i]));//计算外接矩形
//top、left、right、bottom tl左上 br右下
rectangle(dstImg,boundRect[i].tl(), boundRect[i].br(),Scalar(,,),,);
//Point org = boundRect[i].tl();
Point org = boundRect[i].br();
putText(dstImg,"green",org,CV_FONT_HERSHEY_SIMPLEX,1.2f,CV_RGB(,,),,);
}
imshow("result", dstImg); waitKey();
}

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

视频颜色跟踪

 #include "opencv2/opencv.hpp"
using namespace cv; //设置HSV颜色区间
int blue_min_h = ;
int blue_min_s = ;
int blue_min_v = ;
int blue_max_h = ;
int blue_max_s = ;
int blue_max_v = ; int green_min_h = ;
int green_min_s = ;
int green_min_v = ;
int green_max_h = ;
int green_max_s = ;
int green_max_v = ; int red_min_h = ;
int red_min_s = ;
int red_min_v = ;
int red_max_h = ;
int red_max_s = ;
int red_max_v = ; void main()
{
VideoCapture cap;
cap.open("E://1.mp4");
if (!cap.isOpened())//如果视频不能正常打开则返回
return;
Mat src,dst,hsv,ROI; while ()
{
cap >> src;
if (src.empty())//如果某帧为空则退出循环
break;
//imshow("video", src);
dst = src.clone();
ROI=src(Rect(,,,));//x,y,w,h xy坐标,宽度,高度 区分蓝色按钮和右边的蓝色区域
GaussianBlur(ROI,ROI,Size(,),);
cvtColor(ROI, hsv, CV_BGR2HSV);
Scalar blue_min(blue_min_h, blue_min_s, blue_min_v);
Scalar blue_max(blue_max_h, blue_max_s, blue_max_v);
Scalar green_min(green_min_h, green_min_s, green_min_v);
Scalar green_max(green_max_h, green_max_s, green_max_v);
Scalar red_min(red_min_h, red_min_s, red_min_v);
Scalar red_max(red_max_h, red_max_s, red_max_v);
Mat hsv_blue = Mat::zeros(src.size(), CV_8U);
Mat hsv_green = Mat::zeros(src.size(), CV_8U);
Mat hsv_red = Mat::zeros(src.size(), CV_8U);
inRange(hsv, blue_min, blue_max, hsv_blue);//颜色区间范围筛选
inRange(hsv, green_min, green_max, hsv_green);
inRange(hsv, red_min, red_max, hsv_red);
medianBlur(hsv_blue, hsv_blue, );//中值滤波
medianBlur(hsv_green, hsv_green, );
medianBlur(hsv_red, hsv_red, ); //找轮廓
vector<vector<Point>>contours_blue,contours_green,contours_red;
vector<Vec4i>hierarchy_blue,hierarchy_green,hierarchy_red;
//蓝色轮廓
findContours(hsv_blue, contours_blue, hierarchy_blue, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<Rect>boundRect_blue(contours_blue.size());//定义外接矩形集合
for (int i = ; i < contours_blue.size(); i++)
{
boundRect_blue[i] = boundingRect(Mat(contours_blue[i]));//计算外接矩形
rectangle(dst, boundRect_blue[i].tl(), boundRect_blue[i].br(), Scalar(, , ), , );
Point org = boundRect_blue[i].br();
putText(dst, "blue", org, CV_FONT_HERSHEY_SIMPLEX, 1.2f, CV_RGB(,,), , );
}
//绿色轮廓
findContours(hsv_green, contours_green, hierarchy_green, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<Rect>boundRect_green(contours_green.size());//定义外接矩形集合
for (int i = ; i < contours_green.size(); i++)
{
boundRect_green[i] = boundingRect(Mat(contours_green[i]));//计算外接矩形
rectangle(dst, boundRect_green[i].tl(), boundRect_green[i].br(), Scalar(, , ), , );
Point org = boundRect_green[i].br();
putText(dst, "green", org, CV_FONT_HERSHEY_SIMPLEX, 1.2f, CV_RGB(, , ), , );
}
//红色轮廓
findContours(hsv_red, contours_red, hierarchy_red, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<Rect>boundRect_red(contours_red.size());//定义外接矩形集合
for (int i = ; i < contours_red.size(); i++)
{
boundRect_red[i] = boundingRect(Mat(contours_red[i]));//计算外接矩形
rectangle(dst, boundRect_red[i].tl(), boundRect_red[i].br(), Scalar(, , ), , );
Point org = boundRect_red[i].br();
putText(dst, "red", org, CV_FONT_HERSHEY_SIMPLEX, 1.2f, CV_RGB(,,), , );
} imshow("result", dst);
waitKey();//每帧延时20毫秒
}
cap.release();//释放资源
}

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

上一篇:git rebase命令


下一篇:git rebase