Android OpenCV Color detection -


currently i'm developing app detect colored circles. i'm trying following this tutorial, guy detects red circles on image python. i've written same code, java.

                    mat mat = new mat(bitmap.getwidth(), bitmap.getheight(),                             cvtype.cv_8uc3);                      mat hsv_image = new mat();                     utils.bitmaptomat(bitmap, mat);                     imgproc.cvtcolor(mat, hsv_image, imgproc.color_bgr2hsv);                      mat lower_red_hue_range = new mat();                     mat upper_red_hue_range = new mat();                      core.inrange(hsv_image, new scalar(0, 100, 100), new scalar(10, 255, 255), lower_red_hue_range);                     core.inrange(hsv_image, new scalar(160, 100, 100), new scalar(179, 255, 255), upper_red_hue_range);                     utils.mattobitmap(hsv_image, bitmap);                 mutablebitmap = bitmap.copy(bitmap.config.argb_8888, true);                 image.setimagebitmap(mutablebitmap); 

image use identical 1 tutorial: enter image description here

this image applied bgr2hsv: enter image description here

when execute code using lower red hue range, detects blue circle. when use upper red hue range gives me black bmp(doesn't detect anything). how can be? doing wrong? literally copy moved python java. why's result different then? in advance.

your mat of cvtype.cv_8uc1 image, i.e. working on grayscale image. try cvtype.cv_8uc3

mat mat = new mat(bitmap.getwidth(), bitmap.getheight(), cvtype.cv_8uc3); 

hsv_image should this:

enter image description here

how select custom range:


you may want detect green circle. well, in hsv, tipically range is:

h in [0,360] s,v in [0,100] 

however, cv_8uc3 images, each component h,s,v can represented 256 values @ most, since it's stored in 1 byte. so, in opencv, ranges h,s,v cv_8uc3 are:

h in [0,180] <- halved fit in range s,v in [0,255] <- stretched fit range 

so switch typical range opencv range need to:

opencv_h = typical_h / 2; opencv_s = typical_s * 2.55;  opencv_v = typical_v * 2.55; 

so, green colors around value of hue of 120. hue can have value in interval [0,360]. however, mat3b hsv images, range h in [0,180], i.e. halved can fit in 8 bit representation @ 256 possible values. so, want h value around 120 / 2 = 60, 50 70. set minimum value s,v 100 in order prevent dark (almost black) colors.

mat green_hue_range inrange(hsv_image, cv::scalar(50, 100, 100), cv::scalar(70, 255, 255), green_hue_range); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -