2201_76011993 2023-01-11 00:29 采纳率: 100%
浏览 49
已结题

使用pyqtgraph无法在子窗口和父窗口都绘制3D图

在使用pyqt5和pyqtgraph绘制3D散点图的时候,我设置了两个窗口,实现在父窗口中根据读取文件绘制了3D图,在父窗口中成功启动子窗口。但是,当我想在子窗口中根据父窗口中读取的文件绘制3D图时报错。即,父窗口中绘制了3D图,子窗口报错;子窗口绘制了3D图,父窗口报错。
部分代码:

img

class MainWindow(QtWidgets.QMainWindow,Ui_zhibeichuli):#主界面

def __init__(self,parent=None):
    super(MainWindow,self).__init__(parent)
    self.setupUi(self)
    '''定义信号槽'''

    self.actionfile_PcdLas_open.triggered.connect(self.open_file_pointCloud_pcd_las)
    #self.action_exit.triggered.connect(MainWindow.close)

    self.action_save.triggered.connect(self.save_point_cloud)
    self.action_slope_chuli.triggered.connect(self.open_son_slope)
    '''初始化显示布局'''
    self.pointView = GLViewWidget()
    self.gridLayout.addWidget(self.pointView, 0, 1)
    self.grid_item = gl.GLGridItem(QVector3D(100, 100, 1))#生成网格
    self.pointView.addItem(self.grid_item)
    '''初始三维坐标'''
    self.x_axis_item = gl.GLLinePlotItem(pos=np.array([[0, 0, 0], [10, 0, 0]], dtype=np.float32),
                                         color=(1, 0, 0, 1),
                                         width=2)
    self.pointView.addItem(self.x_axis_item)
    self.y_axis_item = gl.GLLinePlotItem(pos=np.array([[0, 0, 0], [0, 10, 0]], dtype=np.float32),
                                         color=(0, 1, 0, 1),
                                         width=2)
    self.pointView.addItem(self.y_axis_item)
    self.z_axis_item = gl.GLLinePlotItem(pos=np.array([[0, 0, 0], [0, 0, 10]], dtype=np.float32),
                                         color=(0, 0, 1, 1),
                                         width=2)
    self.pointView.addItem(self.z_axis_item)

打开文件部分(父窗口):
'''槽函数,打开点云文件,*.pcd *.las'''
def open_file_pointCloud_pcd_las(self):
global slope_pos_pcd_las#存储xyz坐标

    from PyQt5.QtWidgets import QFileDialog
    dir=QFileDialog()#创建文件对话框
    dir.setFileMode(QFileDialog.ExistingFiles)#设置多选
    #设置只显示的文件类型
    dir.setNameFilter('点云文件(*.pcd *.las)')

    if dir.exec_():#判断是否选择了文件

        (raod_pcd_las_name, extension) = os.path.splitext(dir.selectedFiles()[0])#获取文件后缀
        if extension=='.pcd':
            pcd = o3d.io.read_point_cloud(dir.selectedFiles()[0])#根据文件地址读取文件
            xyz_pcd = np.array(pcd.points)#转为np数组
            #Point_Cloud_chuli().show_point_before(xyz_pcd)#显示pcd
            slope_pos_pcd_las=xyz_pcd#全局变量存储,用于坡度处理调用
            self.pointView.clear()#清除布局
            '''重新添加布局'''
            self.pointView = GLViewWidget()
            self.gridLayout.addWidget(self.pointView, 0, 1)
            self.grid_item = gl.GLGridItem(QVector3D(100, 100, 1))
            self.pointView.addItem(self.grid_item)
            points = xyz_pcd
            '''用于点云的坐标变换'''
            x_list = points[..., 0]
            y_list = points[..., 1]
            z_list = points[..., 2]
            # x, y, z = points[0, 0], points[0, 1], points[0, 2]
            x, y, z = min(x_list), min(y_list), min(z_list)
            x_trans = x_list - x
            y_trans = y_list - y
            z_trans = z_list - z
            n = len(x_trans)
            points_trans = np.zeros((n, 3))
            points_trans[..., 0] = x_trans
            points_trans[..., 1] = y_trans
            points_trans[..., 2] = z_trans
            '''坐标轴xyz'''
            self.x_axis_item = gl.GLLinePlotItem(pos=np.array([[0, 0, 0], [10, 0, 0]], dtype=np.float32),
                                                 color=(1, 0, 0, 1),
                                                 width=2)
            self.pointView.addItem(self.x_axis_item)
            self.y_axis_item = gl.GLLinePlotItem(pos=np.array([[0, 0, 0], [0, 10, 0]], dtype=np.float32),
                                                 color=(0, 1, 0, 1),
                                                 width=2)
            self.pointView.addItem(self.y_axis_item)
            self.z_axis_item = gl.GLLinePlotItem(pos=np.array([[0, 0, 0], [0, 0, 10]], dtype=np.float32),
                                                 color=(0, 0, 1, 1),
                                                 width=2)
            self.pointView.addItem(self.z_axis_item)
            '''点云显示样式'''
            colors = np.ones((n, 4))
            size = np.zeros(shape=points.shape[0])
            self.points_plot_item = gl.GLScatterPlotItem(pos=points_trans,
                                                         color=colors,
                                                         size=size,
                                                         pxMode=False)
            self.pointView.addItem(self.points_plot_item)

打开子窗口:
def open_son_slope(self):

    self.op=Son_slopeWindow()
    self.op.show()

子窗口:
class Son_slopeWindow(QtWidgets.QMainWindow,Ui_slope_chuli):
def init(self):
super(Son_slopeWindow,self).init()
self.setupUi(self)
self.pushButton.clicked.connect(self.slope_show)
'''----------'''
pcd = o3d.io.read_point_cloud("D:/desk/002.pcd")

    filtered_cloud = Point_Cloud_chuli().slope_chuli(pcd, 2, 0.5,1)

    points = np.array(filtered_cloud.points)

    '''用于点云的坐标变换'''
    x_list = points[..., 0]
    y_list = points[..., 1]
    z_list = points[..., 2]
    # x, y, z = points[0, 0], points[0, 1], points[0, 2]
    x, y, z = min(x_list), min(y_list), min(z_list)
    x_trans = x_list - x
    y_trans = y_list - y
    z_trans = z_list - z
    n = len(x_trans)

    points_trans = np.zeros((n, 3))
    points_trans[..., 0] = x_trans
    points_trans[..., 1] = y_trans
    points_trans[..., 2] = z_trans

    '''--------'''

    self.points_afterView=GLViewWidget()
    self.gridLayout_2.addWidget(self.points_afterView,0,1)

    self.grid_ite=gl.GLGridItem(QVector3D(100,100,1))
    self.points_afterView.addItem(self.grid_ite)

    self.x_axis=gl.GLLinePlotItem(pos=np.array([[0,0,0],[10,0,0]]),
                                  color=(1,0,0,1),
                                  width=2)
    self.points_afterView.addItem(self.x_axis)
    self.y_axis = gl.GLLinePlotItem(pos=np.array([[0, 0, 0], [0, 10, 0]]),
                                    color=(0, 1, 0, 1),
                                    width=2)
    self.points_afterView.addItem(self.y_axis)
    self.z_axis = gl.GLLinePlotItem(pos=np.array([[0, 0, 0], [0, 0, 10]]),
                                    color=(0, 0, 1, 1),
                                    width=2)
    self.points_afterView.addItem(self.z_axis)
    '''-----'''
    colors = np.ones((n, 4))
    size = np.zeros(n)

    self.points_plot_after = gl.GLScatterPlotItem(pos=points_trans,
                                                  color=colors,
                                                  size=size,
                                                  pxMode=False)
    self.points_afterView.addItem(self.points_plot_after)

报错类型:

img

可以说明的是,文件及处理后的文件的数据没问题。

  • 写回答

2条回答 默认 最新

  • qq_1311209878 2023-01-11 08:25
    关注

    建议你把报错提示 以文本的形式复制出来大家解答

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月19日
  • 已采纳回答 1月11日
  • 赞助了问题酬金15元 1月11日
  • 请提交代码 1月11日
  • 展开全部

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上