【问题标题】:Segment a region from a numpy array从 numpy 数组中分割一个区域
【发布时间】:2021-06-10 05:23:20
【问题描述】:

在对图像 I 进行一些处理后,提取了图像的某些区域。 Here is the .npy file.

segmented_image = np.load('data.npy')
plt.imshow(segmented_image)  

现在,我正在尝试crop/segment P 地区。我该怎么做?

提前致谢。

【问题讨论】:

  • 我会尝试找到彩色图片的最小/最大 x/y 值,然后将图像裁剪为该值
  • @kabooya 是的,但我正在尝试以无人监督的方式进行分割,例如面积计算或形状。

标签: python numpy tensorflow opencv computer-vision


【解决方案1】:

你可以试试轮廓过滤。

import cv2
import numpy as np

image = np.load("data.npy")
cv2.imshow("image", image)

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

_, threshold_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY)
cv2.imshow("threshold_image", threshold_image)

contours, hierarchy = cv2.findContours(threshold_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

# here you can apply your conter filter logic
# In this image I can see biggest contur is "p"
selected_contour = max(contours, key=lambda x: cv2.contourArea(x))

mask_image = np.zeros_like(threshold_image)
cv2.drawContours(mask_image, [selected_contour], -1, 255, -1)
cv2.imshow("mask_image", mask_image)

segmented_image = cv2.bitwise_and(image, image, mask=mask_image)
cv2.imshow("segmented_image", segmented_image)

cv2.waitKey(0)


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2013-03-09
    • 2018-07-20
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多