YuXin_Liu/图像分割/segment/seg3D.py

50 lines
1.4 KiB
Python
Raw Normal View History

2024-09-24 13:53:45 +08:00
import numpy as np
import cv2
import matplotlib.pyplot as plt
#add for background
# background_image = cv2.imread('./origionSeg.png')
# resized_image = cv2.resize(background_image, (100, 100))
# 创建一个空白图像假设大小为100x100
height, width = 100, 100
segmentation_result = np.zeros((height, width), dtype=np.uint8)
# 创建一些分割区域
segmentation_result[10:30, 10:30] = 50 # 区域1
segmentation_result[40:60, 40:60] = 100 # 区域2
segmentation_result[70:90, 70:90] = 150 # 区域3
segmentation_result[20:50, 70:90] = 200 # 区域4
# 保存图像
cv2.imwrite('segmentation_result.png', segmentation_result)
segmentation_result = cv2.imread('segmentation_result.png', cv2.IMREAD_GRAYSCALE)
height, width = segmentation_result.shape
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#add for background
# ax.imshow(resized_image)
unique_labels = np.unique(segmentation_result)
heights = np.linspace(1, 10, len(unique_labels)) # 高度范围从1到10可以根据需要调整
for i, label in enumerate(unique_labels):
mask = (segmentation_result == label)
x, y = np.meshgrid(np.arange(width), np.arange(height))
x = x[mask]
y = y[mask]
z = np.zeros_like(x)
dz = np.full_like(x, heights[i])
ax.bar3d(x, y, z, 1, 1, dz, shade=True)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Height')
plt.show()