1 channel iplimage -> Android Bitmap

為{幸葍}努か 提交于 2020-01-16 01:00:31

问题


I need convert a 1-channel iplimage (gray) in an Android Bitmap. I have:

IplImage aux = IplImage.create(senal_gray.width, senal_gray.height, IPL_DEPTH_8U, 4); 
cvCvtColor(senal_gray, aux, CV_GRAY2BGRA);
Bitmap bm = Bitmap.createBitmap(aux.width, aux.height, Bitmap.Config.ARGB_8888);
bm.copyPixelsFromBuffer(aux.getByteBuffer());

I think that the problem is in the order of channels, because with this code I get a translucent picture. Perhaps I need change the order of channels in "aux" to get ARGB order and check with Bitmap configuration (ARGB_8888). Is this possible?


回答1:


I've never used the OpenCV bindings for Android, but here's some code to get you started. Regard it as pseudocode, because I can't try it out... but you'll get the basic idea.

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


来源:https://stackoverflow.com/questions/8807414/1-channel-iplimage-android-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!