【问题标题】:how to Extract rois as induvidual roi images [closed]如何将 roi 提取为单个 roi 图像 [关闭]
【发布时间】:2025-12-12 03:30:01
【问题描述】:

我的图像在透明图像中包含多个 rois //非矩形//。请帮助我如何将这些 rois 提取为单个 roi 图像

【问题讨论】:

    标签: image image-processing extract opencv-python roi


    【解决方案1】:
    1. 找到图片的contours

    import cv2
    
    img = cv2.imread("gzgSI.jpg")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 127, 255, 0)
    contours, hierarchy = cv2.findContours(thresh,
                                           cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    
    for cnt in contours:
        cv2.drawContours(img, [cnt], 0, (0, 255, 0), 3)
    

    结果:

    1. 现在我们确定,我们已经检测到图像中的所有轮廓,我们可以单独保存它们。

      • 获取每个contour的坐标

        for i, cnt in enumerate(contours):
            x, y, width, height = cv2.boundingRect(cnt)
        
      • 设置图片中的坐标

        roi = img[y:y+height, x:x+width]
        
      • 一些示例结果:

    • 确保在运行代码之前已创建rois 文件夹。

    代码:

    import cv2
    
    img = cv2.imread("gzgSI.jpg")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 127, 255, 0)
    contours, hierarchy = cv2.findContours(thresh,
                                           cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    
    for i, cnt in enumerate(contours):
        x, y, width, height = cv2.boundingRect(cnt)
        roi = img[y:y+height, x:x+width]
        cv2.imwrite("rois/roi{}.png".format(i), roi)
    

    【讨论】:

    • ty ahx... 发誓我已经用同样的方法解决了))
    【解决方案2】:
    import numpy as np
    import matplotlib.pyplot as plt
    
    !mkdir /content/test
    !rm -r /content/test/*
    image = cv2.imread('/content/test.png')
    original = image.copy()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (3, 3), 0)
    canny = cv2.Canny(blurred, 120, 255, 1)
    kernel = np.ones((5,5),np.uint8)
    dilate = cv2.dilate(canny, kernel, iterations=1)
    
    # Find contours
    cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    
    # Iterate thorugh contours and filter for ROI
    image_number = 0
    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        #cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
        ROI = original[y:y+h, x:x+w]
        #cv2.imwrite("/content/test/ROI_{}.png".format(image_number), ROI)
        image_number += 1
    
    ## (4) Create mask and do bitwise-op
    mask = np.zeros(image.shape[:2],np.uint8)
    cv2.drawContours(mask, [c],-1, 255, -1)
    dst = cv2.bitwise_and(image, image, mask=mask)
    
    ## Save it
    #cv2.imwrite("dst.png", dst)
    cv2.imwrite("/content/test/ROI_{}.png".format(image_number), dst)
    image_number += 1
    
    
    
    # cv2.imshow('canny', canny)
    # cv2.imshow('image', image)
    # cv2.waitKey(0)
    
    plt.imshow(canny)
    plt.imshow(image)here
    

    【讨论】:

    • 恭喜您解决了自己的问题。
    • 另一个帮助 plzzz...如果可能的话 ahx plz uh *.com/q/64267283/8814904
    • 有机会我会看问题的
    • 无论如何,非常感谢 ahx
    最近更新 更多