图像处理切出与图案不同的区域

问题描述 投票:0回答:1

enter image description here

enter image description here

嗨,

我想获得第一张图像和第二张图像之间的差异,

我想从图像上剪掉数字。

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

enter image description here

但是我想要的是:

enter image description here

是否可以像这样剪切图像?

这是我所做的:

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]

我直接得到了一些区别,但是它没有满足我的要求,我尝试了位数模糊和类似的方法来像第4张图像一样进行处理,但是我无法使其像第4张图像一样清晰。 (我用油漆手动创建了第四个图像。)

python image image-processing python-imaging-library crop
1个回答
2
投票

这是一个具有挑战性的问题,因为该模式是经过精心设计的,因此很难用软件解决。

我建议执行以下步骤:

  • 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()

结果:enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.