图像处理 切割与图案不同的区域
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]
我直接得到了位差,但它没有给我我想要的东西,我尝试了 medianBlur 和类似的东西来像第 4 个图像那样做,但我无法像第 4 个图像那样使它清晰。
(我用绘画手动创建了第 4 个图像。)
这是一个具有挑战性的问题,因为模式是故意设计的,很难用软件解决。
我建议采取以下步骤:
- 将
img
和 pattern
转换为二进制图像(灰度级别不是数字的一部分)。
- 计算
img
和 pattern
的绝对差。
- 应用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()
结果:
如您所见,解决方案并不完美,但要获得完美的结果非常具有挑战性...
嗨,
我想得到第一张图片和第二张图片的区别,
我想从图像中剪切数字。
我得到像素之间的差异,但结果是:
但我想要的是:
能不能把图剪成这样?
这是我所做的:
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]
我直接得到了位差,但它没有给我我想要的东西,我尝试了 medianBlur 和类似的东西来像第 4 个图像那样做,但我无法像第 4 个图像那样使它清晰。
(我用绘画手动创建了第 4 个图像。)
这是一个具有挑战性的问题,因为模式是故意设计的,很难用软件解决。
我建议采取以下步骤:
- 将
img
和pattern
转换为二进制图像(灰度级别不是数字的一部分)。 - 计算
img
和pattern
的绝对差。 - 应用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()
结果:
如您所见,解决方案并不完美,但要获得完美的结果非常具有挑战性...