Natthakit Iewprasert
Natthakit Iewprasert

Reputation: 51

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:

/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?

Upvotes: 3

Views: 2328

Answers (1)

Natthakit Iewprasert
Natthakit Iewprasert

Reputation: 51

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

Upvotes: 2

Related Questions