繁体   English   中英

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

[英]Image Processing Cut an area that is different from a pattern

在此处输入图片说明

在此处输入图片说明

你好,

我想区分第一张图片和第二张图片,

我想从图像中删除数字。

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

在此处输入图片说明

但我想要的是:

在此处输入图片说明

可以像这样剪切图像吗?

这是我所做的:

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 张图像那样使其清晰。
(我用油漆手动创建了第四张图像。)

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

我建议采取以下步骤:

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

这是代码:

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

结果:
在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM