android bitmap转成opencv的mat(图像扫描)

如何将bitmap的像素数据转换成opencv的mat对象?

首先从bitmap中或者像素数据:

        String imgPath="/data/data/org.opencv.samples.tutorial2/cache/test_img.png";
        bitmap = BitmapFactory.decodeFile(imgPath);
        ByteBuffer allocate = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(allocate);
        array = allocate.array();

然后将像素数据复制给mat对象:(格式rgba)

    Mat outs=Mat::zeros(h,w, CV_8UC4);
    int index=0;
    for (int i = 0; i < outs.rows; ++i) {
        for (int j = 0; j < outs.cols; ++j) {
            outs.at<Vec4b>(i,j)[0]= arr_[index]>>8;
            outs.at<Vec4b>(i,j)[1]= arr_[index+1]>>8;
            outs.at<Vec4b>(i,j)[2]= arr_[index+2]>>8;
            outs.at<Vec4b>(i,j)[3]= arr_[index+3]>>8;
            index+=4;
        }
    }

猜你喜欢

转载自blog.csdn.net/mhhyoucom/article/details/107487847