【问题标题】:how to extract colored region on image and warp into desired size in python?如何在python中提取图像上的彩色区域并变形为所需的大小?
【发布时间】:2021-09-21 15:44:49
【问题描述】:

我是图像处理和计算机视觉的新手,

我有上面带有额外蒙版区域 (956 x 712) 的书皮, 我需要帮助才能使用 opencv 获取以下图像(407 X 569)。

谢谢。

【问题讨论】:

  • 欢迎来到 Stackoverflow!你能在你的帖子中添加一些尝试的代码吗?以目前的信息,我们甚至不知道你是否可以运行 python。

标签: python opencv image-processing computer-vision


【解决方案1】:

如果您知道这本书的确切坐标,您可以使用以下代码选择感兴趣的区域

我假设您已经将原始图像作为 img 作为 numpy 数组。

result_image = img[y:y+h, x:x+w, ...]

其中 x、y、w、h 是图像中书籍的边界框坐标。

如果你想自动找到黑色背景上的书 您可以将图像转换为二进制并找到最大的非黑色部分。然后获取该区域的坐标并进行裁剪。

img

# convert to binary
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

# find the biggest non-black area
contours, _ = cv2.findContours(
    binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
    )
c = max(contours, key=cv2.contourArea)

# get coordinates of the biggest area
x, y, w, h = cv2.boundingRect(c)

# crop it
result_image = img[y:y+h, x:x+w, ...]

result_image

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2021-10-15
    • 2018-08-31
    相关资源
    最近更新 更多