机器人 - 如何 - 用OpenCV的颜色检测?机器人、颜色、OpenCV

2023-09-04 08:51:44 作者:流萤

我的目标是使用,只有黄色的对象将被显示的方式HSV颜色空间来显示一个脱粒图像。我用这个code(基于OpenCV的安卓2.3.1样品给予了code):

my goal is to display a threshed image using the HSV color space in a way that only yellow objects will be shown. i use this code (based on a code given by the openCV 2.3.1 android samples):

protected Bitmap processFrame(VideoCapture capture) {
    //capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
    //Imgproc.cvtColor(mGray, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);

    capture.retrieve(mHSV, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
    Imgproc.cvtColor(mHSV, mRgba, Imgproc.COLOR_RGB2HSV, 4);
    //Core.inRange(mRgba, new Scalar(20, 100, 100), new Scalar(30, 255, 255), mRgba);

    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);

    if (Utils.matToBitmap(mRgba, bmp))
        return bmp;

    bmp.recycle();
    return null;
}

底座(摘要)类包含了跑的方法:

the base (Abstract)class contains the "run" method:

protected abstract Bitmap processFrame(VideoCapture capture);

public void run() {
...
bmp = processFrame(mCamera);
...
canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
...
}

我得到这个扭曲的preVIEW,我想我能理解(HSV格式),但为什么会重演(i`v画一条绿线,以强调它)4的时间?什么是黑色的水平线? 我究竟做错了什么?

i get this distorted preview which i think i can understand (HSV format) but why is it repeating itself (i`v draw a green line to emphasize it) 4 time? and what is the black horizontal line? what am i doing wrong?

最后一件事,什么是背后的逻辑:

one last thing, what is the logic behind:

Imgproc.cvtColor(mHSV, mRgba, Imgproc.COLOR_RGB2HSV, 4);

为什么它COLOR_RGB2HSV?不应该说,它是COLOR_HSV2RGB?

why is it COLOR_RGB2HSV? shouldnt it be COLOR_HSV2RGB?

比方说,我心中已经通过了这个问题,我怎样才能使一个灰度图像与在其天然的颜色黄色物体?我想到了使用Core.inRange()方法,但是当我这样做,我收到黑屏。

Let's say i'v passed this problem, how can i make a gray level image with the yellow objects in their native color? i thought using the Core.inRange() method but when i do this i get black screen.

是的,我想我看起来像一个总挺举,但我需要从什么地方开始,不是吗?

yes, i guess i look like a total jerk but i need to start from somewhere, don't i?

10倍!

更新1:的 我试图做RGB-> HSV-> RGB这种方式:

Update 1: i tried to do RGB->HSV->RGB this way:

 @Override
protected Bitmap processFrame(VideoCapture capture) {
    //capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
    //Imgproc.cvtColor(mGray, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);

    capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGB);
    Imgproc.cvtColor(mRgba, mHSV, Imgproc.COLOR_RGB2HSV,0);
    //Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_BGR2RGB, 4);
    //Core.inRange(mRgba, new Scalar(20, 100, 100), new Scalar(30, 255, 255), mRgba);
    Imgproc.cvtColor(mHSV,mRgba , Imgproc.COLOR_HSV2RGB,0);

    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);

    if (Utils.matToBitmap(mRgba, bmp))
        return bmp;

    bmp.recycle();
    return null;
}

和我得到:

and i got:

更新2:的

Update 2:

我终于明白,设置帧之前,它必须被转换成RGB *的 A 的*空间。 所以我现在试图与code如下门槛:

i finally understand that before setting a frame, it must be converted into RGB*A* space. so i now tried the threshold with the code as follow:

capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
    Imgproc.cvtColor(mRgba, mHSV, Imgproc.COLOR_RGB2HSV,0);
    Core.inRange(mHSV, new Scalar(20, 100, 100), new Scalar(30, 255, 255), mHSVThreshed);
    Imgproc.cvtColor(mHSVThreshed, mRgba, Imgproc.COLOR_HSV2RGB, 0);
    Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGB2RGBA, 0);
    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);

但现在它给了我力量关机...任何想法?

but now it gives me force shutdown... any ideas?

推荐答案

我觉得mHSVThreshed是一个二进制垫

I think mHSVThreshed is a binary mat

所以也许这行:

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

应更改为:

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

我花了很多时间处理显示的问题太多......

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

希望这有助于...