Initialize visual components such as SAM for assistance
This commit is contained in:
34
utils/image_processing/contour.py
Normal file
34
utils/image_processing/contour.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import cv2
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
# Load the image
|
||||
image = cv2.imread('../../mm_agents/stackoverflow.png')
|
||||
|
||||
# Convert to grayscale
|
||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
# Apply adaptive thresholding to get a binary image
|
||||
thresh = cv2.adaptiveThreshold(
|
||||
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2
|
||||
)
|
||||
|
||||
# Find contours
|
||||
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
# Filter out contours that are not of cell size
|
||||
# This is done by assuming that cells will have a relatively standard size
|
||||
# The size filter is just a placeholder, real values depend on the actual image size
|
||||
min_cell_size = 500
|
||||
max_cell_size = 5000
|
||||
cell_contours = [cnt for cnt in contours if min_cell_size < cv2.contourArea(cnt) < max_cell_size]
|
||||
|
||||
# Draw contours on the image
|
||||
contour_output = image.copy()
|
||||
cv2.drawContours(contour_output, cell_contours, -1, (0, 255, 0), 2)
|
||||
|
||||
# Display the image with cell contours
|
||||
plt.figure(figsize=(12,6))
|
||||
plt.imshow(cv2.cvtColor(contour_output, cv2.COLOR_BGR2RGB))
|
||||
plt.title('Spreadsheet with Cell Contours')
|
||||
plt.axis('off')
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user