c++ - 在OpenCV C++中拼接2张图像后去除图像的黑色部分

标签 c++ image opencv image-processing computer-vision

所以我在 OpenCV C++ 中拼接了 2 个图像,但我知道图像中有一个全黑的部分,想删除它。要走的路是什么?

这是我的图像输出:

enter image description here

最佳答案

这个想法是对每列的像素求和,然后遍历数据以构建新图像。如果列的值为零,则表示它是黑色的,因此我们忽略它,否则我们将列 ROI 连接到最终图像。这是列像素的总和:



结果

enter image description here

我在 Python 中实现了它,但您可以将类似的想法应用到 C++

import cv2
import numpy as np 
# import matplotlib.pyplot as plt

# Load image, convert to grayscale, and sum column pixels
image = cv2.imread('1.jpg')
h, w = image.shape[:2]
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
first_pass = True
pixels = np.sum(gray, axis=0).tolist()

# Build new image
for index, value in enumerate(pixels):
    if value == 0:
        continue
    else:
        ROI = image[0:h, index:index+1]
        if first_pass:
            result = image[0:h, index+1:index+2]
            first_pass = False
            continue
        result = np.concatenate((result, ROI), axis=1)

cv2.imshow('result', result)
cv2.imwrite('result.png', result)
# Uncomment for plot visualization
# plt.plot(pixels, color='teal')
# plt.show()
cv2.waitKey()

关于c++ - 在OpenCV C++中拼接2张图像后去除图像的黑色部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59796119/

相关文章:

image - OpenCV:处理 12 位灰度原始数据

c++ - 是否可以从资源创建Gtk::Builder而不实现try/catch block ?

c++ - 在 C++ 中限制对类的公共(public)成员的访问的设计模式

ios - 如何将图像保存在文档目录中并在 Swift 中获取它

android - 如何编程按钮将表格布局导出到 Android 应用程序上的图像或 PDF 文件

python - 在 Python 中为 OpenCV 视频对象指定压缩质量

python - 使用opencv和Python消除图像模糊

c++ - 读/写 : Illegal seek - client-server C++

android - 编译android-ndk代码的问题

Java JFrame : Image not displayed