当前位置: 移动技术网 > 移动技术>移动开发>Android > android bitmap转成opencv的mat(图像扫描)

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

2020年07月23日  | 移动技术网移动技术  | 我要评论
如何将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的像素数据转换成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]= (uchar)arr_[index];
            outs.at<Vec4b>(i,j)[1]= (uchar)arr_[index+1];
            outs.at<Vec4b>(i,j)[2]= (uchar)arr_[index+2];
            outs.at<Vec4b>(i,j)[3]= (uchar)arr_[index+3];
            index+=4;
        }
    }

本文地址:https://blog.csdn.net/mhhyoucom/article/details/107487847

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网