Reg: Копировать BLOB-объекты в зависимости от размера их размера.
Цель: скопировать большие капли в другое изображение маски
У меня есть пороговое изображение с каплями, как показано:
Как я могу скопировать большие капли в изображение маски и пропустить однопиксельные капли?
Мой код (но я не получаю желаемый результат):
import numpy as np
import cv2
ref_img = cv2.imread('threshold.jpg', 0)
thresh = np.copy(ref_img)
cnts,_ = cv2.findContours(ref_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros(ref_img.shape, dtype="uint8")
for c in cnts:
(x,y),radius = cv2.minEnclosingCircle(c)
area = cv2.contourArea(c)
if int(area) < 1:
cv2.circle(mask, (int(x), int(y)), int(radius), (255, 255, 255), -1)
cv2.imshow('img', mask)
cv2.waitKey(0)
Примечание. Использование OpenCV 2.4.x
1 ответ
Вот один из методов, которые вы можете использовать для достижения своей цели. Пояснения приведены в комментариях к коду
import numpy as np
import cv2
ref_img = cv2.imread('threshold.jpg', 0)
img, cnts,_ = cv2.findContours(ref_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros(ref_img.shape, dtype="uint8")
for c in cnts:
# Get the bounding rect surrounding your blobs
x,y,w,h = cv2.boundingRect(c)
# Calculating the area of the rect is similar to the area of the blob minus the complexity
area = w*h
# For box area bigger than one, copy the information from the source image to the mask.
# Since the bounding box contains all the relevant information, just copy the entire box to the mask.
if int(area) > 1 :
mask[y:y+h,x:x+w] = ref_img[y:y+h,x:x+w]
cv2.imshow('img', mask)
cv2.waitKey(0)