上篇雖然成功把Bitmap轉(zhuǎn)為了BGRA的格式傳到Mat矩陣中,但是在做人臉識別的過程中,需要的圖像是3通道的,即BGR格式。雖然opencv中有函數(shù)cvtColor(test,bgr,CV_RGBA2BGR);可以將其轉(zhuǎn)換,但是這樣經(jīng)過ARGB_8888->BGRA->BGR轉(zhuǎn)了一大圈貌似浪費cpu和內(nèi)存資源。不如直接將Bitmap的ARGB_8888直接轉(zhuǎn)為BGR傳到Mat矩陣中。代碼如下:
public byte[] getPixelsBGR(Bitmap image) {
// calculate how many bytes our image consists of
int bytes = image.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer
byte[] temp = buffer.array(); // Get the underlying array containing the data.
byte[] pixels = new byte[(temp.length/4) * 3]; // Allocate for BGR
// Copy pixels into place
for (int i = 0; i < temp.length/4; i++) {
pixels[i * 3] = temp[i * 4 + 2]; //B
pixels[i * 3 + 1] = temp[i * 4 + 1]; //G
pixels[i * 3 + 2] = temp[i * 4 ]; //R
}
return pixels;
}
這樣得到的數(shù)組就是BGR格式的圖像數(shù)據(jù),可以傳遞到Mat矩陣中,格式為CV_8UC3.
但是這樣全是自己寫代碼進行的轉(zhuǎn)換,一次偶然發(fā)現(xiàn)完全沒必要這么麻煩,完全可以有更簡單的方法。。??吹搅诉@篇文章:bitmap轉(zhuǎn)mat
在安卓設(shè)備上進行人臉識別,效率比較重要,內(nèi)存和cpu的消耗要降到最低。以上各種方法哪種用時最少,還有待驗證。
————————————————
原文鏈接:https://blog.csdn.net/u013547134/article/details/40918513