如何从图像中裁剪出边框 [英] How to crop a bounding box out of an image

查看:186
本文介绍了如何从图像中裁剪出边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

稍微解释一下这个问题.我有一张已经包含白色边框的图像,如下所示: 输入图像

To explain the question a bit. I have an image that already contains a white bounding box as shown here: Input image

我需要的是裁剪由边框包围的图像部分.

What I need is to crop the part of the image surrounded by the bounding box.

FindContours在这里似乎不起作用,因此我使用以下代码尝试了一些操作:

FindContours doesn't seem to work here so I tried something using the following code:

import cv2
import numpy as np

bounding_box_image = cv2.imread('PedestrianRectangles/1/grim.pgm')

edges = cv2.Canny(bounding_box_image, 50, 100)  # apertureSize=3

cv2.imshow('edge', edges)
cv2.waitKey(0)

lines = cv2.HoughLinesP(edges, rho=0.5, theta=1 * np.pi / 180, 
threshold=100, minLineLength=100, maxLineGap=50)

# print(len(lines))

for i in lines:
    for x1, y1, x2, y2 in i:
        # print(x1, y1, x2, y2)
        cv2.line(bounding_box_image, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv2.imwrite('houghlines5.jpg', bounding_box_image)

没有成功.玩弄参数也没有太大帮助.下图显示了我的代码片段的结果: 输出

without any success. Playing around with the parameters didn't help too much either. The result of my code snippet is show on the following image: Output

我有在进行线检测等之后进行裁剪的想法.

I had the idea to do cropping after the line detection etc.

我对opencv来说还比较陌生,所以将不胜感激.是否有解决这个问题的好方法或简单方法?闲逛无济于事,因此任何链接和代码段都将很有帮助.

I am relatively new to opencv so help would be appreciated. Is there a good or easy approach to this problem that I'm missing? Googling around didn't help so any links, code snippets would be helpful.

推荐答案

  1. 二进制@ 250的阈值
  2. 在二进制文件中找到轮廓
  3. 通过boundingRect的高度/宽度过滤轮廓
  1. Threshold to binary @ 250
  2. Find contours in the binary
  3. Filter the contour by height/width of boundingRect

这篇关于如何从图像中裁剪出边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆