2
votes

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:

/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: assertionG_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: assertionG_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: assertionnode != NULL' failed 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)

Maybe it is anything conflict between 2 of them, something like window handle . What to do?

1
please fix your indenting. - tacaswell
sorry about the indenting ,edited it already - Natthakit Iewprasert
nice, that you came back with the solution ! - berak
Good work you found your own solution. Please provide your solution as answer yourself and accept it. It will help to mark this question as solved (writing solved in the question is not enough) - Abid Rahman K
Yes, sure , at first I can't post solution as an answer because they said "User with repulation less than 10 have to wait for 10 hour since the question posted". So I edit the post instead, and now I will submit the solution as answer - Natthakit Iewprasert

1 Answers

1
votes

Problem solved:

I have solved the problem. It is the matplotlib backend that use gtk+3 while cv2.show() use gtk2.x to process

I have add 1 line of matplotlib.use('GTKAgg') to tell matplotlib to use gtk2 to draw a canvas. 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