【问题标题】:Change pixels for improve contrast in picture更改像素以提高图片的对比度
【发布时间】:2020-04-21 01:42:37
【问题描述】:

我输入了带有隐藏文本的图像文件,问题是隐藏文本的像素差异非常小,有时只有 1px。我想更改像素以查看此文本。

因为从不使用类似的想法将图像转换为 numpy 数组并用 dict 替换值:

from PIL import Image
import matplotlib

img = Image.open('4YtCA.jpg')
data = np.array( img, dtype='uint8' )
#print (data)

a = np.ravel(data)
u, c = np.unique(a, return_counts=True)
print (u)
[ 48  49  50  51  77  78  79  80 100 101 102 103 121 122 123 124 142 143
 144 145 164 165 166 167 188 189 190 191 208 209 210 211 212 230 231 232
 233 253 254 255]

#new values for replace
new = (u.reshape(-1, 4) / [1,2,3,4]).astype(int)
print (new)
[[ 48  24  16  12]
 [ 77  39  26  20]
 [100  50  34  25]
 [121  61  41  31]
 [142  71  48  36]
 [164  82  55  41]
 [188  94  63  47]
 [208 104  70  52]
 [212 115  77  58]
 [233 126  84  63]]

d = dict(zip(u, np.ravel(new)))
#print (d)

#https://stackoverflow.com/a/46868996
indexer = np.array([d.get(i, -1) for i in range(data.min(), data.max() + 1)])

out = indexer[(data - data.min())]

matplotlib.image.imsave('out.png', out.astype(np.uint8))

我认为我的解决方案不是很好,因为最后一个值没有很好地看到。是否可以将像素更改为一些不同的颜色,如红色、绿色、紫色?或者以更好的方式更改合同?最好的办法是用一些巧妙的方式改变每个像素,但不知道如何。

输入图片:

输出图像:

【问题讨论】:

    标签: python image numpy replace python-imaging-library


    【解决方案1】:

    您可以尝试直方图均衡。我现在将在终端中使用 ImageMagick 来演示:

    magick hidden.jpg -equalize -rotate -90 result.png
    

    “局部自适应阈值” - 见here

    magick hidden.jpg -lat 50x50 -rotate -90 result.png
    

    如果您正在运行 v6 ImageMagick,请将前面命令中的 magick 替换为 convert


    这在 Python 中是相当等价的:

    from PIL import Image 
    from skimage.filters import threshold_local 
    import numpy as np 
    
    # Open image in greyscale 
    im = Image.open('hidden.jpg').convert('L') 
    na = np.array(im) 
    
    # Local Adaptive Threshold 
    LAT = threshold_local(na, 49) 
    result = na > LAT 
    
    Image.fromarray((result*255).astype(np.uint8)).save('result.png') 
    

    如果您真的不想引入对skimage 的新依赖,您可以使用 PIL 或 Numpy 生成图像的模糊副本,然后从原始图像中减去模糊部分,然后对差异图像进行阈值处理。看起来像这样:

    #!/usr/bin/env python3
    
    from PIL import Image, ImageFilter
    import numpy as np
    
    # Open image in greyscale, and make heavily blurred copy
    im = Image.open('hidden.jpg').convert('L')
    blur = im.filter(ImageFilter.BoxBlur(25))
    
    # Go to Numpy for maths!
    na = np.array(im)
    nb = np.array(blur)
    
    # Local Adaptive Threshold
    res = na >= nb
    
    # Save
    Image.fromarray((res*255).astype(np.uint8)).save('result.png')
    

    【讨论】:

      【解决方案2】:
      from PIL import Image
      import numpy as np
      
      img = Image.open('4YtCA.jpg').convert('L') 
      
      data = np.array(img, dtype='uint8')
      u, c = np.unique(data, return_counts=True)
      
      # Set the background colors to white and the rest to black
      #data = np.where(np.isin(data, u[c>17000]), 255, 0).astype(np.uint8)
      data = np.isin(data, u[c>17000]).astype(np.uint8) * 255  # thanks to Mad Physicist
      
      # Create new image and save
      img_new = Image.fromarray(data)
      img_new.save('4YtCA_new.jpg')
      

      【讨论】:

      • 你在这里真的不需要where(它似乎是最被滥用的功能之一,没有明显的原因)。 np.isin(...).astype(np.uint8) * 255 可能是一个更快的选择。如果需要,您可以将乘法步骤分解为就地。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多