深度图像预处理并生成点云

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")




















  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值