Color-based object detection using Hue and Saturation thresholding

翻译 2016年06月01日 10:33:23

函数原型: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*/



#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;
}

相关文章推荐

Region-based Convolutional Networks for Accurate Object Detection and Segmentation----R-CNN论文笔记

一、为什么提出R-CNN目标检测性能停滞不前,性能最好的集成方法又太复杂,所以作者提出了一个既能大幅提升性能,又更简单的R-CNN。二、R-CNN的框架上面的框架图清晰的给出了R-CNN的目标检测流程...

[深度学习论文笔记][Object Localization] OverFeat: Integrated Recognition, Localization and Detection using C

Sermanet, Pierre, et al. “Overfeat: Integrated recognition, localization and detection using convolu...

Learning color and locality cues for moving object detection and segmentation方法解析

这篇文章来自2009年的CVPR会议论文,其提出了一种从单眼视频中自动检测和分割移动物体的算法。算法相比于之前存在的其他算法的优势在于,在目标的移动是部分位置移动并且是部分帧存在移动的情况下,算法仍然...

论文阅读理解 - R-FCN: Object Detection via Region-based Fully Convolutional Networks

R-FCN: Object Detection via Region-based Fully Convolutional Networks Paper Code

An Introduction to CNN based Object Detection

An Introduction to CNN based Object Detection
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Color-based object detection using Hue and Saturation thresholding
举报原因:
原因补充:

(最多只允许输入30个字)