Having difficulties to detect certain colors using openCV

こ雲淡風輕ζ 提交于 2019-12-13 01:15:52

问题


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:

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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/36586924/having-difficulties-to-detect-certain-colors-using-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!