【问题标题】:How to find the same ROI for the same text with different colors?如何为不同颜色的相同文本找到相同的 ROI?
【发布时间】:2021-04-01 18:35:58
【问题描述】:

我正在尝试在这两张图片上找到 ROI:

我正在将此代码用于图像 #1:

image_1 = image1
corr1 = []
gray = cv2.cvtColor(image_1, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (1,1), 1)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,10)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
dilate = cv2.dilate(thresh, kernel, iterations=3)

cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
ROI_numbers1 = 0
ROI1 = []
for c in cnts:
    area = cv2.contourArea(c)
    if area > 5:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image_1, (x, y), (x + w, y + h), (0,255,0), 1)
        ROI1.append(image_1[y:y+h, x:x+w])
        corr1.append([y,y+h, x,x+w])
        ROI_numbers1 += 1

图片 #2 的代码如下:

image_2 = image2
corr2 = []
gray = cv2.cvtColor(image_2, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (1,1), 1)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,10)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
dilate = cv2.dilate(thresh, kernel, iterations=3)

cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
ROI_numbers2 = 0
ROI2 = []
for c in cnts:
    area = cv2.contourArea(c)
    if area > 5:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image_2, (x, y), (x + w, y + h), (0,255,0), 1)
        ROI2.append(image_2[y:y+h, x:x+w])
        corr2.append([y,y+h, x,x+w])
        ROI_numbers2 += 1

使用 OpenCV 显示 ROI 后,我得到了:

为什么图像 #1 中蓝色文本的 ROI 区域小于图像 #2 中白色文本的 ROI 区域?

【问题讨论】:

  • @HansHirse 如果可能的话,你能提出一些解决方案吗?

标签: python opencv roi


【解决方案1】:

将图像转换为灰度时,您将获得白色和蓝色文本的不同灰度值。因此,cv2.GaussianBlur 将给出不同的结果,并且也遵循cv2.adaptiveThreshold。最后,找到的轮廓是不同的,跟随 ROIs。

这里不要转换成灰度!在您的原始三通道图像中,遮盖除背景以外的任何内容,即纯灰色 (53, 53, 53)。那个面具取代了你的thresh。然后,您可以从那里使用现有的实现。

这里有一个最小的例子来检查生成的边界矩形 (ROI) 是否相同:

import cv2
import numpy as np


def cnts_from_image(image):
    thresh = (~np.all(image == (53, 53, 53), axis=2)).astype(np.uint8) * 255
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    dilate = cv2.dilate(thresh, kernel, iterations=3)
    cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    return cnts


rects_white = [cv2.boundingRect(c) for c in cnts_from_image(cv2.imread('white_text.png'))]
rects_blue = [cv2.boundingRect(c) for c in cnts_from_image(cv2.imread('blue_text.png'))]

print('All rectangles identical:', np.all([rw == rb for rw, rb in zip(rects_white, rects_blue)]))
# All rectangles identical: True
----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
NumPy:         1.20.2
OpenCV:        4.5.1
----------------------------------------

【讨论】:

  • 它适用于灰色背景,但不适用于其他任何背景。我应该在 (53, 53, 53) 中手动输入不同的值来检查不同的背景颜色吗?
  • @animesh 没错,你需要根据背景颜色调整这个RGB/BGR值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
相关资源
最近更新 更多