ANDROID - color detection using openCV - how to?

南楼画角 提交于 2019-12-02 17:21:09

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...

friends. i give you the result of 1 month of hard work and help from friends across the ocean:

Ethan was right. but the code needed some fixing.

the code:

    capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_BGRA);
    Imgproc.cvtColor(mRgba, mHSV, Imgproc.COLOR_BGR2HSV,3);
    Core.inRange(mHSV, new Scalar(0, 100, 30), new Scalar(5, 255, 255), mHSVThreshed);
    Imgproc.cvtColor(mHSVThreshed, mRgba, Imgproc.COLOR_GRAY2BGR, 0);
    Imgproc.cvtColor(mRgba, mRgba2, Imgproc.COLOR_BGR2RGBA, 0);
    Bitmap bmp = Bitmap.createBitmap(mRgba2.cols(), mRgba2.rows(), Bitmap.Config.ARGB_8888);


    if (Utils.matToBitmap(mRgba2, bmp))...

first, the mat is binary 0 or 255 so the transform to gray level is more "natural". second, the conversion from HSVto RGBis in fact HSV-BGR!!. and last thing is that the preview is expecting RGBA Bitmap.

thats it. hope other can benefit from this post. SHALOM!

Well as far as I see it you fetch the image frame in RGBA and save it under the name "mHSV"

capture.retrieve(mHSV, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);

so you should there store it as mRgba

in the cvtColor you need to transform it to HSV via COLOR_RGBA2HSV. Assuming you have changed the names this would be:

Imgproc.cvtColor(mRgba, mRSV, Imgproc.COLOR_RGB2HSV, 0);

And I assume this repetition of the images comes from the "4" in you cvtColor function since your HSV picture will only have 3 channels. Put in a 0 there and it should be detected automatically...

I hope it helps...

why is it COLOR_RGB2HSV? shouldnt it be COLOR_HSV2RGB?

I would say that it should :). Looks like a problem on how opencv grabs the pixels. Check that both your input and output images have the same size and numberof channels.

This

capture.retrieve(mHSV, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);

looks also weird to me, Are you storing the RGBA image that you get into an image at HSV format? That would explain the problem. Try to do something like that : RGB(capture retrieve) => HSV(cvt color) => color detection => RGB (cvt color again)

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