如何使用 OpenCV (Python) 在不损失质量的情况下更改特定颜色?
How to use OpenCV (Python) to change specific colors without loss of quality?
所以,我有 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 将所有红色变为白色,将所有其他颜色变为黑色,尽可能少 "blockage"?
这是一个应该相当有效的方法:
结果:
代码:
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)
所以,我有 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 将所有红色变为白色,将所有其他颜色变为黑色,尽可能少 "blockage"?
这是一个应该相当有效的方法:
结果:
代码:
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)