我试图实现简单的直方图计算算法,一切都适用于算法。但是,当我想使用cv2.imshow显示输入图像然后关闭它并显示图像的直方图(我已实现)时,出现以下错误:
/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() /usr/lib/python2.7/dist-packages/gi/module.py: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
节点!= NULL'失败 type_ = g_type.pytype
这是我的模块(直方图类)
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()
这是一个测试脚本:
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)
/////
当我注释掉一个并且仅使用cv2.imshow('输入图像',img)或histogram.plot()时一切正常,但是当我在同一个脚本中使用它们时cv2.waitKey(0)之后的错误
也许它们中的两个之间存在冲突,就像窗口句柄一样。怎么办?
答案 0 :(得分:1)
问题解决了:
我已经解决了这个问题。使用gtk + 3的matplotlib后端,而cv2.show()使用gtk2.x来处理
我添加了1行matplotlib.use('GTKAgg')告诉matplotlib使用gtk2绘制画布。所以它看起来像这样
...
import matplotlib
matplotlib.use('GTKAgg')
import matplotlib.pyplot as plt
...
而且,我还将“import matplotlib.pyplot as plt”重新定位到模块文件的顶部,一切正常