当我在互联网上搜索一个校正亮度的算法时,我发现了这个关于前瞻性校正和回顾性校正的文章。我对未来的修正很感兴趣。基本上,我们拍摄的场景中有图像(原始的一个)和另外两个,一个明亮的和一个黑暗的照片,我们只看到原始图片的背景。
我的问题是在openCV或代码示例中找不到对这些公式的任何修改。我试图使用这些公式,因为它们在我的代码中,但这一次我遇到了数据类型的问题。这发生在我试图通过对图像应用操作来找到C常量时。
下面是我在代码中实现公式的方式:
def calculate_C(im, im_b):
fx_mean = cv.mean(im)
fx_over_bx = np.divide(im,im_b)
mean_fx_bx = cv.mean(fx_over_bx)
c = np.divide(fx_mean, mean_fx_bx)
return c
#Basic image reading and resizing
# Original image
img = cv.imread(image_path)
img = cv.resize(img, (1000,750))
# Bright image
b_img = cv.imread(bright_image_path)
b_img = cv.resize(b_img, (1000,750))
# Calculating C constant from the formula
c_constant = calculate_C(img, b_img)
# Because I have only the bright image I am using second formula from the article
img = np.multiply(np.divide(img,b_img), c_constant)当我试图运行这段代码时,我会得到以下错误:
img = np.multiply(np.divide(img,b_img),c_constant) ValueError:操作数不能与形状一起广播(750,1000,3) (4,
那么,我能做些什么来修复我的代码吗?或者有什么提示可以和我分享,用这种方法或更好的方法来处理亮度校正?
发布于 2022-09-07 23:34:57
您使用的是cv2.means函数,它为每个通道返回带有形状(4,)-均值的数组。您可能需要忽略最后一个频道,并将其正确地广播给numpy。
或者你可以用numpy来计算,而不是opencv。
我只是以提供的文章中的图片为例。
grain.png:

grain_background.png:

完整的例子:
import cv2
import numpy as np
from numpy.ma import divide, mean
f = cv2.imread("grain.png")
b = cv2.imread("grain_background.png")
f = f.astype(np.float32)
b = b.astype(np.float32)
C = mean(f) / divide(f, b).mean()
g = divide(f, b) * C
g = g.astype(np.uint8)
cv2.imwrite("grain_out.png", g)您需要使用掩码除法操作,因为普通操作可能导致零=> nan值的除法。
结果图像(output.png):

https://stackoverflow.com/questions/73618021
复制相似问题