ImageView上的Android OpenCV颜色分段显示

时间:2014-07-04 14:20:38

标签: android c++ opencv colors

我到处寻找解决方案,但我没有找到任何解决方案。我知道这是一个反复出现的问题,所以我会尽量准确。

我想使用Android和OpenCV在HSV中执行颜色分割。我有一个HSV样本数组,对应于我想要分段的颜色,我从中获取meanstd以获得一系列值,如:

lowerBoundH = new Scalar(meanSamples[0]-stdSamples[0]);
lowerBoundS = new Scalar(meanSamples[1]-stdSamples[1]);
upperBoundH = new Scalar(meanSamples[0]+stdSamples[0]);
upperBoundS = new Scalar(meanSamples[1]+stdSamples[1]);

获得H,S和V的这些范围后,我会执行以下操作(我只使用H& S通道):

mH = new Mat();
mS = new Mat();
List<Mat> channels = new ArrayList<Mat>(2);

// convert inputFrame to HSV for segmentation
Mat hsvFrame = new Mat();
convertToHSV(inputFrame, hsvFrame);
Core.split(hsvFrame, channels);
// get H&S channels of the HSVimage
mH = channels.get(0);
mS = channels.get(1);

if(mH!=null & mS!=null){
    mHinRange = new Mat();
    mSinRange = new Mat();
    Core.inRange(mH, lowerBoundH, upperBoundH, mHinRange);
    Core.inRange(mS, lowerBoundS, upperBoundS, mSinRange);
    Mat filtered = new Mat(hsvFrame.height(), hsvFrame.width(), CvType.CV_8UC1, new Scalar(4));

    Core.bitwise_and(mHinRange, mSinRange, filtered);
    Mat aux = new Mat();
    Mat mRgb = new Mat();
    inputFrame.copyTo(mRgb);
    Imgproc.cvtColor(filtered, mRgb, Imgproc.COLOR_GRAY2BGR, 0);
    Imgproc.cvtColor(mRgb, aux, Imgproc.COLOR_BGR2RGBA, 0);

    return aux;
}else{
    Log.v("NULL","H OR S CHANNELS ARE NULL");
    return null;
}

然后,我将该已细分的Mat转换为Bitmap,以便将其显示在ImageView中,如下所示:

Bitmap bmp = null;
Mat tmp = new Mat(result.height(),result.width(), CvType.CV_8UC1, new Scalar(4));
try {
    bmp = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(tmp, bmp);
}catch (CvException e){Log.d("Exception",e.getMessage());}
qrImage.setImageBitmap(bmp);

但是当我显示生成的图像时,我得到一个完全黑色的图像。我知道这是错误的,因为相机直接面向我要分割的彩色物体。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

多么愚蠢的错误。我从空tmp Mat创建了Bitmap。

相反,我需要使用result Mat:

创建它
Bitmap bmp = null;
try {
    bmp = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(result, bmp);
}catch (CvException e){Log.d("Exception",e.getMessage());}
qrImage.setImageBitmap(bmp);