#include #include // Trackbar values int low[] = {50, 50, 50}; int high[] = {250, 250, 250}; int main(int argc, char* argv[]) { char* windowNames[] = { "band 0", "band 1", "band 2" }; printf("Hit ESC key to quit ...\n"); // Pick one of these two input sources; comment out the other line. // Note - the movie file goes into the directory with your source code. cv::VideoCapture cap(0); // open the default camera //cv::VideoCapture cap("131023-124754.avi"); // open a movie file if(!cap.isOpened()) {// check if we succeeded printf("error - can't open the input source\n"); system("PAUSE"); return -1; } double WIDTH = cap.get(CV_CAP_PROP_FRAME_WIDTH); double HEIGHT = cap.get(CV_CAP_PROP_FRAME_HEIGHT); printf("Image width=%f, height=%f\n", WIDTH, HEIGHT); // Create image windows. Meaning of flags: //CV_WINDOW_NORMAL enables manual resizing; CV_WINDOW_AUTOSIZE is automatic // You can "or" the above choice with CV_WINDOW_KEEPRATIO, which keeps aspect ratio cv::namedWindow("Input image", CV_WINDOW_AUTOSIZE); for (int i=0; i<3; i++) cv::namedWindow(windowNames[i], CV_WINDOW_AUTOSIZE); // Create trackbars for (int i=0; i<3; i++) { cv::createTrackbar( "low", windowNames[i], &low[i], 255, NULL ); cv::createTrackbar( "high", windowNames[i], &high[i], 255, NULL ); } // Run an infinite loop until user hits the ESC key while (1){ cv::Mat imgInput; cap >> imgInput;// get image from camera if (imgInput.empty()) break; cv::imshow("Input image", imgInput); // Convert to HSV cv::Mat imgHSV; cv::cvtColor(imgInput, imgHSV, CV_BGR2HSV); // Split into planes cv::Mat planes[3]; split( imgHSV, planes ); // Threshold for (int i=0; i<3; i++){ cv::Mat imageThreshLow, imageThreshHigh; threshold(planes[i], imageThreshLow,// output thresholded image low[i],// value to use for threshold 255,// output value cv::THRESH_BINARY);// threshold_type threshold(planes[i], imageThreshHigh,// output thresholded image high[i],// value to use for threshold 255,// output value cv::THRESH_BINARY_INV);// threshold_type bitwise_and(imageThreshLow, imageThreshHigh, planes[i]); } // Show images in the windows for (int i=0; i<3; i++) cv::imshow(windowNames[i], planes[i]); // Finally, AND all the thresholded images together cv::Mat imgResult( cv::Size(WIDTH,HEIGHT), // size of image CV_8UC1, // type: CV_8UC1=8bit, unsigned, 1 channel cv::Scalar(255)); // initialize to this value for (int i=0; i<3; i++) bitwise_and(imgResult, planes[i], imgResult); // Clean up binary image using morphological operators cv::Mat structuringElmt(7,7,CV_8U,cv::Scalar(1)); morphologyEx(imgResult, imgResult, cv::MORPH_CLOSE, structuringElmt); cv::imshow("Binary result", imgResult); // wait for x ms (0 means wait until a keypress) if (cv::waitKey(33) == 27) break;// ESC is ascii 27 } return EXIT_SUCCESS; }