假設我有如下影像,我只想裁剪視網膜的一些子影像。

所以我邁出的第一步就是找個口罩
# Find Mask
img = imread(folder 'test.jpg')
blur = cv2.GaussianBlur(img, (3, 3), cv2.BORDER_DEFAULT)
edged = cv2.Canny(blur, 10, 250)
# threshold
thresh = cv2.threshold(edged, 128, 255, cv2.THRESH_BINARY)[1]
# apply close morphology
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# get bounding box coordinates from the one filled external contour
filled = np.zeros_like(thresh)
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
max_cnt = max(cnts, key=cv2.contourArea)
x,y,w,h = cv2.boundingRect(max_cnt)
cv2.drawContours(filled, [max_cnt], 0, 255, -1)
# crop filled contour image
mask = filled.copy()
cv2_imshow(mask)
這是找到的面具:

然后我希望在蒙版的內部區域內裁剪 50 * 50 的子影像
這是我在網上找到的一些代碼:
# This gives the retina only
new_image = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
new_image[:,:,3] = mask[:,:]
imshow(new_image)

下面更接近我想要的。掩碼中有兩個子影像。但我真正想要的是隨機裁剪掩碼內的 5 個子影像。有沒有辦法我可以做到這一點?或者如何檢查裁剪后的影像是否在蒙版內?
def img_to_grid(img):
# ww = [[i.min(), i.max()] for i in np.array_split(range(img.shape[0]),row)]
# hh = [[i.min(), i.max()] for i in np.array_split(range(img.shape[1]),col)]
print(img.shape[0], img.shape[1])
ww = [[i, i 100-1] for i in range(0, img.shape[0],100)]
print(ww)
hh = [[i, i 100-1] for i in range(0, img.shape[1],100)]
print(hh)
grid = [img[j:jj,i:ii,:] for j,jj in ww for i,ii in hh]
print((j, jj, i, ii) for j,jj in ww for i,ii in hh)
return grid, len(ww), len(hh)
def plot_grid(grid,row,col,h=5,w=5):
fig, ax = plt.subplots(nrows=row, ncols=col)
[axi.set_axis_off() for axi in ax.ravel()]
fig.set_figheight(h)
fig.set_figwidth(w)
c = 0
for row in ax:
for col in row:
col.imshow(np.flip(grid[c],axis=-1))
c =1
plt.show()
grid , r,c = img_to_grid(img)
plot_grid(grid,r,c)

uj5u.com熱心網友回復:
如果要提取不需要內接在圓圈中的真正隨機子影像,您可以在非零蒙版邊界框中獲取一些坐標,檢查它是否僅包含坐標,如果是,則保存它們。
sub_imgs = []
# Define minimum width and height for the cropped region
min_width = 32
min_height = 32
# Define the number of sub-images
sub_img_count = 5
while len(sub_imgs) < sub_img_count:
x1 = np.random.randint(x, x w)
y1 = np.random.randint(y, y h)
maxw = w - (x1 - x)
maxh = h - (y1 - y)
# Skip if maximum possible sizes don't meet the requirement
if not (maxw > min_width and maxh > min_height):
continue
width = np.random.randint(min_width, maxw)
height = np.random.randint(min_height, maxh)
# Extract the region only if it is within the mask
if np.all(mask[y1: y1 height, x1: x1 width]):
sub_img = img[y1: y1 height, x1: x1 width, :]
sub_imgs.append(sub_img)
# Save the cropped regions
for i, sub_img in enumerate(sub_imgs):
cv2.imwrite(f'{i 1}.jpg', sub_img)
裁剪的子影像示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523543.html
上一篇:在Java中合并影像
