【发布时间】:2020-08-24 11:17:24
【问题描述】:
我有一个无法解决的图像处理问题。我有一组 375 张图片,如下图 (1)。我正在尝试去除背景,以便进行“背景减法”(或“前景提取”)并仅在纯背景(黑色/白色/...)上获得浪费。
我尝试了很多东西,包括来自 OpenCV 的 createBackgroundSubtractorMOG2 或阈值。我还尝试通过从前景中减去它来逐个像素地删除背景,因为我有一组 237 个背景图像 (2)(没有浪费的地毯,但与带有对象的图像有点偏移)。背景图像的亮度也存在差异。
(2) Example of a background image
这是一个我能够测试的代码示例,它给出了以下 (3) 和 (4) 的结果。我使用 Python 3.8.3。
# Function to remove the sides of the images
def delete_side(img, x_left, x_right):
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if j<=x_left or j>=x_right:
img[i,j] = (0,0,0)
return img
# Intialize the background model
backSub = cv2.createBackgroundSubtractorMOG2(history=250, varThreshold=2, detectShadows=True)
# Read the frames and update the background model
for frame in frames:
if frame.endswith(".png"):
filepath = FRAMES_FOLDER + '/' + frame
img = cv2.imread(filepath)
img_cut = delete_side(img, x_left=190, x_right=1280)
gray = cv2.cvtColor(img_cut, cv2.COLOR_BGR2GRAY)
mask = backSub.apply(gray)
newimage = cv2.bitwise_or(img, img, mask=mask)
img_blurred = cv2.GaussianBlur(newimage, (5, 5), 0)
gray2 = cv2.cvtColor(img_blurred, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray2, 10, 255, cv2.THRESH_BINARY)
final = cv2.bitwise_or(img, img, mask=binary)
newpath = RESULT_FOLDER + '/' + frame
cv2.imwrite(newpath, final)
我受到 Stackoverflow 或其他网站上的许多其他案例的启发(例如:removing pixels less than n size(noise) in an image - open CV python)。
(3) The result obtained with the code above
(4) Result when increasing the varThreshold argument to 10
很遗憾,生成的图片仍然有很多噪点。
作为“背景减法”的初学者,我没有获得最佳解决方案的所有关键。如果有人想以更有效和更干净的方式完成这项任务(是否有一种特殊的方法来处理透明物体的情况?可以更有效地消除物体上的噪音吗?等等),我很感兴趣:) 谢谢
【问题讨论】:
-
您的应用程序中的背景是否固定?我认为分割这些对象并不容易。但是,在收购过程中控制一些因素会使这变得容易得多。
-
感谢您的回复。是的,拍照的相机是固定的,背景的传送带总是一样的。可能只有亮度不同。
-
@cam1234 我建议使用GRIP。它是一个 GUI 应用程序,您可以在其中拖放/连接图像处理过滤器,然后将代码导出到 Python。
标签: python deep-learning computer-vision image-segmentation background-subtraction