0

I'm trying to apply the grabcut algorithm to a college work, so I'm trying to display the image through Canvas so the user can interact with it by drawing the mask, but when I try to apply the Canvas function, it's not displaying the selected image , it's just displaying the canvas window with no image

The code:

from tkinter import *
from PIL import Image
from PIL import ImageTk
import cv2
import numpy as np
import tkinter.filedialog

class GrabCutGUI(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.iniciaUI()

    def iniciaUI(self):
        self.master.title("Janela da Imagem Segmentada")
        self.pack() 

        imagem = self.carregaImagemASerExibida()

        self_width, self_height = self.master.size()
        self.canvas = Canvas(self.master, width = self_width, height = self_height, cursor = "cross")#<-A think the error is here, but any tutorial that i found explain this function this way

        self.canvas.create_image(0, 0, anchor = NW, image = imagem)
        self.canvas.pack()

    def carregaImagemASerExibida(self):
        caminhoDaImagem = tkinter.filedialog.askopenfilename()

        if(caminhoDaImagem != ""):
           self.imagemOpenCV = cv2.imread(caminhoDaImagem)
           image = cv2.cvtColor(self.imagemOpenCV, cv2.COLOR_BGR2RGB)
           image = Image.fromarray(image)
           image = ImageTk.PhotoImage(image)

           return image

def main():
    root = Tk()
    appcut = GrabCutGUI(master = root)
    appcut.mainloop() 

if __name__ == "__main__":
    main()

0 Answers0