openCV学习笔记十六:颜色物体识别与跟踪

颜色空间转换——cvtColor()
颜色区间范围筛选——inRange()
void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像const CvArr* lower,//阈值下限 const CvArr* upper,//阈值上限 CvArr* dst//结果图像 )

// A code block
var foo = 'bar';

代码:1.滑动条HSV参数debug工具

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace std;
using namespace cv;

Mat srcImg;
 Mat hsvImg;
 Mat dstImg; 
 int h_min = 35, s_min = 43, v_min = 46;//初始化为绿色的hsv取值范围
 int h_max = 77, s_max = 255, v_max = 255;
 void onChange(int,void* )
 { 
  Scalar hsv_min = Scalar(h_min, s_min, v_min);
  Scalar hsv_max = Scalar(h_max, s_max, v_max); 
  inRange(hsvImg, hsv_min, hsv_max, dstImg);//属于这个范围的会变成白色,其他为黑,存放在dstImg中 
  imshow("HSV-Filter", dstImg);
 }
 int main()
 {
  srcImg = imread("bar.png", CV_LOAD_IMAGE_COLOR);
  cvtColor(srcImg, hsvImg, CV_BGR2HSV);
  dstImg = Mat::zeros(srcImg.size(), CV_8U);//dstImg需同srcImg相同大小,且类型为CV_8U
  namedWindow("HSV-Filter",CV_WINDOW_NORMAL); 
  createTrackbar("h_min", "HSV-Filter", &h_min, 180, onChange, &dstImg); 
  createTrackbar("h_max", "HSV-Filter", &h_max, 180, onChange, &dstImg);
  createTrackbar("s_min", "HSV-Filter", &s_min, 255, onChange, &dstImg);
  createTrackbar("s_max", "HSV-Filter", &s_max, 255, onChange, &dstImg); 
  createTrackbar("v_min", "HSV-Filter", &v_min, 255, onChange, &dstImg); 
  createTrackbar("v_max", "HSV-Filter", &v_max, 255, onChange, &dstImg);
  imshow("srcImg", srcImg); onChange(0,0);//使得一开始就显示"HSV-Filter"窗口 
  waitKey(0);
  return 0;
 }

运行结果如下:
openCV学习笔记十六:颜色物体识别与跟踪_第1张图片

// A code block
var foo = 'bar';

代码:2.摄像头中颜色识别与跟踪

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
VideoCapture video(0);
 Mat frame;
 //视频识别
 //double height = video.get(CV_CAP_PROP_FRAME_HEIGHT);//获得VideoCapture类中的信息
 //double width = video.get(CV_CAP_PROP_FRAME_WIDTH);
 //double count = video.get(CV_CAP_PROP_FRAME_COUNT);
 //double FPS = video.get(CV_CAP_PROP_FPS); 
 while (1)
 {
  video >> frame;  
  Mat hsvImg;
  cvtColor(frame, hsvImg, CV_BGR2HSV);
  int h_min = 35, s_min = 108, v_min = 140;//筛选出绿色区域的值,由debug工具调试得来  
  int h_max = 77, s_max = 255, v_max = 255;
  Scalar hsv_min = Scalar(h_min, s_min, v_min);
  Scalar hsv_max = Scalar(h_max, s_max, v_max);
  Mat dstImg = Mat::zeros(frame.size(), CV_8U);//dstImg需同frame相同大小,且类型为CV_8U 
  inRange(hsvImg, hsv_min, hsv_max, dstImg);//属于这个范围的会变成白色,其他为黑,存放在dstImg中 
  medianBlur(dstImg, dstImg, 5);//中值滤波去除噪声  
  imshow("dstImg", dstImg);//显示颜色删选结果  
  vector<vector<Point>> contours;
  findContours(dstImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); 
  vector<Rect> boundRect(contours.size());
  boundRect[0] = boundingRect(contours[0]);  
  rectangle(frame, boundRect[0], Scalar(0, 255, 0), 1, 8);
  putText(frame, "Green", Point(boundRect[0].x, boundRect[0].y), CV_FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0), 1, 8); 
  imshow("frame", frame); 
  waitKey(50);
 }
 return 0;
}

运行结果如下:
识别的比较糟糕

openCV学习笔记十六:颜色物体识别与跟踪_第2张图片

你可能感兴趣的:(OpenCV学习笔记)