Advertisement
Guest User

Untitled

a guest
Apr 18th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.51 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #import scipy
  4. #import numpy as np
  5. from Tkinter import *
  6. import tkFileDialog
  7. from PIL import Image as IM
  8. import ImageTk
  9. import cPickle as pickle
  10. import array
  11.  
  12. global color_dict
  13. global original_image
  14. global our_image
  15.  
  16. # This dictionary will hold all our image data
  17. color_dict = {}
  18. color_dict["orange"] = []
  19. color_dict["yellow"] = []
  20. color_dict["cyan"] = []
  21. color_dict["green"] = []
  22. color_dict["white"] = []
  23. color_dict["robot_blue"] = []
  24. color_dict["robot_pink"] = []
  25.  
  26. class _Colortable_Generator:
  27.     def __init__(self, master):
  28.         self.initComplete = 0
  29.         frame = Frame(master, width=900, height=600)
  30.         frame.pack()
  31.         self.master = master
  32.         self.x, self.y, self.w, self.h = -1,-1,-1,-1
  33.        
  34.         self.master.bind('<Enter>', self.bindConfigure)
  35.  
  36.         # This stuff all generates the main GUI itself
  37.  
  38.         self.master.title("Colortable_Generator")
  39.  
  40.         self.Button_3 = Button(self.master,text="Save Colors", width="15")
  41.         self.Button_3.place(x=24, y=468, width=117, height=28)
  42.         self.Button_3.bind("<ButtonRelease-1>", self.Button_3_Click)
  43.  
  44.         self.Button_4 = Button(self.master,text="Load Colors", width="15")
  45.         self.Button_4.place(x=24, y=516, width=116, height=27)
  46.         self.Button_4.bind("<ButtonRelease-1>", self.Button_4_Click)
  47.  
  48.         self.Button_5 = Button(self.master,text="Quit", width="15")
  49.         self.Button_5.place(x=24, y=566, width=116, height=27)
  50.         self.Button_5.bind("<ButtonRelease-1>", quit)
  51.  
  52.         self.Button_6 = Button(self.master,text="Write Look-Up Table", width="15")
  53.         self.Button_6.place(x=424, y=540, width=120, height=27)
  54.         self.Button_6.bind("<ButtonRelease-1>", self.Button_6_Click)
  55.  
  56.         self.Clear_Select_Button = Button(self.master,text="Clear Selected Colors", width="15")
  57.         self.Clear_Select_Button.place(x=24, y=360, width=127, height=28)
  58.         self.Clear_Select_Button.bind("<ButtonRelease-1>", self.Clear_Select_Button_Click)
  59.  
  60.         self.Load_Montage_Button = Button(self.master,text="Load Image", width="15")
  61.         self.Load_Montage_Button.place(x=24, y=36, width=119, height=30)
  62.         self.Load_Montage_Button.bind("<ButtonRelease-1>", self.Load_Montage_Button_Click)
  63.  
  64.         lbframe = Frame( self.master )
  65.         self.Canvas_1_frame = lbframe
  66.         scrollbar = Scrollbar(lbframe, orient=VERTICAL)
  67.         self.Canvas_1 = Canvas(lbframe, width="640", background="white", height="480", yscrollcommand=scrollbar.set)
  68.         scrollbar.config(command=self.Canvas_1.yview)
  69.         scrollbar.pack(side=RIGHT, fill=Y)
  70.         self.Canvas_1.pack(side=LEFT, fill=BOTH, expand=1)
  71.  
  72.         self.Canvas_1_frame.place(x=180, y=36)
  73.         self.Canvas_1.bind("<ButtonRelease-1>", self.Canvas_1_Click)
  74.  
  75.         self.Threshold_Val_Entry = Entry(self.master,width="15")
  76.         self.Threshold_Val_Entry.place(x=60, y=432, width=40, height=22)
  77.         self.Threshold_Val_Entry_StringVar = StringVar()
  78.         self.Threshold_Val_Entry.configure(textvariable=self.Threshold_Val_Entry_StringVar)
  79.         self.Threshold_Val_Entry_StringVar.set("0")
  80.         self.Threshold_Val_Entry_StringVar_traceName = self.Threshold_Val_Entry_StringVar.trace_variable("w", self.Threshold_Val_Entry_StringVar_Callback)
  81.  
  82.         self.Label_1 = Label(self.master,text="Threshold", width="15")
  83.         self.Label_1.place(x=24, y=408, width=112, height=22)
  84.  
  85.         self.Radiobutton_1 = Radiobutton(self.master,text="Orange", value="orange", width="15")
  86.         self.Radiobutton_1.place(x=12, y=84, width=134, height=26)
  87.         self.RadioGroup1_StringVar = StringVar()
  88.         self.RadioGroup1_StringVar.set("orange")
  89.         self.RadioGroup1_StringVar_traceName = self.RadioGroup1_StringVar.trace_variable("w", self.RadioGroup1_StringVar_Callback)
  90.         self.Radiobutton_1.configure(variable=self.RadioGroup1_StringVar )
  91.  
  92.         self.Radiobutton_2 = Radiobutton(self.master,text="Yellow", value="yellow", width="15")
  93.         self.Radiobutton_2.place(x=12, y=120, width=134, height=26)
  94.         self.Radiobutton_2.configure(variable=self.RadioGroup1_StringVar )
  95.  
  96.         self.Radiobutton_3 = Radiobutton(self.master,text="Cyan", value="cyan", width="15")
  97.         self.Radiobutton_3.place(x=12, y=156, width=136, height=26)
  98.         self.Radiobutton_3.configure(variable=self.RadioGroup1_StringVar )
  99.  
  100.         self.Radiobutton_4 = Radiobutton(self.master,text="Field (Green)", value="green", width="15")
  101.         self.Radiobutton_4.place(x=12, y=192, width=140, height=27)
  102.         self.Radiobutton_4.configure(variable=self.RadioGroup1_StringVar )
  103.  
  104.         self.Radiobutton_5 = Radiobutton(self.master,text="White", value="white", width="15")
  105.         self.Radiobutton_5.place(x=12, y=228, width=134, height=26)
  106.         self.Radiobutton_5.configure(variable=self.RadioGroup1_StringVar )
  107.  
  108.         self.Radiobutton_6 = Radiobutton(self.master,text="Robot Blue", value="robot_blue", width="15")
  109.         self.Radiobutton_6.place(x=12, y=264, width=134, height=26)
  110.         self.Radiobutton_6.configure(variable=self.RadioGroup1_StringVar )
  111.  
  112.         self.Radiobutton_7 = Radiobutton(self.master,text="Robot Pink", value="robot_pink", width="15")
  113.         self.Radiobutton_7.place(x=12, y=300, width=134, height=26)
  114.         self.Radiobutton_7.configure(variable=self.RadioGroup1_StringVar )
  115.  
  116.         self.master.resizable(0,0)
  117.  
  118.     # The following two functions are tkinter boilerplate
  119.        
  120.     def bindConfigure(self, event):
  121.         if not self.initComplete:
  122.             self.master.bind("<Configure>", self.Master_Configure)
  123.             self.initComplete = 1
  124.  
  125.  
  126.     def Master_Configure(self, event):
  127.         pass
  128.         if event.widget != self.master:
  129.             if self.w != -1:
  130.                 return
  131.         x = int(self.master.winfo_x())
  132.         y = int(self.master.winfo_y())
  133.         w = int(self.master.winfo_width())
  134.         h = int(self.master.winfo_height())
  135.         if (self.x, self.y, self.w, self.h) == (-1,-1,-1,-1):
  136.             self.x, self.y, self.w, self.h = x,y,w,h
  137.  
  138.  
  139.         if self.w!=w or self.h!=h:
  140.             self.w=w
  141.             self.h=h
  142.  
  143.     def Button_3_Click(self, event): #click method for component ID=15
  144.         pass
  145.         # This uses pickle to save our color dict to a file
  146.         save_file = file("Saved_Colortable", "a")
  147.         pickle.dump(color_dict,save_file)
  148.         save_file.close()
  149.  
  150.     def Button_6_Click(self, event): #click method for component ID=15
  151.         pass
  152.         look_up_table = [0]*262143 # our LUT must have 262143 spots for the 262143 possible color combos
  153.         # Run the following on each color_dict color:
  154.         orange_list = color_dict["orange"]
  155.         yellow_list = color_dict["yellow"]
  156.         cyan_list = color_dict["cyan"]
  157.         green_list = color_dict["green"]
  158.         white_list = color_dict["white"]
  159.         blue_list = color_dict["robot_blue"]
  160.         pink_list = color_dict["robot_pink"]
  161.         y = ()
  162.         u = ()
  163.         v = ()
  164.         for i in range(len(orange_list)):
  165.             color_triple = orange_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
  166.             y = bin(color_triple[0])
  167.             y = y[:-2]
  168.             u = bin(color_triple[1])
  169.             u = u[:-2]
  170.             v = bin(color_triple[2])
  171.             v = v[:-2]
  172.             yuv_space = y + u[2:] + v[2:]
  173.             yuv_space_decimal = int(yuv_space,2)
  174.             offset = 0
  175.             while offset < int(self.Threshold_Val_Entry_StringVar.get()):
  176.                 look_up_table[yuv_space_decimal - offset] = 1 # "orange"
  177.                 look_up_table[yuv_space_decimal + offset] = 1 # "orange"
  178.                 offset += 1
  179.         offset = 0
  180.         for i in range(len(yellow_list)):
  181.             color_triple = yellow_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
  182.             y = bin(color_triple[0])
  183.             y = y[:-2]
  184.             u = bin(color_triple[1])
  185.             u = u[:-2]
  186.             v = bin(color_triple[2])
  187.             v = v[:-2]
  188.             yuv_space = y + u[2:] + v[2:]
  189.             yuv_space_decimal = int(yuv_space,2)
  190.             offset = 0
  191.             while offset < int(self.Threshold_Val_Entry_StringVar.get()):
  192.                 look_up_table[yuv_space_decimal - offset] = 2 #"yellow"
  193.                 look_up_table[yuv_space_decimal + offset] = 2 #"yellow"
  194.                 offset += 1
  195.         offset = 0
  196.         for i in range(len(cyan_list)):
  197.             color_triple = cyan_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
  198.             y = bin(color_triple[0])
  199.             y = y[:-2]
  200.             u = bin(color_triple[1])
  201.             u = u[:-2]
  202.             v = bin(color_triple[2])
  203.             v = v[:-2]
  204.             yuv_space = y + u[2:] + v[2:]
  205.             yuv_space_decimal = int(yuv_space,2)
  206.             offset = 0
  207.             while offset < int(self.Threshold_Val_Entry_StringVar.get()):
  208.                 look_up_table[yuv_space_decimal - offset] = 4 #"cyan"
  209.                 look_up_table[yuv_space_decimal + offset] = 4 #"cyan"
  210.                 offset += 1
  211.         offset = 0
  212.         for i in range(len(green_list)):
  213.             color_triple = green_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
  214.             y = bin(color_triple[0])
  215.             y = y[:-2]
  216.             u = bin(color_triple[1])
  217.             u = u[:-2]
  218.             v = bin(color_triple[2])
  219.             v = v[:-2]
  220.             yuv_space = y + u[2:] + v[2:]
  221.             yuv_space_decimal = int(yuv_space,2)
  222.             offset = 0
  223.             while offset < int(self.Threshold_Val_Entry_StringVar.get()):
  224.                 look_up_table[yuv_space_decimal - offset] = 8 #"green"
  225.                 look_up_table[yuv_space_decimal + offset] = 8 #"green"
  226.                 offset += 1
  227.         offset = 0
  228.         for i in range(len(white_list)):
  229.             color_triple = white_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
  230.             y = bin(color_triple[0])
  231.             y = y[:-2]
  232.             u = bin(color_triple[1])
  233.             u = u[:-2]
  234.             v = bin(color_triple[2])
  235.             v = v[:-2]
  236.             yuv_space = y + u[2:] + v[2:]
  237.             yuv_space_decimal = int(yuv_space,2)
  238.             offset = 0
  239.             while offset < int(self.Threshold_Val_Entry_StringVar.get()):
  240.                 look_up_table[yuv_space_decimal - offset] = 16 #"white"
  241.                 look_up_table[yuv_space_decimal + offset] = 16 #"white"
  242.                 offset += 1
  243.         offset = 0
  244.         #for i in range(len(blue_list)):
  245.         #    color_triple = blue_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
  246.         #    y = bin(color_triple[0])
  247.         #    y = y[:-2]
  248.         #    u = bin(color_triple[1])
  249.         #    u = u[:-2]
  250.         #    v = bin(color_triple[2])
  251.         #    v = v[:-2]
  252.         #    yuv_space = y + u[2:] + v[2:]
  253.         #    yuv_space_decimal = int(yuv_space,2)
  254.         #    offset = 0
  255.         #    while offset < int(self.Threshold_Val_Entry_StringVar.get()):
  256.                 #look_up_table[yuv_space_decimal - offset] = "blue"
  257.                 #look_up_table[yuv_space_decimal + offset] = "blue"
  258.         #        offset += 1
  259.         offset = 0
  260.         #for i in range(len(pink_list)):
  261.         #    color_triple = pink_list[i] # This is 3 decimal YUV values in the form (Y,U,V)
  262.         #    y = bin(color_triple[0])
  263.         #    y = y[:-2]
  264.         #    u = bin(color_triple[1])
  265.         #    u = u[:-2]
  266.         #    v = bin(color_triple[2])
  267.         #    v = v[:-2]
  268.         #    yuv_space = y + u[2:] + v[2:]
  269.         #    yuv_space_decimal = int(yuv_space,2)
  270.         #    offset = 0
  271.         #    while offset <= int(self.Threshold_Val_Entry_StringVar.get()):
  272.                 #look_up_table[yuv_space_decimal - offset] = "pink"
  273.                 #look_up_table[yuv_space_decimal + offset] = "pink"
  274.         #        offset += 1
  275.         offset = 0
  276.         montage_file = file("Look_Up_Table.raw", "wb")
  277.         final_array = array.array('B')
  278.         final_array.fromlist(look_up_table)
  279.         final_array.tofile(montage_file)
  280.         # Bit-write the whole thing
  281.  
  282.     def Button_4_Click(self, event): #click method for component ID=16
  283.         pass
  284.         # Button to open saved data
  285.         global color_dict
  286.         try:
  287.             save_file = open("Saved_Colortable", "a")
  288.             color_dict = pickle.load(save_file)
  289.             save_file.close()
  290.         except:
  291.              "File Not Found!"
  292.  
  293.     def Clear_Select_Button_Click(self, event): #click method for component ID=10
  294.         pass
  295.         color_dict["orange"] = []
  296.         color_dict["yellow"] = []
  297.         color_dict["cyan"] = []
  298.         color_dict["green"] = []
  299.         color_dict["white"] = []
  300.         color_dict["robot_blue"] = []
  301.         color_dict["robot_pink"] = []
  302.         our_photo = ImageTk.PhotoImage(original_image)
  303.         self.Canvas_1.create_image((325,240),image=our_photo)
  304.         # DOESN'T CLEAR CANVAS, SO IMAGE MUST BE RELOADED.
  305.  
  306.     def Load_Montage_Button_Click(self, event): #click method for component ID=1
  307.         # The montage name is a carryover from when I was still thinking I'd open montage files instead of
  308.         # regular bitmaps
  309.         global our_image
  310.         global original_image
  311.         pass
  312.         filename = tkFileDialog.askopenfilename(filetypes = [('Bitmap Images', '.bmp')])
  313.         our_image = IM.open(filename)
  314.         original_image = our_image
  315.         self.new_image = our_image
  316.         self.our_photo = ImageTk.PhotoImage(self.new_image)
  317.         self.Canvas_1.create_image((325,240),image=self.our_photo)
  318.  
  319.  
  320.     def Canvas_1_Click(self, event): #click method for component ID=9
  321.         x = int(event.x)
  322.         y = int(event.y)
  323.         self.AddYuyvToDict(x, y, str(self.RadioGroup1_StringVar.get()))
  324.  
  325.     # Tkinter boilerplate
  326.        
  327.     def Threshold_Val_Entry_StringVar_Callback(self, varName, index, mode):
  328.         pass
  329.  
  330.  
  331.     def RadioGroup1_StringVar_Callback(self, varName, index, mode):
  332.         pass
  333.  
  334.  
  335.     def AddYuyvToDict(self,x_val, y_val, color_label):
  336.         global color_dict
  337.         our_image.load()
  338.         RGB_pixel_triplet = our_image.getpixel((x_val,y_val))
  339.         putpixel = our_image.putpixel
  340. #        putpixel((x_val,y_val),(255,140,0))
  341.         place_x = 0
  342.         place_y = 0
  343.         while place_x < 640:
  344.             while place_y < 480:
  345.                 if our_image.getpixel((place_x,place_y)) == RGB_pixel_triplet:
  346.                     if self.RadioGroup1_StringVar.get() == "orange":
  347.                         putpixel((place_x,place_y),(255,140,0))
  348.                     elif self.RadioGroup1_StringVar.get() == "yellow":
  349.                         putpixel((place_x,place_y),(255,255,0))
  350.                     elif self.RadioGroup1_StringVar.get() == "cyan":
  351.                         putpixel((place_x,place_y),(0,255,255))
  352.                     elif self.RadioGroup1_StringVar.get() == "green":
  353.                         putpixel((place_x,place_y),(34,139,34))
  354.                     elif self.RadioGroup1_StringVar.get() == "white":
  355.                         putpixel((place_x,place_y),(255,255,255))
  356.                     elif self.RadioGroup1_StringVar.get() == "robot_blue":
  357.                         putpixel((place_x,place_y),(70,130,180))
  358.                     elif self.RadioGroup1_StringVar.get() == "robot_pink":
  359.                         putpixel((place_x,place_y),(255,192,203))                    
  360.                 place_y += 1
  361.             place_y = 0
  362.             place_x += 1
  363.         self.new_image = our_image
  364.         self.our_photo = ImageTk.PhotoImage(self.new_image)
  365.         self.Canvas_1.create_image((325,240),image=self.our_photo)
  366.         # Conversion is based on lua_rgb_to_yuyv() function from luaImageProc.cpp
  367.         YUV_Y = RGB_pixel_triplet[1] # same as "uint8_t y = g;"
  368.         YUV_U = 128 + ((RGB_pixel_triplet[2] - RGB_pixel_triplet[1]) / 2)# same as "uint8_t u = 128 + (b-g)/2;"
  369.         YUV_V = 128 + ((RGB_pixel_triplet[0] - RGB_pixel_triplet[1]) / 2)# same as "uint8_t v = 128 + (r-g)/2;"
  370.        
  371.         # Alternative conversion:
  372.         #YUV_Y = RGB_pixel_triplet[0] *  .299000 + RGB_pixel_triplet[1] *  .587000 + RGB_pixel_triplet[2] *  .114000
  373.         #YUV_U = RGB_pixel_triplet[0] * -.168736 + RGB_pixel_triplet[1] * -.331264 + RGB_pixel_triplet[2] *  .500000 + 128
  374.         #YUV_V = RGB_pixel_triplet[0] *  .500000 + RGB_pixel_triplet[1] * -.418688 + RGB_pixel_triplet[2] * -.081312 + 128
  375.  
  376.         color_dict[color_label] += [(YUV_Y, YUV_U, YUV_V)]
  377.    
  378. def main():
  379.     root = Tk()
  380.     app = _Colortable_Generator(root)
  381.     root.mainloop()
  382.  
  383.  
  384.  
  385.  
  386. if __name__ == '__main__':
  387.     #AddYuyvToDict(1, 1, "orange", [[1,2,3],[1,2,3],[1,2,3]])
  388.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement