为什么OpenCV和Qt窗口不能在同一个应用程序中一起工作?

时间:2016-06-24 12:37:06

标签: python c++ qt opencv

我正在尝试创建一个应用程序,它在python中显示一些Qt窗口的小部件,它控制显示OpenCV窗口的C ++对象。 当我单独调用每个代码路径时,窗口小部件和OpenCV窗口显示,没有任何问题。 但是,当它们被一起调用时,它们都不会被显示出来。

这是我的测试代码:

display.cpp

#include <boost/python.hpp>
#include <string>
#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>



class Display
{
public:
    Display()
    {
        std::cout << "Display built" << std::endl;
    }

    void show()
    {
        std::cout << "Print function" << std::endl;
        cv::Mat1b mat(480, 640);
        cv::imshow("Display", mat);
        cv::waitKey(0);
    }
};

BOOST_PYTHON_MODULE(display)
{
    boost::python::class_<Display>("Display")
        .def("show", &Display::show)
    ;
};

analysis.py

#! /usr/bin/python
# -*- coding: utf-8 -*-

import sys
import numpy as np
from PySide import QtGui, QtCore
import PySide

import threading
import time
import display

class DisplayThread (threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        print "Starting..."
        disp = display.Display()
        disp.show()



class Analysis(QtGui.QWidget):

    def __init__(self):
        super(Analysis, self).__init__()

        self.initUI()

    def initUI(self):      

        hbox = QtGui.QHBoxLayout(self)
        img = np.random.randint(0, 256, size=(100,100)).astype(np.uint8).flatten()
        qimg = PySide.QtGui.QImage(img, 100, 100, PySide.QtGui.QImage.Format_Indexed8)

        pixmap = QtGui.QPixmap(qimg)

        lbl = QtGui.QLabel(self)
        lbl.setPixmap(pixmap)

        hbox.addWidget(lbl)
        self.setLayout(hbox)

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Red Rock')
        self.show()        

def main():
    thread = DisplayThread()
    thread.start()
    app = QtGui.QApplication(sys.argv)
    ex = Analysis()
    sys.exit(app.exec_())
    thread.join()

if __name__ == '__main__':
    main()

我的cmake文件构建了所有内容:

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)

FIND_PACKAGE(PythonInterp)
FIND_PACKAGE(PythonLibs)
FIND_PACKAGE(Boost COMPONENTS python)
FIND_PACKAGE(OPENCV 2.4 REQUIRED opencv_core opencv_highgui opencv_imgproc opencv_calib3d opencv_video opencv_features2d opencv_objdetect)

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
LINK_LIBRARIES(${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${OPENCV_LIBRARIES}) # Deprecated but so convenient!

PYTHON_ADD_MODULE(display src/display.cpp)
FILE(COPY src/analysis.py DESTINATION .)

如果我构建所有内容并对该行进行注释,则会显示“thread.start()”我的Qt窗口。同样,当我只评论这些行时:

app = QtGui.QApplication(sys.argv)
ex = Analysis()
sys.exit(app.exec_())

OpenCV窗口很好地显示。

但两者都收到Gtk错误:

(python:21036): Gtk-WARNING **: gtk_disable_setlocale() must be called before gtk_init()

没有显示任何窗口。 我正在使用Ubuntu 16.04 64bit的默认软件包。 我该如何解决这个问题?

0 个答案:

没有答案