【问题标题】:Image Processing Cut an area that is different from a pattern图像处理 剪切与图案不同的区域
【发布时间】:2020-06-12 19:19:55
【问题描述】:

嗨,

我想得到第一张和第二张的区别,

我想从图像中删除数字。

我得到了像素之间的差异,但结果是:

但我想要的是:

可以这样剪图片吗?

这是我所做的:

import cv2 
import numpy as np
from PIL import Image
import pytesseract
import os
import sys

img = Image.open("recherche.png").convert("RGBA")
pattern = Image.open("pattern.png").convert("RGBA")

pixels = img.load()
pixelsPattern = pattern.load()

new = Image.open("new.png").convert("RGBA")
pixelNew = new.load()

for i in range(img.size[0]):
    for j in range(img.size[1]):
         if(pixels[i,j] != pixelsPattern[i,j]):
             pixelNew[i,j] = pixels[i,j]

我直接得到了位差,但它并没有给我我想要的东西,我尝试了中值模糊和类似的东西来像第四张图像那样做,但我无法让它像第四张图像一样清晰。
(我用油漆手动创建了第四张图像。)

【问题讨论】:

    标签: python image image-processing python-imaging-library crop


    【解决方案1】:

    这是一个具有挑战性的问题,因为该模式被故意设计为使其难以通过软件解决。

    我建议以下步骤:

    • imgpattern 转换为二值图像(灰度不是数字的一部分)。
    • 计算imgpattern 的绝对差。
    • 应用closing 形态运算来闭合小间隙。

    代码如下:

    import cv2
    import numpy as np
    
    # Read image and pattern as Grayscale images (output of cv2.imread is numpty array).
    img = cv2.imread("recherche.png", cv2.IMREAD_GRAYSCALE)
    pattern = cv2.imread("pattern.png", cv2.IMREAD_GRAYSCALE)
    
    # Convert img and pattern to binary images (all values above 1 goes to 255)
    _, img = cv2.threshold(img, 1, 255, cv2.THRESH_BINARY)
    _, pattern = cv2.threshold(pattern, 1, 255, cv2.THRESH_BINARY)
    
    # Compute absolute difference of img and pattern (result is 0 where equal and 255 when not equal)
    dif = cv2.absdiff(img, pattern)
    
    # Apply closing morphological operation
    dif = cv2.morphologyEx(dif, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)));
    
    dif = 255 - dif  # Inverse polarity
    
    # Display result
    cv2.imshow('dif', dif)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    结果:

    如您所见,解决方案并不完美,但获得完美的结果非常具有挑战性......

    【讨论】:

      猜你喜欢
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 2016-03-27
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多