Created
February 11, 2014 09:58
-
-
Save yoggy/8932091 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#if _DEBUG | |
#pragma comment(lib, "opencv_core248d.lib") | |
#pragma comment(lib, "opencv_imgproc248d.lib") | |
#pragma comment(lib, "opencv_highgui248d.lib") | |
#else | |
#pragma comment(lib, "opencv_core248.lib") | |
#pragma comment(lib, "opencv_imgproc248.lib") | |
#pragma comment(lib, "opencv_highgui248.lib") | |
#endif | |
// extract parameters | |
int h_min = 0; | |
int h_max = 30; | |
int s_min = 100; | |
int v_min = 100; | |
bool hsv_extract(const cv::Mat &src, cv::Mat &result, const int &h_min, const int &h_max, const int &s_min, const int &v_min) | |
{ | |
if (src.empty()) return false; | |
cv::Mat hsv_img; | |
cv::cvtColor(src, hsv_img, CV_BGR2HSV); | |
std::vector<cv::Mat> hsv_vec(3); | |
std::vector<cv::Mat> mask_vec(3); | |
cv::split(hsv_img, hsv_vec); | |
cv::Mat h_min_mask, h_max_mask; | |
if (h_min < h_max) { | |
cv::threshold(hsv_vec[0], h_min_mask, h_min, 255, CV_THRESH_BINARY); | |
cv::threshold(hsv_vec[0], h_max_mask, h_max, 255, CV_THRESH_BINARY_INV); | |
mask_vec[0] = h_min_mask & h_max_mask; | |
} | |
else { | |
cv::threshold(hsv_vec[0], h_min_mask, h_min, 255, CV_THRESH_BINARY); | |
cv::threshold(hsv_vec[0], h_max_mask, h_max, 255, CV_THRESH_BINARY_INV); | |
mask_vec[0] = h_min_mask | h_max_mask; | |
} | |
cv::threshold(hsv_vec[1], mask_vec[1], s_min, 255, CV_THRESH_BINARY); | |
cv::threshold(hsv_vec[2], mask_vec[2], v_min, 255, CV_THRESH_BINARY); | |
result = mask_vec[0] & mask_vec[1] & mask_vec[2]; | |
return true; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
cv::VideoCapture capture; | |
capture.open(0); | |
cv::namedWindow("GUI"); | |
cv::createTrackbar("h_min", "GUI", &h_min, 180); | |
cv::createTrackbar("h_max", "GUI", &h_max, 180); | |
cv::createTrackbar("s_min", "GUI", &s_min, 255); | |
cv::createTrackbar("v_min", "GUI", &v_min, 255); | |
while(true) { | |
cv::Mat capture_img, result_img; | |
capture >> capture_img; | |
hsv_extract(capture_img, result_img, h_min, h_max, s_min, v_min); | |
cv::imshow("capture_img", capture_img); | |
cv::imshow("result_img", result_img); | |
int c = cv::waitKey(1); | |
if (c == 27) break; | |
} | |
cv::destroyAllWindows(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment