Я использую opencv-python версии 4.9.0.80.< /p>
Итак, когда у нас есть такие изображения.


Мы хотим выводить такой результат без изображений внутри. Избегайте серого фона.


Я пробовал cv. convexHull(), но результат не такой хороший, как ожидалось.
[img]https: //i.sstatic.net/tCX1SdXym.png[/img]
Я пробую этот код.
import cv2 as cv
import numpy as np
path = '/opt/boundary-remover/SW_test2-01.png'
# path = '/opt/boundary-remover/scaled_2x.png'
# Load image
src = cv.imread(cv.samples.findFile(path))
cv.imwrite('1_original.png', src)
# Convert image to gray and blur it
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.imwrite('2_gray.png', src_gray)
# Detect edges using Canny
canny_output = cv.Canny(src_gray, 100, 200)
cv.imwrite('3_canny.png', canny_output)
# Find contours
contours, _ = cv.findContours(canny_output, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
# Combine all contours into a single contour
all_contours = np.concatenate(contours)
# Find the convex hull object for the combined contour
hull = cv.convexHull(all_contours)
# Draw the combined contour and its convex hull
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)
color = (0, 255, 0) # Green color
# cv.drawContours(drawing, [all_contours], 0, color)
# cv.imwrite('4_all_contours.png', drawing)
cv.drawContours(drawing, [hull], 0, color)
cv.imwrite('5_hull.png', drawing)
cv.drawContours(drawing, [hull], -1, color, thickness=cv.FILLED)
cv.imwrite('7_filled.png', drawing)
Подробнее здесь: https://stackoverflow.com/questions/784 ... ge-updated