简体   繁体   English

使用Android二进制文件使用openCV Matrix裁剪图像

[英]Cropping an image using openCV Matrix using Android binaries

I'm running into problems cropping an image using the Android OpenCV binaries. 我在使用Android OpenCV二进制文件裁剪图像时遇到问题。

This is what I'm doing to try to crop the upper left quadrant of the image: 这是我要尝试裁剪图像左上象限的操作:

Initialization {
    mYuv = new Mat(mFrameSize.height + mFrameSize.height / 2, mFrameSize.width, CvType.CV_8UC1);
    mGraySubmat = mYuv.submat(0, mFrameSize.height, 0, mFrameSize.width);
}


processFrame(byte[] data) {
   mYuv.put(0, 0, data);
   Imgproc.cvtColor(mGraySubmat, mRgba, Imgproc.COLOR_GRAY2BGR, 4);
   bmp = Bitmap.createBitmap(mFrameSize.width/2, mFrameSize.height/2, Bitmap.Config.ARGB_8888);
   Rect roi = new Rect(0,0,mFrameSize.width/2,mFrameSize.height/2);
   Mat faceMat = new Mat(mRgba,roi);
   Utils.matToBitmap(faceMat.clone(), bmp);       

} }

What I get is a garbled image. 我得到的是乱码图像。 I've seen other posts that say to do what I'm doing but it's not working. 我看过其他帖子说可以做我正在做的事情,但是没有用。 Actually the image looks like it's there but there are also shadow images that are out of proportion. 实际上,图像看起来像在那里,但也有阴影图像不成比例。

I can do this manually and it works file - see below. 我可以手动执行此操作,并且可以正常运行-参见下文。 Am I doing something wrong? 难道我做错了什么?

processFrame(byte[] data) {

     for (int i = 0; i < frameSize; i++) {
     int y = (0xff & ((int) data[i]));
     rgba[i] = 0xff000000 + (y << 16) + (y << 8) + y;
}
// Example of cropping the upper left corder
bmp = Bitmap.createBitmap(mFrameSize.width/2, mFrameSize.height/2, Bitmap.Config.RGB_565);
bmp.setPixels(rgba, 0/* offset */, mFrameSize.width /* stride */, 0, 0, mFrameSize.width/2, mFrameSize.height/2);

I guess you start from this example . 我猜你是从这个例子开始的。 In this original source you can see that there is no Imgproc.COLOR_GRAY2BGR conversion. 在此原始源中,您可以看到没有Imgproc.COLOR_GRAY2BGR转换。 If you want to have a color image then you need to use choice two: 如果要获得彩色图像,则需要使用选择二:

Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);

and you do not need mGraySubmat. 并且您不需要mGraySubmat。

If you need grayscale image then the first choice is better: 如果您需要灰度图像,那么首选是更好的选择:

Imgproc.cvtColor(mGraySubmat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);

This latter choice truncates the chrominance components of the default preview format what is a YCbCr_420_SP (NV21) formatted image. 后一种选择会截断默认预览格式的色度分量,即YCbCr_420_SP (NV21)格式的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM