Harris Corner Detector & Shi-Tomasi Corner Detector

Harris Corner

  1.         // Capture the current frame
  2.         cap >> frame;
  3.  
  4.         // Resize the frame
  5.         resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
  6.  
  7.         dst = Mat::zeros(frame.size(), CV_32FC1);
  8.  
  9.         // Convert to grayscale
  10.         cvtColor(frame, frameGray, COLOR_BGR2GRAY );
  11.  
  12.         // Detecting corners
  13.         cornerHarris(frameGray, dst, blockSize, apertureSize, k, BORDER_DEFAULT);
  14.  
  15.         // Normalizing
  16.         normalize(dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
  17.         convertScaleAbs(dst_norm, dst_norm_scaled);
  18.  
  19.         // Drawing a circle around corners
  20.         for(int j = 0; j < dst_norm.rows ; j++)
  21.         {
  22.             for(int i = 0; i < dst_norm.cols; i++)
  23.             {
  24.                 if((int)dst_norm.at<float>(j,i) > thresh)
  25.                 {
  26.                     circle(frame, Point(i, j), 8,  Scalar(0,255,0), 2, 8, 0);
  27.                 }
  28.             }
  29.         }

Good Features To Track (Shi-Tomasi)

  1.         // Capture the current frame
  2.         cap >> frame;
  3.  
  4.         // Resize the frame
  5.         resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
  6.  
  7.         // Convert to grayscale
  8.         cvtColor(frame, frameGray, COLOR_BGR2GRAY );
  9.  
  10.         // Initialize the parameters for Shi-Tomasi algorithm
  11.         vector<Point2f> corners;
  12.         double qualityThreshold = 0.02;
  13.         double minDist = 15;
  14.         int blockSize = 5;
  15.         bool useHarrisDetector = false;
  16.         double k = 0.07;
  17.  
  18.         // Clone the input frame
  19.         Mat frameCopy;
  20.         frameCopy = frame.clone();
  21.  
  22.         // Apply corner detection
  23.         goodFeaturesToTrack(frameGray, corners, numCorners, qualityThreshold, minDist, Mat(), blockSize, useHarrisDetector, k);
  24.  
  25.         // Parameters for the circles to display the corners
  26.         int radius = 8;      // radius of the cirles
  27.         int thickness = 2;   // thickness of the circles
  28.         int lineType = 8;
  29.  
  30.         // Draw the detected corners using circles
  31.         for(size_t i = 0; i < corners.size(); i++)
  32.         {
  33.             Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255));
  34.             circle(frameCopy, corners[i], radius, color, thickness, lineType, 0);
  35.         }

Tracking Specific Color / Tracking Object

Tracking Specific Color

  1.         // Define the range of "blue" color in HSV colorspace
  2.         Scalar lowerLimit = Scalar(60,100,100);
  3.         Scalar upperLimit = Scalar(180,255,255);
  4.  
  5.         // Threshold the HSV image to get only blue color
  6.         inRange(hsvImage, lowerLimit, upperLimit, mask);
  7.  
  8.         // Compute bitwise-AND of input image and mask
  9.         bitwise_and(frame, frame, outputImage, mask=mask);
  10.  
  11.         // Run median filter on the output to smoothen it
  12.         medianBlur(outputImage, outputImage, 5);

Tracking Object

  1.         if(trackingFlag)
  2.         {
  3.             // Check for all the values in 'hsvimage' that are within the specified range
  4.             // and put the result in 'mask'
  5.             inRange(hsvImage, Scalar(0, minSaturation, minValue), Scalar(180, 256, maxValue), mask);
  6.  
  7.             // Mix the specified channels
  8.             int channels[] = {0, 0};
  9.             hueImage.create(hsvImage.size(), hsvImage.depth());
  10.             mixChannels(&hsvImage, 1, &hueImage, 1, channels, 1);
  11.  
  12.             if(trackingFlag < 0)
  13.             {
  14.                 // Create images based on selected regions of interest
  15.                 Mat roi(hueImage, selectedRect), maskroi(mask, selectedRect);
  16.  
  17.                 // Compute the histogram and normalize it
  18.                 calcHist(&roi, 1, 0, maskroi, hist, 1, &histSize, &histRanges);
  19.                 normalize(hist, hist, 0, 255, CV_MINMAX);
  20.  
  21.                 trackingRect = selectedRect;
  22.                 trackingFlag = 1;
  23.             }
  24.  
  25.             // Compute the histogram back projection
  26.             calcBackProject(&hueImage, 1, 0, hist, backproj, &histRanges);
  27.             backproj &= mask;
  28.             RotatedRect rotatedTrackingRect = CamShift(backproj, trackingRect, TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1));
  29.  
  30.             // Check if the area of trackingRect is too small
  31.             if(trackingRect.area() <= 1)
  32.             {
  33.                 // Use an offset value to make sure the trackingRect has a minimum size
  34.                 int cols = backproj.cols, rows = backproj.rows;
  35.                 int offset = MIN(rows, cols) + 1;
  36.                 trackingRect = Rect(trackingRect.x - offset, trackingRect.y - offset, trackingRect.x + offset, trackingRect.y + offset) & Rect(0, 0, cols, rows);
  37.             }
  38.  
  39.             // Draw the ellipse on top of the image
  40.             ellipse(image, rotatedTrackingRect, Scalar(0,255,0), 3, CV_AA);
  41.         }

Show histogram & Equalize histogram & Lomography effect & Cartoonize effect

The Original

Show histogram

  1. void showHistoCallback(int state, void* userData)
  2. {
  3.     // Separate image in BRG
  4.     vector<Mat> bgr;
  5.     split( img, bgr );
  6.  
  7.     // Create the histogram for 256 bins
  8.     // The number of possibles values
  9.     int numbins= 256;
  10.  
  11.     /// Set the ranges ( for B,G,R) )
  12.     float range[] = { 0, 256 } ;
  13.     const float* histRange = { range };
  14.  
  15.     Mat b_hist, g_hist, r_hist;
  16.  
  17.     calcHist( &bgr[0], 1, 0, Mat(), b_hist, 1, &numbins, &histRange );
  18.     calcHist( &bgr[1], 1, 0, Mat(), g_hist, 1, &numbins, &histRange );
  19.     calcHist( &bgr[2], 1, 0, Mat(), r_hist, 1, &numbins, &histRange );
  20.  
  21.     // Draw the histogram
  22.     // We go to draw lines for each channel
  23.     int width= 512;
  24.     int height= 300;
  25.     // Create image with gray base
  26.     Mat histImage( height, width, CV_8UC3, Scalar(20,20,20) );
  27.  
  28.     // Normalize the histograms to height of image
  29.     normalize(b_hist, b_hist, 0, height, NORM_MINMAX );
  30.     normalize(g_hist, g_hist, 0, height, NORM_MINMAX );
  31.     normalize(r_hist, r_hist, 0, height, NORM_MINMAX );
  32.  
  33.     int binStep= cvRound((float)width/(float)numbins);
  34.     for( int i=1; i< numbins; i++)
  35.     {
  36.         line( histImage, 
  37.                 Point( binStep*(i-1), height-cvRound(b_hist.at<float>(i-1) ) ),
  38.                 Point( binStep*(i), height-cvRound(b_hist.at<float>(i) ) ),
  39.                 Scalar(255,0,0)
  40.             );
  41.         line( histImage, 
  42.                 Point( binStep*(i-1), height-cvRound(g_hist.at<float>(i-1) ) ),
  43.                 Point( binStep*(i), height-cvRound(g_hist.at<float>(i) ) ),
  44.                 Scalar(0,255,0)
  45.             );
  46.         line( histImage, 
  47.                 Point( binStep*(i-1), height-cvRound(r_hist.at<float>(i-1) ) ),
  48.                 Point( binStep*(i), height-cvRound(r_hist.at<float>(i) ) ),
  49.                 Scalar(0,0,255)
  50.             );
  51.     }
  52.  
  53.     imshow("Histogram", histImage);
  54.  
  55. }

Equalize histogram

  1. void equalizeCallback(int state, void* userData)
  2. {
  3.     Mat result;
  4.     // Convert BGR image to YCbCr
  5.     Mat ycrcb;
  6.     cvtColor( img, ycrcb, COLOR_BGR2YCrCb);
  7.  
  8.     // Split image into channels
  9.     vector<Mat> channels;
  10.     split( ycrcb, channels );
  11.  
  12.     // Equalize the Y channel only
  13.     equalizeHist( channels[0], channels[0] );
  14.  
  15.     // Merge the result channels
  16.     merge( channels, ycrcb );
  17.  
  18.     // Convert color ycrcb to BGR
  19.     cvtColor( ycrcb, result, COLOR_YCrCb2BGR );
  20.  
  21.     // Show image
  22.     imshow("Equalized", result);
  23. }

Lomography effect

  1. void lomoCallback(int state, void* userData)
  2. {
  3.     Mat result;
  4.  
  5.     const double E = std::exp(1.0);
  6.     // Create Lookup table for color curve effect
  7.     Mat lut(1, 256, CV_8UC1);
  8.     for (int i=0; i<256; i++)
  9.     {
  10.         float x= (float)i/256.0; 
  11.         lut.at<uchar>(i)= cvRound( 256 * (1/(1 + pow(E, -((x-0.5)/0.1)) )) );
  12.     }
  13.  
  14.     // Split the image channels and apply curve transform only to red channel
  15.     vector<Mat> bgr;
  16.     split(img, bgr);
  17.     LUT(bgr[2], lut, bgr[2]);
  18.     // merge result
  19.     merge(bgr, result);
  20.  
  21.     // Create image for halo dark
  22.     Mat halo( img.rows, img.cols, CV_32FC3, Scalar(0.3,0.3,0.3) );
  23.     // Create circle 
  24.     circle(halo, Point(img.cols/2, img.rows/2), img.cols/3, Scalar(1,1,1), -1); 
  25.     blur(halo, halo, Size(img.cols/3, img.cols/3));
  26.  
  27.     // Convert the result to float to allow multiply by 1 factor
  28.     Mat resultf;
  29.     result.convertTo(resultf, CV_32FC3);
  30.  
  31.     // Multiply our result with halo
  32.     multiply(resultf, halo, resultf);
  33.  
  34.     // convert to 8 bits
  35.     resultf.convertTo(result, CV_8UC3);
  36.  
  37.     // show result
  38.     imshow("Lomograpy", result);
  39.  
  40.     // Release mat memory
  41.     halo.release();
  42.     resultf.release();
  43.     lut.release();
  44.     bgr[0].release();
  45.     bgr[1].release();
  46.     bgr[2].release();
  47. }

Cartoonize effect

  1. void cartoonCallback(int state, void* userData)
  2. {
  3.     /** EDGES **/
  4.     // Apply median filter to remove possible noise
  5.     Mat imgMedian;
  6.     medianBlur(img, imgMedian, 7);
  7.  
  8.     // Detect edges with canny
  9.     Mat imgCanny;
  10.     Canny(imgMedian, imgCanny, 50, 150);
  11.  
  12.     // Dilate the edges
  13.     Mat kernel= getStructuringElement(MORPH_RECT, Size(2,2));
  14.     dilate(imgCanny, imgCanny, kernel);
  15.  
  16.     // Scale edges values to 1 and invert values
  17.     imgCanny= imgCanny/255;
  18.     imgCanny= 1-imgCanny;
  19.  
  20.     // Use float values to allow multiply between 0 and 1
  21.     Mat imgCannyf;
  22.     imgCanny.convertTo(imgCannyf, CV_32FC3);
  23.  
  24.     // Blur the edgest to do smooth effect
  25.     blur(imgCannyf, imgCannyf, Size(5,5));
  26.  
  27.     /** COLOR **/
  28.     // Apply bilateral filter to homogenizes color
  29.     Mat imgBF;
  30.     bilateralFilter(img, imgBF, 9, 150.0, 150.0);
  31.  
  32.     // truncate colors
  33.     Mat result= imgBF/25;
  34.     result= result*25;
  35.  
  36.     /** MERGES COLOR + EDGES **/
  37.     // Create a 3 channles for edges
  38.     Mat imgCanny3c;
  39.     Mat cannyChannels[]={ imgCannyf, imgCannyf, imgCannyf};
  40.     merge(cannyChannels, 3, imgCanny3c);
  41.  
  42.     // Convert color result to float 
  43.     Mat resultf;
  44.     result.convertTo(resultf, CV_32FC3);
  45.  
  46.     // Multiply color and edges matrices
  47.     multiply(resultf, imgCanny3c, resultf);
  48.  
  49.     // convert to 8 bits color
  50.     resultf.convertTo(result, CV_8UC3);
  51.  
  52.     // Show image
  53.     imshow("Result", result);
  54.  
  55. }

Slider & Mouse & Button(QT_CHECKBOX, QT_RADIOBOX, QT_PUSH_BUTTON)

Slider

  1. createTrackbar("Lena", "Lena", &blurAmount, 30, onChange, &lena);
  2.  
  3. onChange(blurAmount, &lena);
  4.  
  5. static void onChange(int pos, void* userInput)
  6. {
  7. 	if(pos <= 0)
  8. 		return;
  9. 	// Aux variable for result
  10. 	Mat imgBlur;
  11.  
  12. 	// Get the pointer input image
  13. 	Mat* img= (Mat*)userInput;
  14.  
  15. 	// Apply a blur filter
  16. 	blur(*img, imgBlur, Size(pos, pos));	
  17.  
  18. 	// Show the result
  19. 	imshow("Lena", imgBlur);
  20. }

Mouse

  1. setMouseCallback("Lena", onMouse, &lena);
  2.  
  3. static void onMouse( int event, int x, int y, int, void* userInput )
  4. {
  5. 	if( event != EVENT_LBUTTONDOWN )
  6. 	        return;
  7.  
  8. 	// Get the pointer input image
  9. 	Mat* img= (Mat*)userInput;
  10.  
  11. 	// Draw circle
  12. 	circle(*img, Point(x, y), 10, Scalar(0,255,0), 3);
  13.  
  14. 	// Call on change to get blurred image
  15. 	onChange(blurAmount, img);
  16.  
  17. }

Button(QT_CHECKBOX, QT_RADIOBOX, QT_PUSH_BUTTON)

  1.  
  2. void grayCallback(int state, void* userData)
  3. {
  4. 	applyGray= true;
  5. 	applyFilters();
  6. }
  7. void bgrCallback(int state, void* userData)
  8. {
  9. 	applyGray= false;
  10. 	applyFilters();
  11. }
  12.  
  13. void blurCallback(int state, void* userData)
  14. {
  15. 	applyBlur= (bool)state;
  16. 	applyFilters();
  17. }
  18.  
  19. void sobelCallback(int state, void* userData)
  20. {
  21. 	applySobel= !applySobel;
  22. 	applyFilters();
  23. }
  24.  
  25. 	createButton("Blur", blurCallback, NULL, QT_CHECKBOX, 0);
  26.  
  27. 	createButton("Gray",grayCallback,NULL,QT_RADIOBOX, 0);
  28. 	createButton("RGB",bgrCallback,NULL,QT_RADIOBOX, 1);
  29.  
  30. 	createButton("Sobel",sobelCallback,NULL,QT_PUSH_BUTTON, 0);

OpenCV Simple GUI / QT

Simple Window

namedWindow("Photo", WINDOW_AUTOSIZE);
 
moveWindow("Photo", 520, 10);
 
imshow("Photo", photo);
 
resizeWindow("Lena", 512, 512);
 
destroyWindow("Photo");
 
destroyAllWindows();//

Qt Functions

displayOverlay("Lena", "Overlay 5secs", 5000);
 
displayStatusBar("Lena", "Status bar 5secs", 5000);
 
saveWindowParameters("Lena");
 
loadWindowParameters("Lena");

OpenCV Data Storage

Write OpenCV Data

  1. FileStorage fs("test.yml", FileStorage::WRITE);
  2. Mat m1= Mat::eye(2,3, CV_32F);
  3. fs << "m1" << m1;
  4. fs.release();

Read OpenCV Data

  1. FileStorage fs2("test.yml", FileStorage::READ);
  2. Mat r;
  3. fs2["m1"] >> r;
  4. std::cout << r << std::endl;
  5. fs2.release();

Read/Write Image and Read VideoFile/Camera and Show It

Read Image

  1. Mat color= imread("../lena.jpg");
  2. Mat gray= imread("../lena.jpg", 0);

Write Image

  1. imwrite("lenaGray.jpg", gray);

Show Image

  1. imshow("Lena Gray", gray);

Read VideoFile/Camera

  1. VideoCapture cap;
  2. cap.open(videoFile);
  3. cap.open(0);//default camera
  4.  
  5. if(!cap.isOpened())  // check if succeeded
  6.     return -1;
  7.  
  8. cap.release();

Show VideoFile/Camera

  1. namedWindow("Video",1);
  2. for(;;)
  3. {
  4.     Mat frame;
  5.     cap >> frame; // get a new frame from camera
  6.     imshow("Video", frame);
  7.     if(waitKey(30) >= 0) break;
  8. }