Created
March 20, 2014 04:55
-
-
Save debanjum/9657505 to your computer and use it in GitHub Desktop.
Thresholding, Morphological Operations using Trackbars
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
| // Trackbar Thresholding & Morphological Operations | |
| // Set lower, upper H,S,V Threshold Values such that ROI in white in each of the 6 cases. | |
| // Perform Mophological Operation on Thresholded Image(Removes Noise) | |
| // Hence Final Image Obtained With Given HSV Values, for given environmental conditions. | |
| #include "stdafx.h" | |
| #include "cv.h" | |
| #include "highgui.h" | |
| //Initialsing Trackbar Values | |
| int filterIntH = 0; //Hue | |
| int filterIntS = 0; //Sat | |
| int filterIntV = 0; //Val | |
| int filterIntM = 0; //Morph | |
| int filterIntA1= 0; //LowerAreaLimit | |
| int filterIntA2= 0;//UpperAreaLimit | |
| //Initialising Trackbar Default Value | |
| int g_switch_value = 0; | |
| //Initialising H,S,V Lower Bounds | |
| int H1 = 0; | |
| int S1 = 0; | |
| int V1 = 0; | |
| //Initialising H,S,V Upper Bounds | |
| int H2 = 0; | |
| int S2 = 0; | |
| int V2 = 0; | |
| //Intialising MorphOperationCount, AreaOpen Size Upper,Lower Bound | |
| int M = 0; | |
| //Function Prototypes | |
| void switch_callbackH( int positionH ); | |
| void switch_callbackS( int positionS ); | |
| void switch_callbackV( int positionV ); | |
| void switch_callbackM( int positionM ); | |
| void switch_callbackA1( int positionA1 ); | |
| void switch_callbackA2( int positionA2); | |
| int cvBwAreaOpen(IplImage *image, int Lsize, long double Usize = 100000); | |
| int main() | |
| { | |
| //Initialising Data Structures | |
| int i=0; | |
| const char* name0 = "Original"; | |
| const char* name1 = "Thresholding"; | |
| const char* name2 = "Morphological Operations"; | |
| const char* name3 = "Processed Image"; | |
| const char* name4 = "VideoTesting"; | |
| CvCapture* Cap = cvCreateCameraCapture(0); | |
| IplImage* img; | |
| while(i++<15) | |
| img = cvQueryFrame(Cap); | |
| cvNamedWindow(name0,1); | |
| cvShowImage("Original",img); | |
| //IplImage* img = cvLoadImage( "C:\\Users\\Debanjum\\Documents\\Dell WebCam Central\\Snap Photos\\abcd3.jpg" ); | |
| IplImage* hsv = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3 ); | |
| IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 ); | |
| //Convert to HSV | |
| cvCvtColor(img,hsv,CV_BGR2HSV); | |
| //Original Image in Grayscale | |
| cvNamedWindow( name1, 1 ); | |
| cvShowImage(name1, img); | |
| // Creating Thresholding Trackbars | |
| cvCreateTrackbar( "Hue", name1, &g_switch_value, 255, switch_callbackH ); | |
| cvCreateTrackbar( "Saturation", name1, &g_switch_value, 255, switch_callbackS ); | |
| cvCreateTrackbar( "Value", name1, &g_switch_value, 255, switch_callbackV ); | |
| while( 1 ) | |
| { | |
| cvInRangeS(hsv,cvScalar(filterIntH,0,0,0),cvScalar(255,255,255,0),out); | |
| cvShowImage(name1, out); | |
| if( cvWaitKey( 15 ) == 27 ) | |
| { | |
| H1 = filterIntH; | |
| break; | |
| } | |
| } | |
| while( 1 ) | |
| { | |
| cvInRangeS(hsv,cvScalar(0,0,0,0),cvScalar(filterIntH,255,255,0),out); | |
| cvShowImage(name1, out); | |
| if( cvWaitKey( 15 ) == 27 ) | |
| { | |
| H2 = filterIntH; | |
| break; | |
| } | |
| } | |
| while( 1 ) | |
| { | |
| cvInRangeS(hsv,cvScalar(0,filterIntS,0,0),cvScalar(255,255,255,0),out); | |
| cvShowImage(name1, out); | |
| if( cvWaitKey( 15 ) == 27 ) | |
| { | |
| S1 = filterIntS; | |
| break; | |
| } | |
| } | |
| while( 1 ) | |
| { | |
| cvInRangeS(hsv,cvScalar(0,0,0,0),cvScalar(255,filterIntS,255,0),out); | |
| cvShowImage(name1, out); | |
| if( cvWaitKey( 15 ) == 27 ) | |
| { | |
| S2 = filterIntS; | |
| break; | |
| } | |
| } | |
| while( 1 ) | |
| { | |
| cvInRangeS(hsv,cvScalar(0,0,filterIntV,0),cvScalar(255,255,255,0),out); | |
| cvShowImage(name1, out); | |
| if( cvWaitKey( 15 ) == 27 ) | |
| { | |
| V1 = filterIntV; | |
| break; | |
| } | |
| } | |
| while( 1 ) | |
| { | |
| cvInRangeS(hsv,cvScalar(0,0,0,0),cvScalar(255,255,filterIntV,0),out); | |
| cvShowImage(name1, out); | |
| if( cvWaitKey( 15 ) == 27 ) | |
| { | |
| V2 = filterIntV; | |
| break; | |
| } | |
| } | |
| //Destroying Thresholding Window | |
| cvDestroyWindow( name1 ); | |
| //Creating Morpholgical Operations Window & Trackbar | |
| g_switch_value = 0; | |
| cvNamedWindow( name2, 1 ); | |
| cvCreateTrackbar( "Morph", name2, &g_switch_value, 15, switch_callbackM); | |
| cvCreateTrackbar( "Area:LowerLimit", name2, &g_switch_value, 15024, switch_callbackA1); | |
| //Displaying Thresholded Image in Morphological Operations Window | |
| cvInRangeS(hsv,cvScalar(H1,S1,V1,0),cvScalar(H2,S2,V2,0),out); | |
| cvShowImage(name2, out); | |
| while(1) | |
| { | |
| cvInRangeS(hsv,cvScalar(H1,S1,V1,0),cvScalar(H2,S2,V2,0),out); | |
| cvMorphologyEx(out,out,0,0,CV_MOP_OPEN,filterIntM); | |
| cvBwAreaOpen(out,filterIntA1); | |
| cvShowImage(name2, out); | |
| if( cvWaitKey(15) == 27 ) | |
| break; | |
| } | |
| //Printing H,S,V Uppper & Lower Bounding Values | |
| printf("\nHue:[%d , %d]\nSat:[%d , %d]\nVal:[%d , %d]\n", H1, H2, S1, S2, V1, V2); | |
| //Printing Morphological Opening, Area Filter Uppper & Lower Bounding Values | |
| printf("\nMorph:\t%d\nArea: [%d , 100000]\n", filterIntM, filterIntA1); | |
| //Releasing Original, Processing Images | |
| cvDestroyWindow( name2 ); | |
| cvDestroyWindow( name0 ); | |
| //Showing Final Processed Image | |
| // cvCanny(out,out,10,100,3); | |
| cvNamedWindow(name3, 1); | |
| cvShowImage(name3, out); | |
| //Releasing Processed Image | |
| if (cvWaitKey(0) == 27) | |
| cvDestroyWindow( name3 ); | |
| //Video Testing | |
| while(1) | |
| { | |
| img = cvQueryFrame(Cap); | |
| cvInRangeS(img,cvScalar(H1,S1,V1,0),cvScalar(H2,S2,V2,0),out); | |
| cvBwAreaOpen(out,filterIntA1); | |
| cvMorphologyEx(out,out,0,0,CV_MOP_OPEN,filterIntM); | |
| cvNamedWindow(name4,1); | |
| cvShowImage(name4,out); | |
| if( cvWaitKey(15) == 27) | |
| break; | |
| } | |
| //Final Clean Up | |
| if( cvWaitKey(0) == 27) | |
| { | |
| cvReleaseImage( &img ); | |
| cvReleaseImage( &out ); | |
| cvDestroyWindow(name4); | |
| } | |
| return 0; | |
| } | |
| int cvBwAreaOpen(IplImage *image, int Lsize, long double Usize) | |
| {/* | |
| OpenCV equivalent of Matlab's bwareaopen. | |
| Image must be 8 bits, 1 channel, Black&White(objects) with values 0 and 255 respectively | |
| */ | |
| CvMemStorage *storage; | |
| CvSeq *contour = NULL; | |
| CvScalar white, black; | |
| IplImage *input = NULL; // cvFindContours changes the input | |
| double area; | |
| int foundCountours = 0; | |
| black = CV_RGB( 0, 0, 0 ); | |
| white = CV_RGB( 255, 255, 255 ); | |
| if(image == NULL) | |
| return(foundCountours); | |
| input = cvCloneImage(image); | |
| storage = cvCreateMemStorage(0); | |
| cvFindContours(input, storage, &contour, sizeof (CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0)); | |
| while(contour) | |
| { | |
| area = cvContourArea(contour, CV_WHOLE_SEQ ); | |
| if( area < Lsize || area > Usize) | |
| { | |
| // removes white dots | |
| cvDrawContours( image, contour, black, black, -1, CV_FILLED, 8 ); | |
| } | |
| else | |
| { | |
| if( area >= Lsize && area <= Usize) // fills in black holes | |
| cvDrawContours( image, contour, white, white, -1, CV_FILLED, 8 ); | |
| } | |
| contour = contour->h_next; | |
| } | |
| cvReleaseMemStorage( &storage ); // deallocate CvSeq as well. | |
| cvReleaseImage(&input); | |
| return(foundCountours); | |
| } | |
| void switch_callbackH( int positionH ) | |
| { | |
| filterIntH = positionH; | |
| } | |
| void switch_callbackS( int positionS ) | |
| { | |
| filterIntS = positionS; | |
| } | |
| void switch_callbackV( int positionV ) | |
| { | |
| filterIntV = positionV; | |
| } | |
| void switch_callbackM( int positionM ) | |
| { | |
| filterIntM = positionM; | |
| } | |
| void switch_callbackA1( int positionA1 ) | |
| { | |
| filterIntA1 = positionA1; | |
| } | |
| void switch_callbackA2( int positionA2 ) | |
| { | |
| filterIntA2 = positionA2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment