From 4ae5f89ac6d4be75019742e60347280f739ad537 Mon Sep 17 00:00:00 2001 From: Bryan Steinbach Date: Mon, 18 Oct 2010 09:51:25 -0700 Subject: [PATCH] added inverse yuv --- week6/week6.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/week6/week6.py b/week6/week6.py index 1e25427..87c16fd 100755 --- a/week6/week6.py +++ b/week6/week6.py @@ -35,6 +35,10 @@ def fetchlink(url): ''' Fetch file from link and return path to local file ''' return urllib.urlretrieve(url)[0] +yuvmat = np.array([[0.299,0.587,0.114],[-0.14713,-0.28886,0.436],[0.615,-0.51499,-0.10001]]) # source: wikipedia YUV article +yuvmatinv = np.linalg.inv(yuvmat) +print yuvmatinv + class ImgSearch(HasTraits): # Model query = Str("Enter search") @@ -43,8 +47,9 @@ class ImgSearch(HasTraits): refresh = Button() rotate90 = Button() - yuv = Button() rotate_color = Button() + rgb_to_yuv = Button() + yuv_to_rgb = Button() query = "red star winter orbit" @@ -61,17 +66,18 @@ class ImgSearch(HasTraits): self.ima = pylab.rot90(self.ima) self.draw_image() - def _yuv_fired(self): + def _rgb_to_yuv_fired(self): ''' Show YUV as RGB ''' - mat = np.array([[0.299,0.587,0.114],[-0.14713,-0.28886,0.436],[0.615,-0.51499,-0.10001]]) # source: wikipedia YUV article - x = np.zeros_like(self.ima) - for i in range(self.ima.shape[0]): - for j in range(self.ima.shape[1]): - x[i,j,:] = np.dot(mat,self.ima[i,j,:]) - self.ima = x + self.color_transform(yuvmat) + self.draw_image() + + def _yuv_to_rgb_fired(self): + ''' Show RGB as YUV ''' + self.color_transform(yuvmatinv) self.draw_image() def _rotate_color_fired(self): + ''' Advance color space RGB -> BRG ''' x = np.zeros_like(self.ima) r,g,b = self.ima[:,:,0],self.ima[:,:,1],self.ima[:,:,2] x[:,:,0] = b @@ -97,7 +103,7 @@ class ImgSearch(HasTraits): def load_image(self): ''' Load image from disk ''' im = Image.open(self.localfile) - self.ima = np.asarray(im) + self.ima = np.asarray(im)/256.0 print "ima.shape: ",self.ima.shape @@ -109,6 +115,14 @@ class ImgSearch(HasTraits): self.localfile = fetchlink(self.url) print "localfile: ",self.localfile + def color_transform(self,mat): + ''' Linear colorspace transform ''' + x = np.zeros_like(self.ima) + for i in range(self.ima.shape[0]): + for j in range(self.ima.shape[1]): + x[i,j,:] = np.dot(mat,self.ima[i,j,:]) + self.ima = x + # View qi = Item('query',label='Input',width=250) @@ -118,10 +132,11 @@ class ImgSearch(HasTraits): refi = Item('refresh',show_label=False) rot90i = Item('rotate90',show_label=False) - yuvi = Item('yuv',show_label=False) + rgbyuvi = Item('rgb_to_yuv',show_label=False) + yuvrgbi = Item('yuv_to_rgb',show_label=False) rci = Item('rotate_color',show_label=False) - view = View(HSplit(qi,rqi),urli,figi,HSplit(refi,rot90i,yuvi,rci), + view = View(HSplit(qi,rqi),urli,figi,HSplit(refi,rot90i,rci,rgbyuvi,yuvrgbi), width=400,height=300,resizable=True) def __init__(self): -- 2.11.4.GIT