所以,我有this floor plan
我想改变它的颜色,这样我就可以用OCR识别房间号了。为此,我想执行以下操作:将所有红色更改为白色,将所有其他颜色更改为黑色,因此剩下的只是房间号。我想尝试阈值处理,但我在文档中看到它只能在灰度图像上执行,所以我首先运行以下代码对其进行灰度处理:
import cv2
import os
from ConvertSVG import svg_2_png
# Convert the SVG to a PNG
output_path = os.path.join('converted svgs', 'Andover HS Level 3.png')
svg_2_png(os.path.join('svg', 'Andover HS Level 3.svg'), output_path)
img = cv2.imread(output_path)
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Grayscale", gray_image)
cv2.waitKey(0)
然后我得到了this output
正如你所看到的,灰度确实起作用了,但房间号变得更阻塞,OCR更难识别。
如何使用OpenCV-python将所有的红色改为白色,将所有其他颜色改为黑色,并尽可能地减少“阻塞”?
发布于 2018-08-11 16:38:24
以下是一种应该运行得相当好的方法:
结果:
代码:
import cv2
import numpy as np
# load image in BGR
input_image = cv2.imread("floorplan.png").astype(np.float32) / 255.0
# get scalar redness - this is smooth and mostly correct,
# but it includes lots of stuff we don't want
redness = input_image[:, :, 2] - np.mean(input_image[:, :, :2], -1)
# create a blocky mask around the highly-red pixels
mask_coarse = cv2.dilate((redness > 0.7).astype(np.uint8), np.ones((3, 3)), iterations=5)
mask_fine = cv2.dilate(
(mask_coarse * (redness > 0.3)).astype(np.uint8), np.ones((3, 3)), iterations=2
)
# mask redness with the blocky mask
output = redness * mask_fine * 255
cv2.imwrite("mask.png", output)
https://stackoverflow.com/questions/51801087
复制相似问题