我想从来自 Microsoft Kinect 的图像中提取剪影。我将这些文件另存为 .mat 文件。
这是我现在的代码:
lang-python
import scipy.io
import cv2
import numpy as np
# Load the data from kinect_data.mat
data = scipy.io.loadmat(r"C:\Users\01130309\Desktop\Inżynierka\inzynierka_python\venv\Scripts\kinect_data.mat")
B = 数据['数据']
背景图片 = B[:, :, 0]
gmm = cv2.createBackgroundSubtractorMOG2(历史=300,varThreshold=16,detectShadows=True)
内核 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
ksize_median = 3
ksize_gaussian = (5,5)
阈值 = 100
def update_frame(i): 框架 = B[:, :, i]
# Apply Gaussian blur to denoised frame
denoised_frame = cv2.GaussianBlur(frame, ksize_gaussian, 0)
# Process the frame through the background subtraction algorithm
fg_mask = gmm.apply(denoised_frame)
# Process the frame through a morphological open operation
opened_frame = cv2.morphologyEx(fg_mask, cv2.MORPH_OPEN, kernel)
# Threshold the image
thresholded_frame = cv2.threshold(opened_frame, threshold_value, 255, cv2.THRESH_BINARY)[1]
# Process the frame through a morphological closing operation
closed_frame = cv2.morphologyEx(thresholded_frame, cv2.MORPH_CLOSE, kernel)
# Remove holes in the silhouette via a hole-filling operation
contours, hierarchy = cv2.findContours(closed_frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
cv2.drawContours(closed_frame, [cnt], 0, 255, -1)
# Remove small objects from the image
filtered_frame = cv2.morphologyEx(closed_frame, cv2.MORPH_OPEN, kernel, iterations=2)
# Remove small white dots from the silhouette
filtered_frame = cv2.erode(filtered_frame, kernel, iterations=1)
# Show the resulting silhouette
cv2.imshow("Silhouette", filtered_frame)
cv2.waitKey(1)
对于范围内的 i(1000, 3990): 更新帧(i)
cv2.destroyAllWindows()
This code gave me something that look like this:
[enter image description here](https://i.stack.imgur.com/c6BOf.png)
[enter image description here](https://i.stack.imgur.com/9KcGP.png)
[enter image description here](https://i.stack.imgur.com/cjm6A.png)
Like you see - there is still a lots of noise from the background and sometimes silhouette has blank spots.
\*\*What can I improve? \*\*
I tried different types of blurring, I was trying to change the threshold and as you can see, I tried to remove holes or these small objects.