建筑点云转换为二值图像并提取轮廓

1.点云转换为二值图像

def point2picture(filename):
    pcd=o3d.io.read_point_cloud(filename)
    min=pcd.get_min_bound()
    max=pcd.get_max_bound()

    res=0.5
    row=math.ceil((max[1]-min[1])/res)#点云坐标系和屏幕坐标系是反的
    col=math.ceil((max[0]-min[0])/res)

    image=np.zeros((row,col,1),np.uint8)

    for i in tqdm(range(len(pcd.points))):
        temp_row=math.floor((max[1]-(pcd.points[i])[1])/res)
        temp_col=math.floor(((pcd.points[i])[0]-min[0])/res)
        image[temp_row][temp_col]=255
    cv2.imwrite("/home/zhengxin/PV_System/data/temp.png",image)

在这里插入图片描述

2.形态学开运算操作

def open_operation(filename):
    k=np.ones((10,10),np.uint8)
    image=cv2.imread(filename)
    cv2.dilate(cv2.erode(image,k),k)
    cv2.imwrite("/home/zhengxin/PV_System/data/temp_open.png",image)

在这里插入图片描述

3.Canny边缘检测并寻找轮廓

def find_contours(val):
    threshold = val
    canny_output = cv2.Canny(src_gray, threshold, threshold * 2)#Canny(0,1)
    contours, hierarchy = cv2.findContours(canny_output, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)#edge
    drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)
    for i in range(len(contours)):
        color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
        cv2.drawContours(drawing, contours, i, color, 2, cv2.LINE_8, hierarchy, 0)
    cv2.imshow('Contours', drawing)

在这里插入图片描述

4.主函数

pc_path="/home/zhengxin/PV_System/data/building.pcd"
img_path="/home/zhengxin/PV_System/data/temp.png"
open_path="/home/zhengxin/PV_System/data/temp_open.png"
point2picture(pc_path)
open_operation(img_path)

src_gray=cv2.imread(open_path,cv2.IMREAD_GRAYSCALE)
src_gray = cv2.blur(src_gray, (3,3))

# Create Window
source_window = 'Source'
cv2.namedWindow(source_window)
cv2.imshow(source_window, src_gray)
max_thresh = 255
thresh = 100 # initial threshold
cv2.createTrackbar('Canny Thresh:', source_window, thresh, max_thresh, find_contours)
find_contours(thresh)
cv2.waitKey()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值