最佳答案
这个想法是对每列的像素求和,然后遍历数据以构建新图像。如果列的值为零,则表示它是黑色的,因此我们忽略它,否则我们将列 ROI 连接到最终图像。这是列像素的总和:
结果
我在 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/