gpt4 book ai didi

java - 如何使用 OpenCV 访问两个图像中的每个像素并找到它们的绝对差异? (安卓工作室)

转载 作者:行者123 更新时间:2023-12-02 13:00:35 24 4
gpt4 key购买 nike

我有两个 Mat 图像,在 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 Primitive 类型。由于我对 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

关于java - 如何使用 OpenCV 访问两个图像中的每个像素并找到它们的绝对差异? (安卓工作室),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44318208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com