如何改进Python中的轮廓提取算法?

问题描述 投票:0回答:0

我想从来自 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")

从numpy数组中获取数据

B = 数据['数据']

获取背景图片

背景图片 = B[:, :, 0]

创建用于背景减除的 GMM 对象

gmm = cv2.createBackgroundSubtractorMOG2(历史=300,varThreshold=16,detectShadows=True)

开闭操作的形态学结构元素

内核 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))

定义中值滤波器的内核大小

ksize_median = 3

定义高斯滤波器的内核大小

ksize_gaussian = (5,5)

定义二值图像的阈值

阈值 = 100

update_frame 函数返回动画的下一帧

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)

使用 update_frame 函数创建动画

对于范围内的 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.
python image image-processing cv2
© www.soinside.com 2019 - 2024. All rights reserved.