【问题标题】:How to access every pixel in two images and find their absolute difference using OpenCV? (Android Studio)如何使用 OpenCV 访问两个图像中的每个像素并找到它们的绝对差异? (安卓工作室)
【发布时间】:2017-06-01 22:43:36
【问题描述】:

我在 Android Studio 中有两个 Mat 图像,它们是 BGRA (8UC4)。我希望能够从两个图像中提取 BGRA 像素值,方法是一次遍历每个像素,然后找到它们在 RGB 值中的绝对差异。我不想减少透明度,所以我不能使用 Core.absdiff()。是否有捷径可寻?我已经使用 mat.get() 和 mat.put() 实现了这个功能,但它非常慢。

我已经看到下面发布的解决方案,但我不确定它是如何工作的,或者我如何更改它以使用我的图片/所需的功能:

Mat A ;
A.convertTo(A,cvType.CV_16SC3);
int size = (int) (A.total() * A.channels());
short[] temp = new short[size];
A.get(0, 0, temp);
for (int i = 0; i < size; i++)
   temp[i] = (short) (temp[i] / 2);
C.put(0, 0, temp);

我读到的大部分内容都涉及将 Mat 数据放入 Java Primitive 类型。由于我是 Java 和 OpenCV 的新手,我不太确定这意味着什么?

谢谢

【问题讨论】:

    标签: java android android-studio opencv rgba


    【解决方案1】:

    这是一个 C++ 实现。

    Mat aBGRA[4];     // array of Mats to hold Blue Green Red Alpha channels
    cv::split(A, aBGRA);  // split Mat A into channels
    Mat bBGRA[4];
    cv::split(B, bBGRA);  // split Mat B into channels
    Mat cBGR[3];      // Mat array to hold absolute diff of A and B BGR channels
    for ( int idx = 0; idx < 3; ++idx)   // loop BGR channels
    {
        cBGR[idx] = Mat::zeros(A.rows, A.cols, CV_8U); 
        Mat diff(aBGRA[idx] != bBGRA[idx]); // create mask where A & B differ
        vector<Point> nonZero;
        cv::findNonZero(diff, nonZero); // collect list of points where A & B differ
        // for each different point in this channel
        for (auto itr = nonZero.begin(); itr != nonZero.end(); ++itr)
        {
           Point p(*itr);
           // set cBGR at point to the absolute difference between A and B at this point
           cBGR[idx].at<uint8_T>(p) = abs(aBGRA[idx].at<uint8_t>(p) - bBGRA[idx].at<uint8_t>(p)); 
        }
    }
    Mat C;
    cv::merge(cBGR, 3, C); // merge BGR channels into C
    

    【讨论】:

    • 我的问题是针对 Java 的,因为我使用的是 Android Studio。您介意解释一下您的代码吗?
    • 抱歉,我不懂Java。我添加了 cmets 来解释代码。我认为 Java OpenCV 具有与 C++ OpenCV 相同的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2018-05-01
    • 2011-12-22
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多