如何用OpenCV Python精准去除图像中的棋盘格图案?
问题:OpenCV去除图像中棋盘格图案的优化方案
背景需求
需使用OpenCV去除目标图像中的棋盘格图案,明确该需求属于模板匹配范畴,计划通过Canny等滤波器处理图像与模板后进行滑动匹配。
已尝试情况
尝试过两种OpenCV目标去除方案,但效果不理想,处理后的图像存在棋盘格未完全清除等问题。
当前实现代码
import cv2 import numpy as np # Resizes a image and maintains aspect ratio def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA): # Grab the image size and initialize dimensions dim = None (h, w) = image.shape[:2] # Return original image if no need to resize if width is None and height is None: return image # We are resizing height if width is none if width is None: # Calculate the ratio of the height and construct the dimensions r = height / float(h) dim = (int(w * r), height) # We are resizing width if height is none else: # Calculate the ratio of the width and construct the dimensions r = width / float(w) dim = (width, int(h * r)) # Return the resized image return cv2.resize(image, dim, interpolation=inter) # Load template, convert to grayscale, perform canny edge detection template = cv2.imread('C:\\Users\Quirino\Desktop\Reti\Bounding_box\Checkboard.jpg') template = cv2.resize(template, (640,480)) template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) template = cv2.Canny(template, 50, 200) (tH, tW) = template.shape[:2] # cv2.imshow("template", template) # Load original image, convert to grayscale original_image = cv2.imread('F:\\ARCHAIDE\Appearance\Data_Archaide_Complete\MTL_G6\MTL_G6_MMO090.jpg') # original_image = cv2.resize(original_image, (640,480)) final = original_image.copy() gray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY) found = None # Dynamically rescale image for better template matching for scale in np.linspace(0.2, 1.0, 20)[::-1]: # Resize image to scale and keep track of ratio resized = maintain_aspect_ratio_resize(gray, width=int(gray.shape[1] * scale)) r = gray.shape[1] / float(resized.shape[1]) # Stop if template image size is larger than resized image if resized.shape[0] < tH or resized.shape[1] < tW: break # Detect edges in resized image and apply template matching canny = cv2.Canny(resized, 50, 200) detected = cv2.matchTemplate(canny, template, cv2.TM_CCOEFF) (_, max_val, _, max_loc) = cv2.minMaxLoc(detected) # Uncomment this section for visualization ''' clone = np.dstack([canny, canny, canny]) cv2.rectangle(clone, (max_loc[0], max_loc[1]), (max_loc[0] + tW, max_loc[1] + tH), (0,255,0), 2) cv2.imshow('visualize', clone) cv2.waitKey(0) ''' # Keep track of correlation value # Higher correlation means better match if found is None or max_val > found[0]: found = (max_val, max_loc, r) # Compute coordinates of bounding box (_, max_loc, r) = found (start_x, start_y) = (int(max_loc[0] * r), int(max_loc[1] * r)) (end_x, end_y) = (int((max_loc[0] + tW) * r), int((max_loc[1] + tH) * r)) original_image = cv2.resize(original_image, (640,480)) # Draw bounding box on ROI to remove cv2.rectangle(original_image, (start_x, start_y), (end_x, end_y), (0,255,0), 2) cv2.imshow('detected', original_image) # Erase unwanted ROI (Fill ROI with white) cv2.rectangle(final, (start_x, start_y), (end_x, end_y), (255,255,255), -1) final = cv2.resize(final, (640,480)) cv2.imshow('final', final) cv2.waitKey(0)
20230207 更新
采用相关优化方法后,80%的图像处理效果达标,但仍存在两类问题:
- 误识别并遮挡无关区域
- 仅部分覆盖棋盘格,无法完全清除
寻求帮助
请问还有哪些可尝试的优化方案?
内容的提问来源于stack exchange,提问作者caesar753
相关产品推荐
相关产品推荐

