我试图在NxN
窗口中计算每个像素周围的对比度,并将结果保存在新图像中,其中新图像中的每个像素是旧图像中其周围区域的对比度。从另一个帖子中我得到了以下信息:
1) Convert the image to say LAB and get the L channel
2) Compute the max for an NxN neighborhood around each pixel
3) Compute the min for an NxN neighborhood around each pixel
4) Compute the contrast from the equation above at each pixel.
5) Insert the contrast as a pixel value in new image.
目前我有以下几点:
def cmap(roi):
max = roi.reshape((roi.shape[0] * roi.shape[1], 3)).max(axis=0)
min = roi.reshape((roi.shape[0] * roi.shape[1], 3)).min(axis=0)
contrast = (max - min) / (max + min)
return contrast
def cm(img):
# convert to LAB color space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# separate channels
L, A, B = cv2.split(lab)
img_shape = L.shape
size = 5
shape = (L.shape[0] - size + 1, L.shape[1] - size + 1, size, size)
strides = 2 * L.strides
patches = np.lib.stride_tricks.as_strided(L, shape=shape, strides=strides)
patches = patches.reshape(-1, size, size)
output_img = np.array([cmap(roi) for roi in patches])
cv2.imwrite("labtest.png", output_img)
代码抱怨roi的大小。有更好的(Python式)方法来做我想做的事吗
可以使用Dilation和Erosion形态学运算来查找NxN邻域的最大值和最小值
使用形态学运算使解决方案比“手动”将图像分割成小块要简单得多
您可以使用以下阶段:
以下是完整的代码示例:
输入图像:
L
图像:img_max
:img_min
:对比图
img_contrast_uint8
:相关问题 更多 >
编程相关推荐