2014-04-02 76 views
0

我正在從用戶處獲取圖像,並使用標籤顯示它。現在我必須使用該圖像進行進一步處理。 我的代碼是:如何從標籤中讀取圖像以進行進一步處理 - tkinter,python

from Tkinter import Tk, Frame, BOTH 
from Tkinter import * 
import cv2 
from collections import * 
from experiment import * 
from scipy.spatial import distance 
import Tkinter,tkFileDialog 
from PIL import Image, ImageTk 
class Example(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent)    
     self.parent = parent   
     self.initUI() 
    def initUI(self): 
     self.parent.title("PISE") 
     self.pack(fill=BOTH, expand=1) 
def query(): 
    path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')]) 
    im = Image.open(path) 
    tkimage = ImageTk.PhotoImage(im) 
    myvar=Label(root,image = tkimage) 
    myvar.image = tkimage 
    myvar.pack() 
    myvar.place(x = 850, y = 5) 
    custName.set(path) 
    cont_list1 = list() 
    ene_list1 = list() 
    homo_list1 = list() 
    cor_list1 = list() 
    B_mean1 = list() 
    G_mean1 = list() 
    R_mean1 = list() 
    graylist1 = list() 
    resizelist1 = list() 
    eq_graylist1 = list() 
    dis_list1 = list() 
    imge = cv2.imread(tkimage) 
    arr = array(imge) 
    g_img = cv2.imread(tkimage,0) 
    gray_re_img = cv2.resize(g_img,(256,256)) 
    graylist1.append(gray_re_img) 
    equ = cv2.equalizeHist(gray_re_img) 
    eq_graylist1.append(equ) 
    re_img = cv2.resize(imge,(256,256)) 
    resizelist1.append(re_img) 
    blue, green, red = cv2.split(re_img) 
    total = re_img.size 
    B = sum(blue)/total 
    G = sum(green)/total 
    R = sum(red)/total 
    B_mean1.append(B) 
    G_mean1.append(G) 
    R_mean1.append(R) 
    im = skimage.io.imread(tkimage, as_grey=True) 
    im = skimage.img_as_ubyte(im) 
    im /= 32 
    g = skimage.feature.greycomatrix(im, [1], [0], levels=8, symmetric=False, normed=True) 
    cont = skimage.feature.greycoprops(g, 'contrast')[0][0] 
    cont_list1.append(cont) 
    ene = skimage.feature.greycoprops(g, 'energy')[0][0] 
    ene_list1.append(ene) 
    homo = skimage.feature.greycoprops(g, 'homogeneity')[0][0] 
    homo_list1.append(homo) 
    cor = skimage.feature.greycoprops(g, 'correlation')[0][0] 
    cor_list1.append(cor) 
    dis = skimage.feature.greycoprops(g, 'dissimilarity')[0][0] 
    dis_list1.append(dis) 
    feature_matrix_ip = zip(B_mean1 , G_mean1 , R_mean1, cont_list1 , ene_list1 , homo_list1 , cor_list1 , dis_list1) 
root = Tk() 
root.geometry("1105x605+300+300") 
app = Example(root) 
custName = StringVar(None) 
yourName = Entry(app, textvariable=custName) 
yourName.focus_set() 
yourName.pack(padx = 20, pady = 20,anchor='n') 
yourName.place(y = 25, x = 100, width = 500, height = 25) 
button1 = Button(app, text='Select the Query Image',command = query) 
button1.pack(padx = 2, pady = 2,anchor='ne') 
button1.place(x = 550, y = 25) 
root.mainloop() 

但它給我的錯誤,如:

imge = cv2.imread(tkimage) 
TypeError: expected string or Unicode object, instance found 

如何克服這種錯誤?

感謝您的支持!

回答

2

您正在給cv2.imread一個PhotoImage實例,但它期望一個文件名。改爲給它path

imge = cv2.imread(path) 
相關問題