如何在两个图像中访问每个像素并使用OpenCV找到它们的绝对差异?(Android Studio)



我有两个垫子图像,它们是Android Studio中的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原始类型中。由于我是Java和OpenCV的新手,我不确定这意味着什么?

谢谢

这是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

相关内容

最新更新