-1

I have a project where i need to detect specific colors from leaves images, like green, brown and yellow. I found this tutorial (http://opencv-srf.blogspot.com.br/2010/09/object-detection-using-color-seperation.html) that explains how to create a real time trackbar to find the best values for that, but it uses images from a webcam, and i want to use it with pictures. Can you guys please help me do that?

Thank you.

Nicholas
  • 47
  • 1
  • 11
  • Please note that this is a question and answer site, not a code writing service. If you [edit] your question to describe what you have tried so far and where you are stuck, then we can try to help with specific problems. You should also read [ask]. – Toby Speight Apr 13 '16 at 17:40
  • I'm really sorry. I asked this question attending a request from another user in another question i asked (http://stackoverflow.com/questions/36586924/having-difficulties-to-detect-certain-colors-using-opencv) and in a hurry when i was at my job because i wanted to study it later before my classes started and i forgot to show you the code i wrote which was not working. I'll take your advice and I will not ask questions in such a hurry so i dont forget to show you more details. Thank you. – Nicholas Apr 14 '16 at 11:20

1 Answers1

1

Here is the code for thresholding an HSV image, selecting the ranges with trackbars.

Note that, differently from a video (as described here), I used morphologyEx to perform morphological operations, and replaced C style cvCreateTrackbar with the C++ function createTrackbar.

The comments in the code should be clear. Please ping me if something is not clear:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // Load BGR image
    Mat3b bgr = imread("path_to_image");
    if (bgr.empty()) 
    {
        cout << "Cannot open the image" << endl;
        return -1;
    }

    // Transform to HSV
    Mat3b hsv;
    cvtColor(bgr, hsv, COLOR_BGR2HSV); 

    // Create a window called "Control"
    namedWindow("Control", CV_WINDOW_AUTOSIZE); 

    // Set starting values for ranges
    int iLowH = 0;
    int iHighH = 179;

    int iLowS = 0;
    int iHighS = 255;

    int iLowV = 0;
    int iHighV = 255;

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

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

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

    //Show the original image
    imshow("Original", bgr); 

    // Create kernel for morphological operation
    Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));

    // Infinte loop, until user press "esc"
    while (true)
    {
        Mat mask;
        inRange(hsv, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), mask); //Threshold the image

        //morphological opening (remove small objects from the foreground)
        morphologyEx(mask, mask, MORPH_OPEN, kernel);

        //morphological closing (fill small holes in the foreground)
        morphologyEx(mask, mask, MORPH_CLOSE, kernel);

        //Show the thresholded image
        imshow("Thresholded Image", mask); 

        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;
}
Miki
  • 40,887
  • 13
  • 123
  • 202