首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算对比图

计算对比图
EN

Stack Overflow用户
提问于 2021-02-25 16:57:11
回答 1查看 429关注 0票数 2

我试图计算NxN窗口中每个像素周围的对比度,并将结果保存在新图像中,其中新图像中的每个像素都是旧图像周围区域的对比度。从另一篇文章中我得到了这个:

代码语言:javascript
复制
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.

目前,我有以下几点:

代码语言:javascript
复制
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的大小。有什么更好的方法来做我想做的事吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-25 19:44:01

您可以使用扩张侵蚀形态学操作来查找NxN邻域的最大值和最小值。

  • 膨胀 of NxN等价于NxN邻域的极大
  • 腐蚀 of NxN等价于NxN邻域的最小

使用形态学操作使解决方案比“手动”将图像分割成小块要简单得多。

您可以使用以下阶段:

  • 转换到实验室的颜色空间,得到L通道。
  • 使用“扩展”形态学操作(扩展相当于在NxN邻域中找到最大像素)。
  • 使用“腐蚀”形态操作(膨胀相当于在NxN邻域中找到最大像素)。
  • 将图像转换为浮动类型(在使用除法操作之前需要)。
  • 计算对比图(对比度图的范围为0,1)。
  • 将对比度映射转换为带有四舍五入的uint8 --转换的准确性降低了,因此我不能推荐它(但我假设您需要转换才能将输出作为图像)。

下面是一个完整的代码示例:

代码语言:javascript
复制
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()

输入图像:

L图像:

img_max

img_min

对比图img_contrast_uint8

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66373003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档