【发布时间】: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 如果可能的话,你能提出一些解决方案吗?