首页 > 解决方案 > 如何可视化选择性搜索的分割图像?

问题描述

如何可视化应用于图像的选择性搜索算法的分割图像输出?

import cv2

image = cv2.imread("x.jpg")

ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
ss.setBaseImage(image)

ss.switchToSelectiveSearchQuality()
rects = ss.process()

也就是得到右边的图像

在此处输入图像描述

标签: pythonopencv

解决方案


我认为您可以使用以下内容。我试过了 - 它工作正常

import cv2, random

image = cv2.imread("x.jpg")

ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
ss.setBaseImage(image)

ss.switchToSelectiveSearchQuality()
rects = ss.process()

for i in range(0, len(rects), 100):
    # clone the original image so we can draw on it

    output = image.copy()
    # loop over the current subset of region proposals
    for (x, y, w, h) in rects[i:i + 100]:
        # draw the region proposal bounding box on the image
        color = [random.randint(0, 255) for j in range(0, 3)]
        cv2.rectangle(output, (x, y), (x + w, y + h), color, 2)

    cv2.imshow("Output", output)
    key = cv2.waitKey(0) & 0xFF
    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

为什么是100?我选择了 100 的块大小。

原图:

在此处输入图像描述

处理后: 在此处输入图像描述


推荐阅读