обычная сварочная полоса: https://i.sstatic.net/MBcyyIyp .jpg
У меня возникли проблемы при обнаружении дефектов (например, дыр, неоднородностей) с помощью opencv. Я новичок в opencv и пробовал обнаружение контуров, обнаружение краев, но не получил желаемых результатов. Я хочу создать алгоритм с использованием opencv, который обнаруживает эти дыры и помечает их, не отмечая никаких других ненужных вещей, которые не являются дефектами.
Вот подход, который я использую в своем коде
Код: Выделить всё
def detect_holes(image):
# Convert the image to HSV color space
hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
# Define HSV range for the golden bronze region
lower_hsv = (10, 20, 20)
upper_hsv = (30, 255, 255)
# Create a mask for the golden bronze region
mask = cv2.inRange(hsv_image, lower_hsv, upper_hsv)
# Apply morphological operations to clean up the mask
kernel = np.ones((5,5), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=3)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=3)
# Smooth the edges of the mask
mask = cv2.GaussianBlur(mask, (15, 15), 0)
_, mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
# Apply the mask to the original image
masked_image = cv2.bitwise_and(image, image, mask=mask)
# Convert the masked image to grayscale
gray = cv2.cvtColor(masked_image, cv2.COLOR_RGB2GRAY)
# Set up the SimpleBlobDetector parameters
params = cv2.SimpleBlobDetector_Params()
params.minThreshold = 10
params.maxThreshold = 200
params.filterByArea = True
params.minArea = 130
params.maxArea = 300
params.filterByCircularity = True
params.minCircularity = 0.1
params.filterByConvexity = True
params.minConvexity = 0.5
params.filterByInertia = True
params.minInertiaRatio = 0.01
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs
keypoints = detector.detect(gray)
# Draw detected blobs as red circles
result = cv2.drawKeypoints(image, keypoints, np.array([]), (255,0,0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
return result, len(keypoints)
Подробнее здесь: https://stackoverflow.com/questions/788 ... ing-opencv