python - 在图像锐化中进行相同操作后得到不同的图像阵列

标签 python numpy opencv image-processing computer-vision

我正在尝试通过应用不锐化 mask 来锐化图像,即用高斯模糊图像减去图像,然后将差异添加回图像。

这是我运行的代码:

 img = cv2.imread('redhat.jpg')
 gauss = cv2.GaussianBlur(img,(7,7),0)
 diff = img - gauss
 sharp = img + diff
 cv2_imshow(img)
 cv2_imshow(sharp)

原图:

enter image description here

锐利:

enter image description here

如果我运行:而不是上面的代码:

 img = cv2.imread('redhat.jpg')
 gauss = cv2.GaussianBlur(img,(7,7),0)
 sharp = cv2.addWeighted(img, 2, gauss, -1, 0)
 cv2_imshow(img)
 cv2_imshow(sharp)

然后我就得到了正确的清晰图像:

enter image description here

有人可以向我解释为什么我在第一个代码中得到奇怪的结果吗?根据我的理解,这两段代码都在执行相同的数学运算。

最佳答案

diff = img - gauss 中,减法产生负值,但两个输入的类型为 uint8,因此运算结果被强制为相同类型,该类型不能容纳负值.

您必须将其中一张图像转换为签名类型才能正常工作。例如:

gauss = cv2.GaussianBlur(img,(7,7),0)
diff = img.astype(np.int_) - gauss
sharp = np.clip(img + diff, 0, 255).astype(np.uint8)

使用cv2.addWeighted()效率更高。

关于python - 在图像锐化中进行相同操作后得到不同的图像阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75258632/

相关文章:

javascript - Django 表单 : How to edit fields that aren't on the model itself

python - 对称函数的 numpy.gradient 的分量不同

c++ - opencv:如何用距中心的距离填充椭圆形状

android - 发现错误 "This release is not compliant with the Google Play 64-bit requirement(Opencv lib)"

python - 将包含另一个具有多个值的字典列表的字典列表转换为数据帧

python - 禁用 Internet Explorer 的 Remote WebDriver native 事件

python - 为什么在运行时python show file not found 错误?

python - 如何在 python 中创建自己的数据类型以便覆盖算术运算符?

python - 为什么 “np.inf//2” 导致 NaN 而不是无穷大?

c++ - 使用 OpenCV 将帧转换为就像从上方获取的一样