我正在使用opencv通过opencv将android位图转换为grescale。下面是我正在使用的代码,
IplImage image = IplImage.create( bm.getWidth(), bm.getHeight(), IPL_DEPTH_8U, 4); //creates default image
bm.copyPixelsToBuffer(image.getByteBuffer());
int w=image.width();
int h=image.height();
IplImage grey=cvCreateImage(cvSize(w,h),image.depth(),1);
cvCvtColor(image,grey,CV_RGB2GRAY);
bm 是源图像。此代码工作正常并转换为灰度,我已经通过保存到 sdcard 然后再次加载对其进行了测试,但是当我尝试使用以下方法加载它时,我的应用程序崩溃了,任何建议。
bm.copyPixelsFromBuffer(grey.getByteBuffer());
iv1.setImageBitmap(bm);
iv1 是我想设置 bm 的 imageview。
最佳答案
我从未使用过适用于 Android 的 OpenCV 绑定(bind),但这里有一些代码可以帮助您入门。将其视为伪代码,因为我无法尝试...但您会了解基本概念。它可能不是最快的解决方案。我从 this answer 粘贴.
public static Bitmap IplImageToBitmap(IplImage src) {
int width = src.width;
int height = src.height;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for(int r=0;r<height;r++) {
for(int c=0;c<width;c++) {
int gray = (int) Math.floor(cvGet2D(src,r,c).getVal(0));
bitmap.setPixel(c, r, Color.argb(255, gray, gray, gray));
}
}
return bitmap;
}
关于android - 灰度 Iplimage 到 android 位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7527917/