import numpy as np
import os
import cv2
import os
from PIL import Image
path_cloud = "C:/Users\lenovo\Desktop\shiyan\p_image/"
path_color="C:/Users\lenovo\Desktop\shiyan\label/"
files = os.listdir(path_cloud)
files.sort(key=lambda x: (x[:-4]))
for i in range(len(files)):
#np.empty((256*256, 4))
cloud_dir = path_cloud + files[i]
img = Image.open(cloud_dir)
img1 = img.convert("L")
#img1 = img.transpose(Image.FLIP_TOP_BOTTOM)
img1=np.array(img1)
# temp = []
# for j in range(0,300):
# for l in range (0,300):
# if img1[j,l]!=0:
# arr=np.array([j,l,img1[j,l]])
# temp.append(arr)
# else:
# continue
# temp=np.array(temp)
#np.savetxt("C:/Users\lenovo\Desktop\shiyan\ori_txt/" + str(i).zfill(4) + ".txt", temp, fmt="%.2f")
for j in range(0,300):
for l in range (0,300):
if 0<=img1[j,l]<=30 :
img1[j, l]=0
else:
img1[j,l]=img1[j,l]
#cv2.imwrite("C:/Users\lenovo\Desktop\shiyan\h_image/" + str(i+8000).zfill(4) + ".png", img1)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
img1 = cv2.morphologyEx(img1, cv2.MORPH_CLOSE, kernel) # 闭
#灰度图像边收缩
for j in range(2,298):
num=0
for l in range (2,298):
if img1[j,l]!=0 and img1[j,l+1]!=0 and img1[j,l-1]==0 and img1[j,l+2]!=0 and img1[j,l-2]==0:
num=num+1
if num<2:
img1[j, l] = 0
img1[j, l+1] = 0
#img1[j, l+2] = 0
else:
continue
else:
img1[j,l]=img1[j,l]
for j in range(2,298):
num1=0
for l in range (2,298):
if img1[j,l]!=0 and img1[j,l+1]==0 and img1[j,l-1]!=0 and img1[j,l+2]==0 and img1[j,l-2]!=0:
num1=num1+1
if num1<2:
img1[j, l] = 0
img1[j, l-1] = 0
# img1[j, l-2] = 0
else:
continue
else:
img1[j,l]=img1[j,l]
img1 = img1.T
for j in range(2, 298):
num2= 0
for l in range(2, 298):
if img1[j, l] != 0 and img1[j, l + 1] != 0 and img1[j, l - 1] == 0 and img1[j, l + 2] != 0 and img1[
j, l - 2] == 0:
num2 = num2 + 1
if num2 < 2:
img1[j, l] = 0
img1[j, l + 1] = 0
# img1[j, l + 2] = 0
else:
continue
else:
img1[j, l] = img1[j, l]
for j in range(2, 298):
num3 = 0
for l in range(2, 298):
if img1[j, l] != 0 and img1[j, l + 1] == 0 and img1[j, l - 1] != 0 and img1[j, l + 2] == 0 and img1[
j, l - 2] != 0:
num3 = num3 + 1
if num3 < 2:
img1[j, l] = 0
img1[j, l - 1] = 0
# img1[j, l - 2] = 0
else:
continue
else:
img1[j, l] = img1[j, l]
img1 = img1.T
cv2.imwrite("C:/Users\lenovo\Desktop\shiyan\h_image/" + str(i).zfill(4) + ".png", img1)
img1 = Image.fromarray(img1)
img1 = img1.transpose(Image.FLIP_TOP_BOTTOM)
img1 = np.array(img1)
temp = []
for j in range(0,300):
for l in range (0,300):
if img1[j,l]!=0:
arr=np.array([j,l,img1[j,l]])
temp.append(arr)
else:
continue
temp=np.array(temp)
np.savetxt("C:/Users\lenovo\Desktop\shiyan/finall_txt/" + str(i).zfill(4) + ".txt", temp, fmt="%.2f")
深度图像预处理并生成点云
于 2022-03-07 11:31:05 首次发布