简体   繁体   English

Ubuntu:将cv2.imshow()与pyplot.plot()一起使用时出错

[英]Ubuntu : error using cv2.imshow() with pyplot.plot()

I trying to implement simple Histogram calculation algorithm, everything work fine for the algorithm. 我尝试实现简单的直方图计算算法,该算法一切正常。 But when I want to use cv2.imshow to show the input image then close it and show the histogram plot for the image (which I've implemented) the following error showed up: 但是,当我想使用cv2.imshow显示输入图像然后关闭它并显示图像的直方图(我已经实现)时,出现以下错误:

/usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: cannot register existing type GtkWidget' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: cannot add class private field to invalid type '<invalid>' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_add_interface_static: assertion G_TYPE_IS_INSTANTIATABLE (instance_type)' failed g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: cannot register existing type GtkBuildable' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_interface_add_prerequisite: assertion G_TYPE_IS_INTERFACE (interface_type)' failed g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_once_init_leave: assertion result != 0' failed g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:146: Warning: g_type_get_qdata: assertion node != NULL' faile /usr/lib/python2.7/dist-packages/gi/module.py:142:警告:无法注册现有类型的GtkWidget' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: cannot add class private field to invalid type '<invalid>' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_add_interface_static: assertion G_TYPE_IS_INSTANTIATABLE(instance_type)'失败g_type = info.get_g_type() GtkWidget' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: cannot add class private field to invalid type '<invalid>' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_add_interface_static: assertion lib GtkWidget' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: cannot add class private field to invalid type '<invalid>' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_add_interface_static: assertion dist- GtkWidget' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: cannot add class private field to invalid type '<invalid>' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_add_interface_static: assertion gi GtkWidget' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: cannot add class private field to invalid type '<invalid>' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_add_interface_static: assertion 142:警告:无法注册现有类型GtkBuildable' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_interface_add_prerequisite: assertion /usr/lib/python2.7/dist-packages/gi/ GtkBuildable' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_interface_add_prerequisite: assertion 142 GtkBuildable' g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:142: Warning: g_type_interface_add_prerequisite: assertion G_TYPE_IS_INTERFACE(interface_type)'失败g_type = info.get_g_type()/ usr / lib / python2。 7 / dist-packages / gi / module.py:142:警告:g_once_init_leave:断言result != 0' failed g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:146: Warning: g_type_get_qdata: assertion /usr/lib/python2.7/dist-packages/gi/module.py result != 0' failed g_type = info.get_g_type() /usr/lib/python2.7/dist-packages/gi/module.py:146: Warning: g_type_get_qdata: assertion节点!= NULL'失败 d type_ = g_type.pytype d type_ = g_type.pytype

and this is my modules (Histogram class) 这是我的模块(直方图类)

class Hist(object):


    def __init__(self,image_source):

        self.img_scr = image_source
            self.hist_profile = self.calculate_histogram()

    def calculate_histogram(self):

            row = len(self.img_scr)
            column = len(self.img_scr[0])
            histogram = [0]*256

            for m in range(0,row):
                 for n in range(0,column):
                     pixel_value = self.img_scr[m][n]
                          histogram[pixel_value] += 1

            return histogram

    def plot(self):

            import matplotlib.pyplot as plt
            xAxis = range(0,256)
            yAxis = self.hist_profile
            plt.plot(xAxis,yAxis)
            plt.xlabel('Intensity Value')
            plt.ylabel('Frequency')
            plt.show()

And this is a test script: 这是一个测试脚本:

import numpy as np
import Histogram as hist


img = cv2.imread('A.jpg',0)
cv2.imshow('Input image',img)
print 'Press any key to continue...'
cv2.waitKey(0)
cv2.destroyAllWindows()


histogram = hist.Hist(img)
histogram.plot()
print 'Press any key to end program.'
cv2.waitKey(0)

///// /////

When I comment out one and use only cv2.imshow('Input image',img) or histogram.plot() everything work fine, but when I use they both in same script the error after cv2.waitKey(0) 当我注释掉一个并且仅使用cv2.imshow('Input image',img)或histogram.plot()时,一切正常,但是当我在同一脚本中同时使用它们时,cv2.waitKey(0)之后出现错误

Maybe it is anything conflict between 2 of them, something like window handle . 其中两个之间可能有任何冲突,例如window handle。 What to do? 该怎么办?

Problem solved: 问题解决了:

I have solved the problem. 我已经解决了问题。 It is the matplotlib backend that use gtk+3 while cv2.show() use gtk2.x to process 是matplotlib后端使用gtk + 3,而cv2.show()使用gtk2.x进行处理

I have add 1 line of matplotlib.use('GTKAgg') to tell matplotlib to use gtk2 to draw a canvas. 我添加了1行matplotlib.use('GTKAgg')来告诉matplotlib使用gtk2绘制画布。 So it is look like this 所以看起来像这样

...
import matplotlib
matplotlib.use('GTKAgg')
import matplotlib.pyplot as plt
...

And,I also reposition the " import matplotlib.pyplot as plt " to the top of module file and everything work fine 并且,我还将“ import matplotlib.pyplot as plt”重新定位到模块文件的顶部,一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM