Advertisement
Guest User

Colortable Generator Version A

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