一、問(wèn)題原因:
? ? Android項(xiàng)目里 bitmap轉(zhuǎn)Mat后通過(guò)Imgcodecs.imwrite 保存發(fā)現(xiàn)原本圖片顏色和轉(zhuǎn)換后的顏色不一致。
原圖片:

轉(zhuǎn)換后圖片:


二、問(wèn)題分析
觀察發(fā)現(xiàn),原本紅色的變成了藍(lán)色,通過(guò)拾色軟件可以看到RGB的數(shù)值變化。
網(wǎng)上查看很多資料后發(fā)現(xiàn)OpenCV的排序不是傳統(tǒng)的RGB,而是BGR排序。


三、解決辦法
????把bitmap的ARGB得順序,變成ABGR順序后,再進(jìn)行bitmap轉(zhuǎn)Mat。
? ? 代碼實(shí)例:
public static BitmapRgbToBgr(Bitmap bitmap){
int width=bitmap.getWidth();
? ? int height=bitmap.getHeight();
? ? int[] pixels=new int[width*height];
? ? bitmap.getPixels(pixels,0,width,0,0,width,height);
? ? int indx=0;
? ? int a=0,r=0,g=0,b=0;
? ? for(int row=0;row
indx=row*width;
? ? ? ? for (int col=0;col
int pixel=pixels[indx];
? ? ? ? ? ? a=(pixel>>24)&0xff;
? ? ? ? ? ? r=(pixel>>16)&0xff;
? ? ? ? ? ? g=(pixel>>8)&0xff;
? ? ? ? ? ? b=pixel&0xff;
? ? ? ? ? ? pixel=((a&0xff)<<24)|((b&0xff)<<16)|((g&0xff)<<8)|(r&0xff);
? ? ? ? ? ? pixels[indx]=pixel;
? ? ? ? ? ? indx++;
? ? ? ? }
}
bitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
? ? bitmap.setPixels(pixels,0,width,0,0,width,height);
? ? return bitmap;
}