在Python中使用OpenCV2删除接缝后,如何判断图像的大小?

问题描述

我正在尝试编写一些代码,以使用OpenCV2从Python的网络摄像头的实时视频中操纵单个图像(“帧”)。我可以操纵框架,但是边缘上有多余的图像。

框架:

Image

我希望“黑色区域”不在框架中,所以我想知道如何控制画布的大小以使黑色区域不可见。

我希望框架看起来像什么

enter image description here

有人知道我可以使用什么代码来裁剪画布大小吗?

解决方法

您可以使用对ImageMagick命令行-trim函数的Python子处理调用来去除任何触及图像侧面的黑色。

请参见https://imagemagick.org/discourse-server/viewtopic.php?f=4&t=35579

输入:

enter image description here

import subprocess
cmd = 'convert seams1.png -fuzz 0% -background black -define trim:percent-background=0% -trim +repage result.png'
subprocess.check_output(cmd,shell=True,universal_newlines=True)

import subprocess
cmd = 'convert seams1.png -fuzz 0% -background black -define trim:percent-background=0% -trim +repage result.png'
subprocess.call(cmd,shell=True)

enter image description here