ANDROID - 使用openCV的颜色检测 - 如何? [英] ANDROID - color detection using openCV - how to?

查看:579
本文介绍了ANDROID - 使用openCV的颜色检测 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用HSV颜色空间以只显示黄色对象的方式显示一个脱粒图像。我使用这个代码(基于由openCV 2.3.1 android示例给出的代码):

  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;
}



基本(抽象)类包含run方法:

  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 );
...
}

我得到这个扭曲的预览,我想我可以理解(HSV格式),但为什么它重复自己(我画一条绿线来强调它)4次?什么是黑色水平线?

我做错了什么?



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

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

为什么是COLOR_RGB2HSV?应该是COLOR_HSV2RGB?



假设我传递了这个问题,我如何使用原始颜色的黄色对象创建一个灰度级图像?我想使用Core.inRange()方法,但是当我这样做,我得到黑屏。



是的,我想我看起来像一个总的急动,但我需要开始



更新1:




i尝试以这种方式执行RGB-> HSV-> RGB:

  @覆盖
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;
}

我得到:





更新2:



我终于明白,在设置框架之前,必须将其转换为RGB A 空格。
所以我现在尝试阈值与代码如下:

  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);

但现在它让我强制关闭...任何想法?

解决方案

我认为mHSVThreshed是一个二进制mat



所以也许这行:

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

应更改为:

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



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



希望这个帮助...


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);
...
}

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);

why is it COLOR_RGB2HSV? shouldnt it be COLOR_HSV2RGB?

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?

10x!

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:

?

Update 2:

i finally understand that before setting a frame, it must be converted into RGBA 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?

解决方案

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

这篇关于ANDROID - 使用openCV的颜色检测 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆