From 20dd7b70c0ed081c944c2a7bb5a67777ba20bc54 Mon Sep 17 00:00:00 2001 From: Ana Huaman Date: Tue, 5 Jul 2011 18:08:46 +0000 Subject: [PATCH] Added tutorial for BackProject in cpp (demo 2) - tutorial code --- .../calcBackProject_Demo2.cpp | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo2.cpp diff --git a/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo2.cpp b/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo2.cpp new file mode 100644 index 0000000000..172aeec0ad --- /dev/null +++ b/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo2.cpp @@ -0,0 +1,109 @@ +/** + * @file BackProject_Demo2.cpp + * @brief Sample code for backproject function usage ( a bit more elaborated ) + * @author OpenCV team + */ + +#include "opencv2/imgproc/imgproc.hpp" +#include "opencv2/highgui/highgui.hpp" + +#include + +using namespace cv; +using namespace std; + +/// Global Variables +Mat src; Mat hsv; Mat hue; +Mat mask; + +int lo = 20; int up = 20; +char* window_image = "Source image"; + +/// Function Headers +void Hist_and_Backproj( ); +void pickPoint (int event, int x, int y, int, void* ); + +/** + * @function main + */ +int main( int argc, char** argv ) +{ + /// Read the image + src = imread( argv[1], 1 ); + /// Transform it to HSV + cvtColor( src, hsv, CV_BGR2HSV ); + + /// Use only the Hue value + hue.create( hsv.size(), hsv.depth() ); + + int ch[] = { 0, 0 }; + mixChannels( &hsv, 1, &hue, 1, ch, 1 ); + + /// Show the image + namedWindow( window_image, CV_WINDOW_AUTOSIZE ); + imshow( window_image, src ); + + /// Set Trackbars for floodfill thresholds + createTrackbar( "Low thresh", window_image, &lo, 255, 0 ); + createTrackbar( "High thresh", window_image, &up, 255, 0 ); + /// Set a Mouse Callback + setMouseCallback( window_image, pickPoint, 0 ); + + waitKey(0); + return 0; +} + +/** + * @function pickPoint + */ +void pickPoint (int event, int x, int y, int, void* ) +{ + if( event != CV_EVENT_LBUTTONDOWN ) + { return; } + + // Fill and get the mask + Point seed = Point( x, y ); + + int newMaskVal = 255; + Scalar newVal = Scalar( 120, 120, 120 ); + + int connectivity = 8; + int flags = connectivity + (newMaskVal << 8 ) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY; + + Mat mask2 = Mat::zeros( src.rows + 2, src.cols + 2, CV_8UC1 ); + floodFill( src, mask2, seed, newVal, 0, Scalar( lo, lo, lo ), Scalar( up, up, up), flags ); + mask = mask2( Range( 1, mask2.rows - 1 ), Range( 1, mask2.cols - 1 ) ); + cout<<"rows: "<