使用 openCV 从一幅图像中减去另一幅图像

发布于 2024-08-27 00:34:47 字数 77 浏览 6 评论 0原文

如何使用 openCV 从一幅图像中减去另一幅图像?

Ps.:我无法使用 python 实现,因为我必须用 C++ 来实现

How can I subtract one image from another using openCV?

Ps.: I coudn't use the python implementation because I'll have to do it in C++

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

思念满溢 2024-09-03 00:34:47
#include <cv.h>
#include <highgui.h>

using namespace cv;

Mat im = imread("cameraman.tif");
Mat im2 = imread("lena.tif");

Mat diff_im = im - im2;

更改图像名称。还要确保它们的尺寸相同。

#include <cv.h>
#include <highgui.h>

using namespace cv;

Mat im = imread("cameraman.tif");
Mat im2 = imread("lena.tif");

Mat diff_im = im - im2;

Change the image names. Also make sure they have the same size.

小鸟爱天空丶 2024-09-03 00:34:47

我宁愿建议使用 OpenCV 方法 cv::absdiff,而不是使用 diff 或只是简单的减法 im1-im2

using namespace cv;
Mat im1 = imread("image1.jpg");
Mat im2 = imread("image2.jpg");
Mat diff;
absdiff(im1, im2, diff);

因为图像通常使用无符号存储格式,@Dat 和 @ssh99 的减法方法将消除所有负差异。例如,如果 BMP 图像的某些像素对于 im1[70, 80, 90] 的值为 [20, 50, 30]对于 im2,同时使用 im1 - im2diff(im1, im2, diff) 将产生值 [0,0,0 ],因为 20-70 = -5050-80 = -3030-90 = -60 以及所有负结果将转换为无符号值 0,在大多数情况下,这不是您想要的。方法 absdiff 将计算所有减法的绝对值,从而产生更合理的 [50,30,60]

Instead of using diff or just plain subtraction im1-im2 I would rather suggest the OpenCV method cv::absdiff

using namespace cv;
Mat im1 = imread("image1.jpg");
Mat im2 = imread("image2.jpg");
Mat diff;
absdiff(im1, im2, diff);

Since images are usually stored using unsigned formats, the subtraction methods of @Dat and @ssh99 will kill all the negative differences. For example, if some pixel of a BMP image has value [20, 50, 30] for im1 and [70, 80, 90] for im2, using both im1 - im2 and diff(im1, im2, diff) will produce value [0,0,0], since 20-70 = -50, 50-80 = -30, 30-90 = -60 and all negative results will be converted to unsigned value of 0, which, in most cases, is not what you want. Method absdiff will instead calculate the absolute values of all subtractions, thus producing more reasonable [50,30,60].

你列表最软的妹 2024-09-03 00:34:47

使用 cv::subtract() 方法。

Mat img1=some_img;
Mat img2=some_img;

Mat dest;

cv::subtract(img1,img2,dest); 

这将执行 (img1-img2) 的元素减法。
您可以找到有关它的更多详细信息 http://docs.opencv.org/modules/核心/doc/operations_on_arrays.html

use cv::subtract() method.

Mat img1=some_img;
Mat img2=some_img;

Mat dest;

cv::subtract(img1,img2,dest); 

This performs elementwise subtract of (img1-img2).
you can find more details about it http://docs.opencv.org/modules/core/doc/operations_on_arrays.html

孤独患者 2024-09-03 00:34:47

使用 LoadImage 将图像加载到内存中,然后使用 方法。

此链接包含一些示例代码,如果有帮助的话:http://permalink。 gmane.org/gmane.comp.lib.opencv/36167

Use LoadImage to load your images into memory, then use the Sub method.

This link contains some example code, if that will help: http://permalink.gmane.org/gmane.comp.lib.opencv/36167

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文