1 | initial version |
If you are able to detect targets and your problem is that you need to filter only green ones then you need to get the average color of each target and see if it's in your color range. To do that you need to you use cv.mean(src, mask). This is documentation for cv.mean.
Also, see this question, it's very close to yours.
Alternatively, you can try this approach
Here is some code on how to make a mask in specific color range, you may need to change the upper and lower color values, also here is used THRESH_BINARY but it may be better to use THRESH_TRUNC.
// segmenting by green color
const greenColorUpper = hue => new cv.Vec(hue, 0.8 * 255, 0.6 * 255); // change this values
const greenColorLower = hue => new cv.Vec(hue, 0.1 * 255, 0.05 * 255); // change this values
const makeColorMask = (img) => {
// filter by color
const imgHLS = img.cvtColor(cv.COLOR_BGR2HLS);
const rangeMask = imgHLS.inRange(greenColorLower(80), greenColorUpper(140)); // change this values
// remove noise
const blurred = rangeMask.blur(new cv.Size(10, 10));
const thresholded = blurred.threshold(
200,
255,
cv.THRESH_BINARY
);
return thresholded;
};
// Find Contours
const contours = new cv.MatVector();
const thresholded = makeColorMask(img)
cv.findContours(thresholded, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);
And here is the documentation for shape matching. If shape matching doesn't work for you, you can write your own circle validator. OpenCV gives you all the vertices of the contour, perimeter, and center of the contour.
Here are very useful posts about color and shape matching with python
https://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/
https://www.pyimagesearch.com/2016/02/15/determining-object-color-with-opencv/
2 | No.2 Revision |
If you are able to detect targets and your problem is that you need to filter only green ones then you need to get the average color of each target and see if it's in your color range. To do that you need to you use cv.mean(src, mask). This is documentation for cv.mean.
Also, see this question, it's very close to yours.
Alternatively, you can try this approach
Here is some code on how to make a mask in specific color range, you may need to change the upper and lower color values, also here is used THRESH_BINARY but it may be better to use THRESH_TRUNC.
// segmenting by green color
const greenColorUpper = hue => new cv.Vec(hue, 0.8 * 255, 0.6 * 255); // change this values
const greenColorLower = hue => new cv.Vec(hue, 0.1 * 255, 0.05 * 255); // change this values
const makeColorMask = (img) => {
// filter by color
const imgHLS = img.cvtColor(cv.COLOR_BGR2HLS);
const rangeMask = imgHLS.inRange(greenColorLower(80), greenColorUpper(140)); // change this values
// remove noise
const blurred = rangeMask.blur(new cv.Size(10, 10));
const thresholded = blurred.threshold(
200,
255,
cv.THRESH_BINARY
);
return thresholded;
};
// Find Contours
const contours = new cv.MatVector();
const hierarchy = new cv.Mat();
const thresholded = makeColorMask(img)
cv.findContours(thresholded, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);
And here is the documentation for shape matching. If shape matching doesn't work for you, you can write your own circle validator. OpenCV gives you all the vertices of the contour, perimeter, and center of the contour.
Here are very useful posts about color and shape matching with python
https://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/
https://www.pyimagesearch.com/2016/02/15/determining-object-color-with-opencv/