2012-01-10 65 views
1

我需要在Android位图中转换1通道iplimage(灰色)。我有:1 channel iplimage - > Android位图

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

我认为问题是在渠道的顺序,因为与此代码我得到一个半透明的图片。也许我需要改变“aux”中的通道顺序来获取ARGB顺序并检查位图配置(ARGB_8888)。这可能吗?

回答

0

我从来没有使用Android的OpenCV绑定,但这里有一些代码让你开始。把它当作伪代码,因为我无法尝试......但你会得到基本的想法。

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