Color-based object detection using Hue and Saturation thresholding
2016-06-01 10:33
603 查看
函数原型:C++: int createTrackbar(const
string& trackbarname, const string& winname,
int* value, int count,
TrackbarCallback onChange=0, void* userdata=0)
//Color_based object detection using Hue and Saturation thresholding
/*1、Program to display a video from attached default camera device
and detect colored blobs using H and S thresholding.
2、Remove noise using opening and closing morphological operations*/
string& trackbarname, const string& winname,
int* value, int count,
TrackbarCallback onChange=0, void* userdata=0)
//Color_based object detection using Hue and Saturation thresholding
/*1、Program to display a video from attached default camera device
and detect colored blobs using H and S thresholding.
2、Remove noise using opening and closing morphological operations*/
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> using namespace std; using namespace cv; int hs_slider=0,low_slider=30,high_slider=100; int low_h=30,low_s=30,high_h=100,high_s=100; void on_hs_trackbar(int ,void *) { switch (hs_slider) { case 0: setTrackbarPos("Low threshold","Segmentation",low_h); setTrackbarPos("High threshold","Segmentation",high_h); break; case 1: setTrackbarPos("Low threshold","Segmentation",low_s); setTrackbarPos("High threshold","Segmentation",high_s); break; } } void on_low_thresh_trackbar(int ,void *) { switch(hs_slider) { case 0: low_h=min(high_slider-1,low_slider); setTrackbarPos("Low threshold","Segmentation",low_h); break; case 1: low_s=min(high_slider-1,low_slider); setTrackbarPos("Low threshold","Segmentation",low_s); break; } } void on_high_thresh_trackbar(int ,void *) { switch(hs_slider) { case 0: high_h=max(low_slider+1,high_slider); setTrackbarPos("High threshold","Segmentation",high_h); break; case 1: high_s=max(low_slider+1,high_slider); setTrackbarPos("High threshold","Segmentation",high_s); break; } } int main() { VideoCapture cap(0); if(!cap.isOpened())//check if the file was open properly { cout<<" failed to open Capture"<<endl; return -1; } namedWindow("Video"); namedWindow("Segmentation"); //creat trackbar createTrackbar("0.H\n1.S","Segmentation",&hs_slider,1,on_hs_trackbar); createTrackbar("Low threshold","Segmentation",&low_slider,255,on_low_thresh_trackbar); createTrackbar("High threshold","Segmentation",&high_slider,255,on_high_thresh_trackbar); // while (char(waitKey(1))!='q'&&cap.isOpened()) { Mat frame,frame_thresholded,frame_hsv; cap>>frame; cvtColor(frame,frame_hsv,CV_BGR2HSV); //Check if the video is over if (frame.empty()) { cout<<"Video over"<<endl; break; } //extract the Hue and Saturation channels int from_to[]={0,0,1,1}; Mat hs(frame_hsv.size(),CV_8UC2); mixChannels(&frame_hsv,1,&hs,1,from_to,2); //check the image for a specific range of H and S inRange(hs,Scalar(low_h,low_s),Scalar(high_h,high_s),frame_thresholded); //open and close to remove noise Mat str_el=getStructuringElement(MORPH_ELLIPSE,Size(7,7)); morphologyEx(frame_thresholded,frame_thresholded,MORPH_OPEN,str_el); morphologyEx(frame_thresholded,frame_thresholded,MORPH_CLOSE,str_el); imshow("Video",frame); imshow("Segmentation",frame_thresholded); } return 0; }
相关文章推荐
- PHP“Cannot use object of type stdClass as array”
- Objective-C Runtime method swizzling简介(翻译)
- Java基础知识:java.lang包-Object、String、StringBuffer和包装类等
- import json后,报错:AttributeError: 'module' object has no attribute 'dumps,原因分析及解决方法
- OpenGL中的Frame Buffer Object 理解
- String 转换成JSONObject
- Objective-C 编程全解-第03章 类和继承
- JSONObject和JSONArray
- Android实现动画过程中遇到View.mViewFlags on null object refrence
- JsonObject生成Json字符串,并且解析为对象-----JsonReader解析Json字符串
- could not reserve enough space for object heap
- Object C中的数据类型表
- Objective-C Runtime基本概念(一)
- Object类
- 【论文笔记】Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
- Java语言中Object对象的hashCode()取值的底层算法是怎样实现的?
- Fake Objects
- iOS开发系列—Objective-C之内存管理
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- iOS objection 依赖注入