欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-07-03 17:49:37
如何将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