OPENCV同型术后删除所有黑色
I am using OpenCv homography to stitch images together. How can I remove the negative space between the images, as below?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您愿意使用非OPENCV解决方案,则可以使用ImageMagick 7 -trim(或使用ImageMagick的Python Wand)。您可以使用subprocess.call
输入:
If you are willing to use a non-OpenCV solution, you can use Imagemagick 7 -trim (or Python Wand, which uses Imagemagick). You can call the Imagemagick command line below from Python using a subprocess.call
Input:
使用
python
wand
函数,我们可以进行修剪到0:Using
python
wand
functions, we can do the trimming to0:建议使用OPENCV的建议:
假设(x0,y0),(x1,y1)是该区域的左上和底部坐标(目标矩形,没有黑色边缘)。
将黑色边缘分成4个(或更少)的轮廓:
“顶轮廓”定义
y0
坐标。“顶轮廓”定义
y1
坐标。“右轮廓”定义
x1
坐标。“左轮廓”(如果存在)定义
x0
坐标。这是一个例证:

在边缘构建一个二进制的“蒙版”,在“图像部分”处构建零:
我们想构建以下二进制图像:

您发布的示例图像使其有些挑战。
将黑色边缘与图像分开。
有些人工制品看起来像JPEG压缩工件,使过程更加困难。
建议使其更简单:
而不是以BGR像素格式缝制图像,而是以Bgra像素格式缝制图像。
将(原始图像集)的像素从BGR转换为BGRA,其中“ A”是一个alpha(透明)通道,填充了255(完全不透明)。
缝合图像后,输出的alpha通道应为255,其中像素是图像的一部分,在“黑色”边缘为零。
输出的Alpha通道为我们提供了所需的二进制掩码“免费”。
对于上述示例图像,我们可以使用以下阶段:
通过试用错误,手动将阈值设置为
10
(没有伪像,它应该为1
)。裁剪矩形与最低面积:
要将背景分为4个(或更少)轮廓,我们必须确保“黑色区域”很好地分开(未连接)。
为此,我们可能会在最低区域裁剪矩形:
查找和分析4(或更少)轮廓:
Inv_thresh = 255-阈值
。现在,我们最多有4个轮廓(在我们的情况下为3个) - “顶部”,“底部”和“右”。
对于每个轮廓,找到边界矩形
(x,y,w,h)= cv2.boundingRect(c)
。如果
x == 0
和(h> w)
我们找到了“左轮廓”。如果
x + w == image_witdh
和(h> w)
我们找到了“正确的轮廓”。如果
y == 0
和(w> h)
我们找到了“顶级轮廓”。如果
y + h == image_height
和(w> h)
我们找到了“底部轮廓”。注意:当上述启发式方法失败时,可能会有很少的情况 - 对于县解决方案,我们可能需要将每个轮廓与所有其他轮廓进行比较(一种排序)。
代码样本:
以下代码示例还将“左”,“右”,“ top”,“底部”写为草图图像上的文本。
结果:
Inv_thresh_bgr
(用于测试):输出

img
:Suggested solution using OpenCV:
Assume (x0, y0), (x1, y1) are the top left and bottom right coordinates of the region to crop (target rectangle without black margins).
Split the black margins into 4 (or less) contours:
The "Top contour" defines
y0
coordinate.The "Top contour" defines
y1
coordinate.The "Right contour" defines
x1
coordinate.The "Left contour" (if exists) defines
x0
coordinate.Here is an illustration:

Building a binary "mask" with zeros at the margins and white at the "image part":
We want to build the following binary image:

The sample image you have posted makes it a bit challenging.
There is no "clear cut" for separating the black margins from the image.
There are artifacts that looks like JPEG compression artifacts that makes the procedure more difficult.
Suggestion for making it simpler:
Instead of stitching images in BGR pixel format, stitch images in BGRA pixel format.
Convert the pixels (of the original set of images) from BGR to BGRA, where "A" is an alpha (transparency) channel, filled with 255 (fully opaque).
After stitching the images, the alpha channel of the output supposed to be 255 where pixels are part of an image, and zero in the "black" margins.
The alpha channel of the output gives us the desired binary mask "for free".
For the above sample image, we may use the following stages:
The threshold was manually set to
10
by trial an error (with no artifacts it supposed to be1
).Cropping rectangle with minimum area:
For separating the background into 4 (or less) contours, we have to make sure that the "black regions" are well split (not connected).
For that purpose we may crop the rectangle with minimum area:
Finding and analyzing the 4 (or less) contours:
inv_thresh = 255 - thresh
.Now we have up to 4 contours (3 in our case) - "Top", "Bottom" and "Right".
For each contour, find the bounding rectangle
(x, y, w, h) = cv2.boundingRect(c)
.if
x == 0
and(h > w)
we found the "Left Contour".if
x + w == image_witdh
and(h > w)
we found the "Right Contour".if
y == 0
and(w > h)
we found the "Top Contour".if
y + h == image_height
and(w > h)
we found the "Bottom Contour".Note: There may be rare cases when the above heuristics fails - for prefect solution we may need to compare each contour with all other contours (kind of sorting).
Code sample:
The following code sample also writes "Left", "Right", "Top", "Bottom" as text on a sketch image for testing).
Results:
inv_thresh_bgr
(used for testing):Output

img
: