OpenCV Python图像冲洗

近江

我正在尝试使用以下代码来显示平均合并图像:

import numpy as np
import cv2
import matplotlib.pyplot as plt

dolphin=cv2.imread('dolphin.png',0) #Also tried without the 0
bicycle=cv2.imread('bicycle.png',0)

自行车-原始 海豚-原创

以下代码将两个图像相加,结果与本课程中显示的结果相同。但是简单的加法avg = img1 + img2不起作用。

简单添加-冲洗区域

sumimg=cv2.add(dolphin,bicycle)
cv2.imshow('Sum image', sumimg)

将两个图像添加在一起而没有任何修改-清除区域是由于该元素的添加超过255,因此该值设置为255

cv2.waitKey(0)
cv2.destroyAllWindows()

以下代码仅给我一张白色图片。当我尝试显示半强度的海豚或周期时...结果相同,除了一些黑点

将图像除以2

avgimg=cv2.add(dolphin/2,bicycle/2)

由avgimg = img1 / 2 + img2 / 2获得的相同结果

cv2.imshow('Avg image', avgimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

Udacity课程显示,如果您将图像除以2,则应该得到以下信息: 从Udacity课程-除以2后添加了两个图像

So the question is: When I divide either of the image by 2, the matrix contains values below 255 and addition of two matrices also contains values below 255, why then is the resulting image a complete washout?

DarkCygnus

If you wish to add both images into one (so they both appear on the resulting image), with each one of the inputs averaged, you should use the addWeighted() method, like this (taken from the docs):

import numpy as np
import cv2

#load your images
dolphin = cv2.imread('dolphin.png') #use 0 for grayscale
bicycle = cv2.imread('bicycle.png') 
#add them with a weight, respectively, last parameter is a scalar added 
dst = cv2.addWeighted(dolphin,0.7,bicycle,0.3,0) 

#show
cv2.imshow('Blended Image',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note: As also mentioned in the previous link, it is important to notice that numpy and OpenCV addition are different, as numpy has a modulo operation (%) while OpenCV has a saturated operation (caps at a maximum), to clarify we have this extracted example from that link:

>>> x = np.uint8([250])
>>> y = np.uint8([10])

>>> print( cv2.add(x,y) ) # 250+10 = 260 => 255 , saturated
[[255]]
>>> print( x+y )          # 250+10 = 260 % 256 = 4 , modulo
[4]

这可能是为什么您改用add()方法获得白色图像的原因(所有像素上限为255,并显示白色)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章