我已经使用MatPlotLib在Python中阅读了一个单词的图片,将其反转,并希望将所有白色区域变大以增强单词的强度。我该怎么办?
我的假设是,我需要查看白色像素 r 范围内的所有像素,并将它们也着色为白色。这是正确的吗?
请在下面找到代码,示例和所需结果。图片的尺寸为(465,748)和
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import pandas as pd
# Code to transform RGB-image into GrayScale and subsequently into inverted BW image.
def rgb2gray(rgb):
# Convert RGB to GrayScale
gray_value = np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
# Invert GrayScale.
gray_value = 1 - gray_value
# Apply a threshold.
gray_value[gray_value >= 0.5] = 1
gray_value[gray_value < 0.5] = 0
return gray_value
# Read in image.
img = mpimg.imread('Data/Development/Image_Test.png')
# BW-scale.
gray = rgb2gray(img)
# Show image.
plt.imshow(gray, cmap = plt.get_cmap('gray'))
源图像:here
答案 0 :(得分:0)
解决方案是让白色区域dilate。在此解决方案中,通过让膨胀运行10次迭代,我得到了 r = 10个像素。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import pandas as pd
# Code to transform RGB-image into GrayScale and subsequently into inverted BW image.
def rgb2gray(rgb):
# Convert RGB to GrayScale
gray_value = np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
# Invert GrayScale.
gray_value = 1 - gray_value
# Apply a threshold.
gray_value[gray_value >= 0.5] = 1
gray_value[gray_value < 0.5] = 0
return gray_value
# Read in image.
img = mpimg.imread('Data/Development/Image_Test.png')
# BW-scale.
gray = rgb2gray(img)
# Dilate the white areas.
gray = ndimage.binary_dilation( gray, iterations = 10 ).astype( gray.dtype )
# Show image.
plt.imshow(gray, cmap = plt.get_cmap('gray'))