Opencv Android Green Color Detection

OpenCV Android Green Color Detection

Green color is HSV space has H = 120 and it's in range [0, 360].

OpenCV halves the H values to fit the range [0,255], so H value instead of being in range [0, 360], is in range [0, 180].
S and V are still in range [0, 255].

As a consequence, the value of H for green is 60 = 120 / 2.

You upper and lower bound should be:

// sensitivity is a int, typically set to 15 - 20 
[60 - sensitivity, 100, 100]
[60 + sensitivity, 255, 255]

UPDATE

Since your image is quite dark, you need to use a lower bound for V. With these values:

sensitivity = 15;
[60 - sensitivity, 100, 50] // lower bound
[60 + sensitivity, 255, 255] // upper bound

the resulting mask would be like:

Sample Image

You can refer to this answer for the details.

how can i track GREEN color in openCV? while red&blue worked

In HSV color space, Hue represents the traditional colors which we perceive. Another main difference is that when RGB color space represented as a cube, HSV is a cylinder so the range of Hue is actually 0 to 360 degrees. Hue represents Green values between ~121 to ~180 degrees and when we rescale that to the input range of Opencv functions (0-255) the value of green should be between 85 to 128. If you are looking for a visual representation this page has a nice interactive model for both RGB and HSV color spaces.

Android OpenCV Color detection

Your mat is of CvType.CV_8UC1 image, i.e. you are working on a grayscale image. Try with CvType.CV_8UC3

Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC3);

hsv_image should look like this:

Sample Image

How to select a custom range:


You may want to detect a green circle.
Well, in HSV, tipically the range is:

H in [0,360]
S,V in [0,100]

However, for CV_8UC3 images, each component H,S,V can be represented by only 256 values at most, since it's stored in 1 byte. So, in OpenCV, the ranges H,S,V for CV_8UC3 are:

H in [0,180] <- halved to fit in the range
S,V in [0,255] <- stretched to fit the range

So to switch from typical range to OpenCV range you need to:

opencv_H = typical_H / 2;
opencv_S = typical_S * 2.55;
opencv_V = typical_V * 2.55;

So, green colors are around the value of hue of 120. The hue can have a value in the interval [0,360].
However, for Mat3b HSV images, the range for H is in [0,180], i.e. is halved so it can fit in a 8 bit representation with at most 256 possible values.
So, you want the H value to be around 120 / 2 = 60, say from 50 to 70.
You also set a minimum value for S,V to 100 in order to prevent very dark (almost black) colors.

Mat green_hue_range
inRange(hsv_image, cv::Scalar(50, 100, 100), cv::Scalar(70, 255, 255), green_hue_range);

Color detection in a static image - OpenCV Android

I don't know if I understand you exactly, but here it comes.

You need to create BufferedImage object to get RGB value:

File f = new File(yourFilePath);
BufferedImage img = ImageIO.read(f);

You can get RGB Color values from the image from then. You have 4 squares; to check their RGB values, you can check the corner pixels' RGB values:

Color leftTop = new Color(img.getRGB(0, 0));
Color rightTop = new Color(img.getRGB(img.getWidth - 1, 0));
Color leftBottom = new Color(img.getRGB(0, img.getHeight - 1));
Color rightBottom = new Color(img.getRGB(img.getWidth - 1, img.getHeight - 1));

After that it's easy to get red, green and blue values individually:

int red = leftTop.getRed();
int green = leftTop.getGreen();
int blue = leftTop.getBlue();

EDIT:
I'm really sorry, I didn't see it's for Android. As you said, Android doesn't have ImageIO class. To accomplish the task in Android, first initialize the image:

Bitmap img = BitmapFactory.decodeFile(yourFilePath);

From then the operation is pretty much the same:

int leftTop = img.getPixel(0, 0);
...

int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);

ANDROID - color detection using openCV - how to?

I think mHSVThreshed is a binary mat

so maybe this line :

Imgproc.cvtColor(mHSVThreshed, mRgba, Imgproc.COLOR_HSV2RGB, 0);

should change to :

Imgproc.cvtColor(mHSVThreshed, mRgba, Imgproc.COLOR_GRAY2RGB, 0);

I spent a lot of time dealing with the "showing" problem too...

hope this help...



Related Topics



Leave a reply



Submit