如何为具有不同颜色的相同文本找到相同的 ROI?
How to find the same ROI for the same text with different colors?
我试图在这两张图片上找到 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 区域?
将图像转换为灰度时,白色和蓝色文本会得到不同的灰度值。因此,cv2.GaussianBlur
会给出不同的结果,cv2.adaptiveThreshold
也会给出不同的结果。最后找到的轮廓是不一样的,跟着ROI。
不要在这里转换为灰度!在你原来的三通道图像中,屏蔽任何不是背景的东西,它是纯灰色 (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
----------------------------------------
我试图在这两张图片上找到 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 区域?
将图像转换为灰度时,白色和蓝色文本会得到不同的灰度值。因此,cv2.GaussianBlur
会给出不同的结果,cv2.adaptiveThreshold
也会给出不同的结果。最后找到的轮廓是不一样的,跟着ROI。
不要在这里转换为灰度!在你原来的三通道图像中,屏蔽任何不是背景的东西,它是纯灰色 (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
----------------------------------------