<tfoot id='8vkmh'></tfoot>
<i id='8vkmh'><tr id='8vkmh'><dt id='8vkmh'><q id='8vkmh'><span id='8vkmh'><b id='8vkmh'><form id='8vkmh'><ins id='8vkmh'></ins><ul id='8vkmh'></ul><sub id='8vkmh'></sub></form><legend id='8vkmh'></legend><bdo id='8vkmh'><pre id='8vkmh'><center id='8vkmh'></center></pre></bdo></b><th id='8vkmh'></th></span></q></dt></tr></i><div id='8vkmh'><tfoot id='8vkmh'></tfoot><dl id='8vkmh'><fieldset id='8vkmh'></fieldset></dl></div>

  • <small id='8vkmh'></small><noframes id='8vkmh'>

          <bdo id='8vkmh'></bdo><ul id='8vkmh'></ul>
        <legend id='8vkmh'><style id='8vkmh'><dir id='8vkmh'><q id='8vkmh'></q></dir></style></legend>

        在 QGraphicsView 中播放图像序列(神秘的内存泄漏

        时间:2023-08-05
      1. <tfoot id='xROUO'></tfoot>
        • <legend id='xROUO'><style id='xROUO'><dir id='xROUO'><q id='xROUO'></q></dir></style></legend>

          <small id='xROUO'></small><noframes id='xROUO'>

            <bdo id='xROUO'></bdo><ul id='xROUO'></ul>
            <i id='xROUO'><tr id='xROUO'><dt id='xROUO'><q id='xROUO'><span id='xROUO'><b id='xROUO'><form id='xROUO'><ins id='xROUO'></ins><ul id='xROUO'></ul><sub id='xROUO'></sub></form><legend id='xROUO'></legend><bdo id='xROUO'><pre id='xROUO'><center id='xROUO'></center></pre></bdo></b><th id='xROUO'></th></span></q></dt></tr></i><div id='xROUO'><tfoot id='xROUO'></tfoot><dl id='xROUO'><fieldset id='xROUO'></fieldset></dl></div>

                    <tbody id='xROUO'></tbody>
                  本文介绍了在 QGraphicsView 中播放图像序列(神秘的内存泄漏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用 OpenCV 和 PyQt5 处理图像序列并制作结果视频.我有一些代码循环遍历目录、读取图像并尝试QGraphicsView上显示它们.

                  I'm trying to process an image sequence and make a video of the results using OpenCV and PyQt5. I've got some code that loops through a directory, reads in the images, and tries to display them on a QGraphicsView.

                  def on_start(self):
                      for f in self.image_list:
                          img = cv2.imread(f)
                          img = cv2qimage(img, False)
                          self.scene.set_qimage(img)
                  

                  self.scene 继承自 QGraphicsScene.

                    def set_qimage(self, qimage):
                          self.pixmap = QPixmap.fromImage(qimage)
                          self.addPixmap(self.pixmap)
                  

                  问题是每次我调用 addPixmap() 时,图像都会添加到所有其他图像之上,很快我就会耗尽内存,一切都崩溃了.当前代码不包含任何处理步骤,它只是将 numpy ndarry 转换为 QImage 并将 QPixmap 添加到场景中.

                  The problem is everytime I call addPixmap() the image is just added on top of all the other images and soon I run out of memory and everything crashes. The current code doesn't include any of the processing steps, it just converts the numpy ndarry to a QImage and adds the QPixmap to the scene.

                  更新 QGraphicsScene 以便我可以流式传输一系列图像的正确方法是什么?

                  推荐答案

                  每次使用 addPixmap() 时,您都会创建一个新的 QGraphicsPixmapItem,从而不必要地添加内存.解决方案是创建一个 QGraphicsPixmapItem 并重用它.另外处理任务会阻塞主线程,所以必须使用线程来完成繁重的任务,并通过信号发送QImage.

                  Every time you use addPixmap() you are creating a new QGraphicsPixmapItem adding memory unnecessarily. The solution is to create a QGraphicsPixmapItem and reuse it. In addition the processing task can block the main thread so you must use a thread to do the heavy task and send the QImage through signals.

                  class ProcessWorker(QObject):
                      imageChanged = pyqtSignal(QImage)
                  
                      def doWork(self):
                          for f in self.image_list:
                              img = cv2.imread(f)
                              img = cv2qimage(img, False)
                              self.imageChanged.emit(img)
                              QThread.msleep(1)
                  
                  class Widget(QWidget):
                      def __init__(self, parent=None):
                          QWidget.__init__(self, parent)
                          lay = QVBoxLayout(self)
                          gv = QGraphicsView()
                          lay.addWidget(gv)
                          scene = QGraphicsScene(self)
                          gv.setScene(scene)
                          self.pixmap_item = QGraphicsPixmapItem()
                          scene.addItem(self.pixmap_item)
                  
                          self.workerThread = QThread()
                          self.worker = ProcessWorker()
                          self.worker.moveToThread(self.workerThread)
                          self.workerThread.finished.connect(self.worker.deleteLater)
                          self.workerThread.started.connect(self.worker.doWork)
                          self.worker.imageChanged.connect(self.setImage)
                          self.workerThread.start()
                  
                  
                      @pyqtSlot(QImage)
                      def setImage(self, image):
                          pixmap = QPixmap.fromImage(image)
                          self.pixmap_item.setPixmap(pixmap)
                  
                  
                  if __name__ == '__main__':
                      app = QApplication(sys.argv)
                      w = Widget()
                      w.show()
                      sys.exit(app.exec_())
                  

                  这篇关于在 QGraphicsView 中播放图像序列(神秘的内存泄漏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PyQt5 closeEvent 方法 下一篇:PyQt5 和 OpenCV 有类似的库;如何避免两者之间的冲

                  相关文章

                  最新文章

                  <legend id='a2OAW'><style id='a2OAW'><dir id='a2OAW'><q id='a2OAW'></q></dir></style></legend>
                1. <small id='a2OAW'></small><noframes id='a2OAW'>

                2. <tfoot id='a2OAW'></tfoot>
                3. <i id='a2OAW'><tr id='a2OAW'><dt id='a2OAW'><q id='a2OAW'><span id='a2OAW'><b id='a2OAW'><form id='a2OAW'><ins id='a2OAW'></ins><ul id='a2OAW'></ul><sub id='a2OAW'></sub></form><legend id='a2OAW'></legend><bdo id='a2OAW'><pre id='a2OAW'><center id='a2OAW'></center></pre></bdo></b><th id='a2OAW'></th></span></q></dt></tr></i><div id='a2OAW'><tfoot id='a2OAW'></tfoot><dl id='a2OAW'><fieldset id='a2OAW'></fieldset></dl></div>
                    <bdo id='a2OAW'></bdo><ul id='a2OAW'></ul>