How to display figure in the same windows using tkinter?

Viewed 52

I'm working on "Image retrieval", which take query image from user and displaying 10 images ( top ) which similar to query. I'm made the UI using tkinter, the images displaying as a plot in new figure, but I want to display display them in the first windows under "upload image" button.

Output images

Also, if there is another way is better than it, (instead of ploting it) please inform me.

This is my code:

import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
from PIL import Image, ImageTk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib

matplotlib.use("TkAgg")

windows = tk.Tk()
windows.geometry("380x250")  # Size of the window

windows.title('Content-based Medical Image Retrieval')
font_style = ('Comic Sans MS', 16, 'bold')
l1 = tk.Label(windows, text='Upload Image & Display Results',
              width=30, font=font_style)
#l1.grid(row=1, column=1, columnspan=4)
l1.pack( expand=1)
font_style = ('Calibri', 10, 'bold')
upload_button = tk.Button(windows, text='Upload Image',
               width=20,height = 2,font=font_style, command=lambda: upload_file())
#upload_button.grid(row=2, column=1, columnspan=4)
upload_button.pack( expand=1)

def upload_file():
    f_types = [('Jpg Files', '*.jpg'),
               ('PNG Files', '*.png')]   # type of files to select
    filename = tk.filedialog.askopenfilename(multiple=True, filetypes=f_types)
    class_name = str(list(filename))[-16:-13].replace("/","")
    print(class_name)
    list_all_images_in_class = os.listdir(dataset_path+'/'+class_name)

    image_path = str(list(filename))[2:-2]
    print(image_path)
    average_precision=0
    list_all_images_in_class=os.listdir(dataset_path+'/'+class_name)
    query_image = cv2.imread(image_path)
    query_image = cv2.cvtColor(query_image, cv2.COLOR_BGR2GRAY)  
    gabor_output=Gabor_Filter(query_image)
    schmid_output=Schmid_Filter(query_image)
    query_vectors=Image_Partition(gabor_output+schmid_output)
    query_vectors = np.array(query_vectors)
    query_classify=kmeans.predict(query_vectors)
    query_histogram,_=np.histogram(query_classify,bins=range(n_dic+1),density=True)

    list_amount_similarity = []
    for x in range(num_of_img):    
        similarity_result=hist_match(list_histogram_of_each_image_in_dataset[x],query_histogram)
        list_amount_similarity.append((similarity_result,str(list_images_path[x])))

    total_image_retrieved=10
    top_image_retrieved = sorted(list_amount_similarity,reverse=True)[:total_image_retrieved]

    path_retrieved_image=[]
    retrieved_image_class=[]
    similarity_result=[]
    count =0

    for similarity_amount, image_path in top_image_retrieved:
        similarity_result.append(similarity_amount) 
        path_retrieved_image.append(image_path)
        class_path=os.path.dirname(image_path)
        retrieved_image_class.append(os.path.basename(class_path)) 

    plt.figure(figsize=(10,8))
    plt.subplot(441);plt.imshow(query_image)
    plt.title('Query');plt.axis('off')


    retrieved_image =[]
    relevant_images=0 

    #calculate the pecision for each query 
    #and then add the pecision to the average_precision
    if int(class_name)==1: 
        for i in range(total_image_retrieved):
            retrieved_image.append(imageio.imread(path_retrieved_image[i]))
            plt.subplot(4,4,i+2); plt.imshow(retrieved_image[i])
            
            plt.title('%d, %.4f,%d' % (i,similarity_result[i],int(retrieved_image_class[i])));plt.axis('off')  
            if int(retrieved_image_class[i])==1:
                relevant_images +=1
        precision=relevant_images/total_image_retrieved
        average_precision+=precision      

    elif int(class_name) ==2:
        for i in range(total_image_retrieved):
            retrieved_image.append(imageio.imread(path_retrieved_image[i]))
            plt.subplot(4,4,i+2); plt.imshow(retrieved_image[i])
            plt.title('%d, %.4f,%d' % (i,similarity_result[i],int(retrieved_image_class[i])));plt.axis('off')  
            #sim.setText(f"similarity: {int(retrived_imgs[i][0] * 100)}%")
            if int(retrieved_image_class[i])==2:
                relevant_images +=1
        precision=relevant_images/total_image_retrieved
        average_precision+=precision 
        
windows.mainloop()
0 Answers
Related