use*_*362 2 python opencv image-processing contrast
我正在尝试计算窗口中每个像素周围的对比度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.
Run Code Online (Sandbox Code Playgroud)
目前我有以下内容:
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.
Run Code Online (Sandbox Code Playgroud)
该代码抱怨 roi 的大小。有没有更好的(Pythonic)方式来做我想做的事?
您可以使用扩张和侵蚀形态学运算来查找 NxN 邻域的最大值和最小值。
使用形态学运算使解决方案比“手动”将图像划分为小块要简单得多。
您可以使用以下阶段:
这是完整的代码示例:
import numpy as np
import cv2
size_n = 5 # NxN neighborhood around each pixel
# Read input image
img = cv2.imread('chelsea.png')
# Convert to LAB color space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# Get the L channel
L = lab[:, :, 0]
# Use "dilate" morphological operation (dilate is equivalent to finding maximum pixel in NxN neighborhood)
img_max = cv2.morphologyEx(L, cv2.MORPH_DILATE, np.ones((size_n, size_n)))
# Use "erode" morphological operation (dilate is equivalent to finding maximum pixel in NxN neighborhood)
img_min = cv2.morphologyEx(L, cv2.MORPH_ERODE, np.ones((size_n, size_n)))
# Convert to type float (required before using division operation)
img_max = img_max.astype(float)
img_min = img_min.astype(float)
# Compute contrast map (range of img_contrast is [0, 1])
img_contrast = (img_max - img_min) / (img_max + img_min)
# Convert contrast map to type uint8 with rounding - the conversion loosed accuracy, so I can't recommend it.
# Note: img_contrast_uint8 is scaled by 255 (scaled by 255 relative to the original formula).
img_contrast_uint8 = np.round(img_contrast*255).astype(np.uint8)
# Show img_contrast as output
cv2.imshow('img_contrast', img_contrast_uint8)
cv2.waitKey()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
915 次 |
最近记录: |