"SimpleBlobDetector" can't detect big object

asked 2017-07-08 09:57:28 -0500

updated 2017-07-08 10:00:13 -0500

LBerger gravatar image

image description

Hello, I'm trying to use SimpleBlobDetector for detecting object.

what I wanted was draw circle on the big object but what I saw was draw circle on only small objects.

I adjusted value with track bar but didnt work.

what is the problem with my code..?

I tried so many methods...

and.. one more question. only black object can be detected by SimpleBlobDetector(So I inverted Thresholded image)

why..?

it is my code.

#include <iostream>
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/opencv.hpp"


using namespace cv;
using namespace std;

 int main( int argc, char** argv )
 {
    VideoCapture cap(0); //capture the video from web cam

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the web cam" << endl;
         return -1;
    }
namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"



int iLowH = 0;
 int iHighH = 179;

 int iLowS = 0; 
 int iHighS = 255;

 int iLowV = 0;
 int iHighV = 255;

 //Create trackbars in "Control" window
 cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
 cvCreateTrackbar("HighH", "Control", &iHighH, 179);

 cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
 cvCreateTrackbar("HighS", "Control", &iHighS, 255);

 cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
 cvCreateTrackbar("HighV", "Control", &iHighV, 255);

    while (true)
    {
        Mat imgOriginal;

        bool bSuccess = cap.read(imgOriginal); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

  Mat imgHSV;

  cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

  Mat imgThresholded;
  Mat revimgThresholded; 

  inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); 

  threshold(imgThresholded, revimgThresholded, 200, 255, 1);  

  Mat imgBlobbing;

  //morphological opening (remove small objects from the foreground)
 // erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
 // dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 

  //morphological closing (fill small holes in the foreground)
 // dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 
 // erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );

  SimpleBlobDetector::Params params;


 // params.filterByColor = 1;
  //params.blobColor = 0;

// Change thresholds
 // params.minThreshold = 0;
 // params.maxThreshold = 50;
 // params.filterByArea = 1;
 // params.minArea = 50;
 // params.maxArea = 10000;  

  SimpleBlobDetector Detector(params);


  std::vector<KeyPoint> keypoints;
  Detector.detect(revimgThresholded, keypoints);


  drawKeypoints(revimgThresholded, keypoints, imgBlobbing, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);


  imshow("Thresholded Image Inverted", revimgThresholded); //show the thresholded image
  imshow("Original", imgOriginal); //show the original image
  imshow("imgBlobbing", imgBlobbing); //라벨링 이미지를 보여줌.

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break; 
       }
    }


   return 0;

}
edit retag flag offensive close merge delete