Having difficulties to detect certain colors using

2019-08-10 01:55发布

I have a project in which i must detect 3 specific colors in many leaves pictures: Green, Yellow and Brown.

I'm using the following image as an example:

enter image description here

The objective to detect the different colors is to determine if the tree is sick or not, so it's really important to be able to tell correctly what is green, yellow and brown, even in small amounts of pixels.

I wrote the following code:

//Load the image
Mat img_bgr = imread("c:\\testeimagem\\theeye\\greening32.jpg", 1);
if (img_bgr.empty()){
    cout << "Nenhuma imagem foi carregada..." << endl;
    return -1;
}

//Display the image
namedWindow("Original Image", WINDOW_NORMAL);
imshow("Original Image", img_bgr);
waitKey(0);
destroyAllWindows;

//Conversion to HSV
Mat img_hsv;
cvtColor(img_bgr, img_hsv, CV_BGR2HSV_FULL);

//Extracting colors - HSV
Mat cores_divididas, green, yellow, brown;

//Yellow
inRange(img_hsv, Scalar(28, 240, 240), Scalar(33, 255, 255), yellow);
imwrite("c:\\testeimagem\\theeye\\yellow.jpg", yellow);

//Green
inRange(img_hsv, Scalar(38, 100, 100), Scalar(70, 190, 190), green);
imwrite("c:\\testeimagem\\theeye\\green.jpg", green);

//Brown
inRange(img_hsv, Scalar(10, 90, 90), Scalar(20, 175, 175), brown);
imwrite("c:\\testeimagem\\theeye\\brown.jpg", brown);

namedWindow("Yellow", WINDOW_NORMAL);
imshow("Yellow", yellow);

namedWindow("Green", WINDOW_NORMAL);
imshow("Green", green);

namedWindow("Brown", WINDOW_NORMAL);
imshow("Brown", brown);

waitKey(0);
destroyAllWindows;

return 0;

If you guys compile this code, you will notice that the green color is not properly detected and the other colors aren't detected at all.

As a guide for reference values, I used this trackbar.

2条回答
该账号已被封号
2楼-- · 2019-08-10 02:54

Try out these ranges:

//Yellow
inRange(img_hsv, Scalar(28, 0, 0), Scalar(33, 255, 255), yellow);
imwrite("yellow.jpg", yellow);

//Green
inRange(img_hsv, Scalar(38, 0, 0), Scalar(70, 255, 255), green);
imwrite("green.jpg", green);

//Brown
inRange(img_hsv, Scalar(10, 0, 0), Scalar(20, 255, 255), brown);
imwrite("brown.jpg", brown);

On your leaf image it seems there is no brown pigment at all. I tested it out with this leaf, Brownish leaf , and it looks ok.

The reason why I tried these ranges is behind the fact that the true color information is (correct me if I'm wrong) embedded in the Hue quantity.

Obs.: Go with CV_BGR2HSV, as already mentioned.

查看更多
狗以群分
3楼-- · 2019-08-10 02:57

When you convert the original image's color space, try using CV_BGR2HSV instead of CV_BGR2HSV_FULL. The link you referenced provides reference values based on CV_BGR2HSV, in which the hue has a different range of values, so that's probably at least one factor causing your issues.

查看更多
登录 后发表回答