gpt4 book ai didi

opencv - 如何使用Opencv获得两个图像之间的异同

转载 作者:行者123 更新时间:2023-12-02 16:56:17 24 4
gpt4 key购买 nike

我想比较两个图像并找到图像的相同和不同部分。我厌倦了“cv::compare and cv::absdiff” 方法,但感到困惑的是哪种方法可以满足我的情况。两者都显示出不同的结果。那么我怎样才能完成我想要的任务呢?

最佳答案

这是一个示例,您可以使用cv::absdiff查找图像相似性:

int main()
{
cv::Mat input1 = cv::imread("../inputData/Similar1.png");
cv::Mat input2 = cv::imread("../inputData/Similar2.png");

cv::Mat diff;
cv::absdiff(input1, input2, diff);

cv::Mat diff1Channel;
// WARNING: this will weight channels differently! - instead you might want some different metric here. e.g. (R+B+G)/3 or MAX(R,G,B)
cv::cvtColor(diff, diff1Channel, CV_BGR2GRAY);

float threshold = 30; // pixel may differ only up to "threshold" to count as being "similar"

cv::Mat mask = diff1Channel < threshold;

cv::imshow("similar in both images" , mask);

// use similar regions in new image: Use black as background
cv::Mat similarRegions(input1.size(), input1.type(), cv::Scalar::all(0));

// copy masked area
input1.copyTo(similarRegions, mask);


cv::imshow("input1", input1);
cv::imshow("input2", input2);
cv::imshow("similar regions", similarRegions);
cv::imwrite("../outputData/Similar_result.png", similarRegions);
cv::waitKey(0);
return 0;
}

使用这两个输入:

enter image description here

enter image description here

您将观察到输出(黑色背景):

enter image description here

关于opencv - 如何使用Opencv获得两个图像之间的异同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32583873/

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