首页 > 解决方案 > 如何在 Python 中使用分水岭算法分离图像

问题描述

如何在Python中使用分水岭算法在图像分割后将单个图像从多个图像中分离出来附加图像由4个图像组成,我们需要从中应用图像分割并将单个图像从这4个图像中分离出来 在此处输入图像描述

标签: pythonopencvimage-processingcomputer-visiondata-science

解决方案


我们将首先淹没它

import cv2;
import numpy as np;

# Read image
im_in = cv2.imread("2SNAT.jpg", cv2.IMREAD_GRAYSCALE);

# Threshold.
# Set values equal to or above 220 to 0.
# Set values below 220 to 255.

th, im_th = cv2.threshold(im_in, 220, 255, cv2.THRESH_BINARY_INV);

# Copy the thresholded image.
im_floodfill = im_th.copy()

# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = im_th.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)

# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask, (0,0), 255);

# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)

# Combine the two images to get the foreground.
im_out = im_th | im_floodfill_inv

填充图像

然后找到轮廓并裁剪

im, contours, hierarchy = cv2.findContours(im_out.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

final_contours = []
for contour in contours:
    area = cv2.contourArea(contour)
    if area > 1000:
        final_contours.append(contour)

裁剪步骤,也在原始图像上绘制矩形

counter = 0        
for c in final_contours:
    counter = counter + 1
# for c in [final_contours[0]]:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.01 * peri, True)
    x,y,w,h = cv2.boundingRect(approx)
    print(x, y, w, h)
    aspect_ratio = w / float(h)

    if (aspect_ratio >= 0.8 and aspect_ratio <= 4):
        cv2.rectangle(im_in,(x,y),(x+w,y+h),(0,255,0),2)
        cv2.imwrite('splitted_{}.jpg'.format(counter), im_in[y:y+h, x:x+w])
cv2.imwrite('rectangled_split.jpg', im_in)

绘制正方形 结果


推荐阅读