opencv - 检测并提取被框架包围的图像

标签 opencv imagemagick

我想从输入图像中获取以下结果图像。生成的图像被具有相同边框大小和类型的框架包围,但边框矩形大小不同。有没有办法做到这一点?我想我需要检测边界包围的区域作为第一步。但不知道。我试图在 ImageMagick 中找到它。

  • 输入图片 (input.png)

  • enter image description here
  • 结果图像 (output1.png)

  • enter image description here
  • 结果图像 (output2.png)

  • enter image description here
  • 边框

  • enter image description here

    更新 1

    这并不完美,但它可以与 OpenCV 一起使用,如下所示。
    import cv2 as cv
    
    def main():
        image_file = '/path/to/your/input/image.png'
        src = cv.imread(image_file, cv.IMREAD_COLOR)
        height, width, channels = src.shape
        image_size = height * width
        img_gray = cv.cvtColor(src, cv.COLOR_RGB2GRAY)
        retval, dst = cv.threshold(img_gray, 1000, 255, cv.THRESH_TOZERO_INV)
        dst = cv.bitwise_not(dst)
        retval, dst = cv.threshold(dst, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
        dst, contours, hierarchy = cv.findContours(
            dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    
        xxx = 0
        for i, contour in enumerate(contours):
            area = cv.contourArea(contour)
            if area < 50000:
                continue
            if image_size * 0.99 < area:
                continue
            if abs(i - xxx) < 10:
                continue
            xxx = i
            x, y, w, h = cv.boundingRect(contour)
            cut = src[y:y+h, x:x+w]
            detector = cv.FastFeatureDetector_create()
            detector.setNonmaxSuppression(False)
            keypoints = detector.detect(cut)
            cv.imwrite('debug_%d.png' % i, cut)
    
    if __name__ == '__main__':
        main()
    

    引用本站:https://angular.io/guide/providers

    更新 2

    fmw42 的方式很棒,但不足以满足我的以下要求。 (我在第一篇文章中没有提到)提取了唯一的蓝色矩形。背景颜色可能是白色的。
  • 输入图片 (input2.png)

  • enter image description here
  • 实际结果图片 (output.png)

  • enter image description here

    最佳答案

    这可以在 ImageMagick (6) 中使用 -connected-components 完成。

    在这里,我转换为 HSV 颜色空间并提取饱和度 channel 。白色和黑色没有饱和度,但粉色和蓝色有。然后我设置阈值,使粉红色和蓝色在黑色背景上变为白色。然后我使用形态侵 eclipse 来消除你的边界的影响。然后我使用连接组件填充白色区域中的任何孔,然后获取它们的边界框并存储在一个数组中。然后我遍历每个边界框并裁剪原始图像。

    https://imagemagick.org/script/connected-components.php

    输入:

    enter image description here

    Unix 语法:

    bboxArr=(`convert wikipedia.png \
    -colorspace HSV -channel 1 -separate +channel \
    -threshold 0 -type bilevel \
    -morphology erode square:3 \
    -define connected-components:verbose=true \
    -define connected-components:mean-color=true \
    -define connected-components:area-threshold=1000 \
    -connected-components 4 null: | grep "gray(255)" | awk '{print $2}'`)
    
    num=${#bboxArr[*]}
    
    for ((i=0; i<num; i++)); do
    convert wikipedia.png -crop ${bboxArr[$i]} +repage wikipedia_$i.png
    done
    

    结果:

    enter image description here

    enter image description here

    如果使用 ImageMagick 7,则将 convert 更改为 magick。

    Windows 语法将需要删除 ( 和 ) 之前的\。并且还将行尾更改为 ^。 grep 和 awk 是 Unix 工具。因此,您可能需要为 Windows 安装此类或找到其他方法来执行此操作。

    关于opencv - 检测并提取被框架包围的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60004337/

    相关文章:

    imagemagick - 如何使用GraphicsMagick取消图片的Alpha channel

    php - 如何使用 Imagemagick PHP API 将 .pdf 文件转换为 .png

    firebase - Firebase ImageMagick 的云函数转换 : not found

    python - 定义线之间的距离

    opencv - 查找图像中最大的 blob

    python - 图像处理:道路提取

    python - OpenCV 霍​​夫圆的回溯

    imagemagick - 如何使用 Imagemagick 确定 GIF 的帧率?

    c++ - Mat 的 tcp 发送缓冲区错误

    python - 将 PDF 转换为 600dpi 的 TIFF 和 jpg 96 dpi