AI Stack Exchange
2023-11-19 14:56 UTC
By Loris Simonetti
AI-110-20231119-social-media-9c4a5ddd
How to accurately detect grid cell boundaries in Python image processing?
I'm working on a Python algorithm to detect individual cells of a grid passed by an image. Currently, I'm facing an issue where the values inside each cell are being selected as contours along with the cells themselves. As you can see: Here's part of my current code: # Read the image image = cv2.imread(image_path) # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur to remove noise for simplifying grid line identification blur = cv2.GaussianBlur(gray, (5,5), 0) # Apply adaptive threshold to the image (to handle variations in brightness and contrast) thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2) # Find the largest contour, which represents the grid itself max_area = 0 c = 0 contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # here I tried both cv2.RETR_EXTERNAL and cv2.RETR_TREE but none of them seems to work for i in contours: area = cv2.contourArea(i) if area > 1000: if area > max_area: max_area = area best_cnt = i image = cv2.drawContours(image, contours, c, (0, 255, 0), 3) c += 1 # Create a mask to search only within these boundaries mask = np.zeros((gray.shape), np.uint8) cv2.drawContours(mask, [best_cnt], 0, 255, -1) cv2.drawContours(mask, [best_cnt], 0, 0, 2) # Cut away the identified mask from the image out = np.zeros_like(gray) out[mask == 255] = gray[mask == 255] # Apply blur and adaptive threshold to this new image blur = cv2.GaussianBlur(out, (5,5), 0) thresh = cv2.adaptiveThresh…
I'm working on a Python algorithm to detect individual cells of a grid passed by an image. Currently, I'm facing an issue where the values inside each cell are being selected as contours along with the cells themselves. As you can see: Here's part of my current code: # Read the image image = cv2.imread(image_path) # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur to remove noise for simplifying grid line identification blur = cv2.GaussianBlur(gray, (5,5), 0) # Apply adaptive threshold to the image (to handle variations in brightness and contrast) thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2) # Find the largest contour, which represents the grid itself max_area = 0 c = 0 contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # here I tried both cv2.RETR_EXTERNAL and cv2.RETR_TREE but none of them seems to work for i in contours: area = cv2.contourArea(i) if area > 1000: if area > max_area: max_area = area best_cnt = i image = cv2.drawContours(image, contours, c, (0, 255, 0), 3) c += 1 # Create a mask to search only within these boundaries mask = np.zeros((gray.shape), np.uint8) cv2.drawContours(mask, [best_cnt], 0, 255, -1) cv2.drawContours(mask, [best_cnt], 0, 0, 2) # Cut away the identified mask from the image out = np.zeros_like(gray) out[mask == 255] = gray[mask == 255] # Apply blur and adaptive threshold to this new image blur = cv2.GaussianBlur(out, (5,5), 0) thresh = cv2.adaptiveThresh…
Full article content could not be extracted automatically. Read the original below.
Source:
AI Stack Exchange
· ai.stackexchange.com