使用opencv减去图像 [英] Subtract images by using opencv

查看:159
本文介绍了使用opencv减去图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要减去两个图片。我的问题是cvSub()函数饱和。我想要做的是:



1)将原始图片转换为灰度。





3)减去图像(值从-255到255) - >使用cvSub()重新缩放的问题。 / p>

4)通过乘以0.5并添加128来重新缩放。



我想改变灰度图像8比特到16比特,但是一切都变得更糟,它变成了许多行代码,最后它没有成功。

解决方案

p>使用openCV,您可以执行以下操作:




  • 将源图像(1和2)缩放0.5倍。两张图片现在都在范围[0..127]

  • 将图片1移动128.它现在在[128..255]范围内

  • 从图像1中减去图像2



这样,不需要范围转换,结果完全缩放到8位。使用 cvConvertScale 前两个操作。



这样的:

  ... 
cvConvertScale(src1,tmp1,0.5,128);
cvConvertScale(src2,tmp2,0.5,0);
cvSub(tmp1,tmp2,dst);

编辑



您对丢失信息(精确度)的评论是正确的,但是当使用整数数学除法时,您总是这样做。在你的情况下,缩放就是这样。简单地认为它把所有的位向右移动一个地方。因此,最后一点信息丢失了。



另一方面,应用操作的顺序也很重要。除以2,可为每个像素引入 0.5 的舍入(或截断)误差。如果在减去两个输入图像之前缩放,则舍入误差加起来 1.0 。这会在结果图像中显示,因为一些像素偏离1,与使用初始和Alexanders方法得到的结果相比。但这是对于更简单的解决方案的折衷,而不将图像扩展为16位或浮点。



查看此示例:



实数: >

(200 - 101)/ 2 = 99/2 = 49.5



解决方案(整数数学):

(200 - 101)/ 2 = 99/2 = 49 b $ b

我的解决方案(整数数学):

(200/2) - (101/2)= 100 - 50 = 50


I want to subtract two images. My problem ist that the cvSub()-function saturates. What I want to do is:

1) Convert the original images to grayscale.

2) Take the grayscale-images (values from 0-255).

3) Subtract the images (values from -255 to 255) -> problem of rescaling using cvSub().

4) Rescale by multiplying with 0.5 and adding 128.

I thought of changing the gray-scale images from 8bit to 16bit, but then everything got worse and it became many lines of code and in the end it didn't work out.

解决方案

Using openCV you could do the following:

  • scale both source images (1 and 2) by factor 0.5. Both images are now in range [0..127]
  • shift image 1 by 128. It now is in the range [128..255]
  • subtract image 2 from image 1

This way, no range conversion is needed and the result is fully scaled to 8 bit. Use the cvConvertScale for the first two operations.

Something like this:

//...
cvConvertScale(src1, tmp1, 0.5, 128);
cvConvertScale(src2, tmp2, 0.5, 0);
cvSub(tmp1, tmp2, dst);

EDIT:

To your comment on loosing information(precision), you are right, but you always do when dividing using integer math. And scaling in your case is just that. Simply think of it as shifting all the bits to the right by one place. So the last bit of information is lost.

On the other hand, the order of the applied operations is also important. By dividing by 2 you introduce a rounding (or truncation) error of 0.5 for every pixel. If you scale both input images before subtracting them, the rounding error adds up to 1.0. This shows up in the result image as some pixels being off by 1 compared to the result you would get with your initial and Alexanders approach. But that is the tradeoff for the simpler solution without expanding the image to 16-bit or floating point.

See this example:

real numbers:
(200 - 101) / 2 = 99 / 2 = 49.5

Alexanders solution (integer math):
(200 - 101) / 2 = 99 /2 = 49

my Solution (integer math):
(200 / 2) - (101 / 2) = 100 - 50 = 50

这篇关于使用opencv减去图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆