dimreduce_display.py
Go to the documentation of this file.
00001 # Copyright (c) 2008, Willow Garage, Inc.
00002 # All rights reserved.
00003 # 
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions are met:
00006 # 
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Willow Garage, Inc. nor the names of its
00013 #       contributors may be used to endorse or promote products derived from
00014 #       this software without specific prior written permission.
00015 # 
00016 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00020 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026 # POSSIBILITY OF SUCH DAMAGE.
00027 #
00028 ## @author Hai Nguyen/hai@gatech.edu
00029 #import opencv as cv
00030 #import opencv.highgui as hg
00031 import util as ut
00032 import dimreduce as dr
00033 import numpy as np
00034 import pickle as pk
00035 import math
00036 import exceptions
00037 
00038 def load_pickle(filename):
00039     p = open(filename, 'r')
00040     picklelicious = pk.load(p)
00041     p.close()
00042     return picklelicious
00043 
00044 def reconstruct_input(nparray):
00045     #split
00046     npbig   = nparray[0:675].A
00047     npsmall = nparray[675:].A
00048 
00049     #reshape
00050     npbig_rs = npbig.reshape((15,15,3))
00051     npsml_rs = npsmall.reshape((3,3,3))
00052 
00053     #convert to cv
00054     img     = ut.np2cv(npbig_rs.astype(np.uint8))
00055     context = ut.np2cv(npsml_rs.astype(np.uint8))
00056     return img, context
00057 
00058 def normalize_for_display(vec):
00059     return (255 * ((vec - np.min(vec)) / np.max(vec)))
00060 
00061 def scale_image(img, scale):
00062     new_img = cv.CreateImage((img.width*scale, img.height*scale), img.depth, img.nChannels)
00063     cv.Resize(img, new_img, cv.CV_INTER_NN)
00064     return new_img
00065 
00066 def tile_images(img_width, img_height, num_width, num_height, images, channels=3):
00067     w = img_width * num_width
00068     h = img_height * num_height
00069     image = cv.CreateImage((int(w), int(h)), 8, channels)
00070     cv.Set(image, cv.Scalar(255,255,255))
00071     while len(images) > 0:
00072         try:
00073             for y in range(int(num_height)):
00074                 for x in range(int(num_width)):
00075                     small_tile = images.pop()
00076                     img_x = x * img_width
00077                     img_y = y * img_height
00078                     cropped = cv.GetSubRect(image, cv.Rectangle(img_x, img_y, img_width,img_height))
00079                     cv.Copy(small_tile, cropped)
00080         except exceptions.IndexError, e:
00081             break
00082     return image
00083 
00084 def display(vec, name):
00085     patch, context = reconstruct_input(vec)
00086     patch = scale_image(patch, 5)
00087     context = scale_image(context, 5)
00088     hg.cvSaveImage(name + '_patch.png', patch)
00089     hg.cvSaveImage(name + '_context.png', context)
00090     hg.cvShowImage('image', patch)
00091     hg.cvShowImage('context', context)
00092     hg.cvWaitKey()
00093 
00094 def tile_nsave(vecs):
00095     patches = []
00096     for i in xrange(vecs.shape[1]):
00097         patch, context = reconstruct_input(vecs[:,i])
00098         patches.append(patch)
00099     tile_width = math.ceil(math.pow(vecs.shape[1], .5))
00100     large_image = tile_images(15, 15, tile_width, tile_width, patches, 3)
00101     return large_image
00102 
00103 show_pca                     = False
00104 save_pca_bases               = False 
00105 process_inputs               = False 
00106 separate_negatives_positives = True
00107 dataset = load_pickle('PatchClassifier.dataset.pickle')
00108 hg.cvNamedWindow('image', 1)
00109 hg.cvNamedWindow('context', 1)
00110 pca_basis = normalize_for_display(dataset.projection_basis)
00111 
00112 if show_pca:
00113     for i in range(pca_basis.shape[1]):
00114         print 'basis', i
00115         display(pca_basis[:,i], 'pca_basis'+str(i))
00116 
00117 if save_pca_bases:
00118     large_image = tile_nsave(pca_basis)
00119     large_image = scale_image(large_image, 8)
00120     hg.cvSaveImage('pca_large_image.png', large_image)
00121 
00122 if process_inputs:
00123     large_image = tile_nsave(dataset.inputs)
00124     hg.cvSaveImage('inputs.png', large_image)
00125 
00126 if separate_negatives_positives:
00127     r, c = np.where(dataset.outputs == 0)
00128     negatives = tile_nsave(dataset.inputs[:,c.A[0]])
00129     r, c = np.where(dataset.outputs == 1)
00130     positives = tile_nsave(dataset.inputs[:,c.A[0]])
00131     hg.cvSaveImage('negatives.png', negatives)
00132     hg.cvSaveImage('positives.png', positives)
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 
00211 
00212 
00213 
00214 
00215 #projection_vectors = dr.pca_vectors(dataset.inputs, 0.95)
00216 #projection_vectors = normalize_for_display(projection_vectors)
00217 #print 'number of projection bases', projection_vectors.shape
00218 
00219 #image = hg.cvLoadImage('1frame489.png')
00220 #subrect = cv.cvCloneImage(cv.cvGetSubRect(image, cv.cvRect(40, 40, 20, 30)))
00221 #hg.cvShowImage('image', image)
00222 #hg.cvWaitKey()
00223 #
00224 #hg.cvShowImage('image', subrect)
00225 #print 'original'
00226 #hg.cvWaitKey()
00227 #
00228 #npsub   = ut.cv2np(subrect, 'BGR')
00229 #img     = ut.np2cv(npsub.reshape((30,20,3)))
00230 #hg.cvShowImage('image', img)
00231 #print 'reconstructed'
00232 #hg.cvWaitKey()
00233 
00234 #for i in range(dataset.num_examples()):
00235 #    patch, context = reconstruct_input(dataset.inputs[:,i])
00236 #    hg.cvShowImage('image', patch)
00237 #    hg.cvShowImage('context', context)
00238 #    hg.cvWaitKey(33)
00239 
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 #PKG = 'laser_interface'
00271 #import sys, os, subprocess
00272 #try:
00273 #    rostoolsDir = (subprocess.Popen(['rospack', 'find', 'rostools'], stdout=subprocess.PIPE).communicate()[0] or '').strip()
00274 #    sys.path.append(os.path.join(rostoolsDir,'src'))
00275 #    import rostools.launcher
00276 #    rostools.launcher.updateSysPath(sys.argv[0], PKG, bootstrapVersion="0.6")
00277 #except ImportError:
00278 #    print >> sys.stderr, "\nERROR: Cannot locate rostools"
00279 #    sys.exit(1)  
00280 #
00281 ##from pkg import *
00282 #import rospy
00283 #from std_msgs.msg import Position
00284 #from std_msgs.msg import RobotBase2DOdom
00285 #import sys
00286 #
00287 #def debug_me(p):
00288 #    print 'received', p.pos.x, p.pos.y, p.pos.th
00289 #rospy.TopicSub('odom', RobotBase2DOdom, debug_me)
00290 #rospy.ready('test')
00291 #rospy.spin()
00292 #def swap_br(npimage):
00293 #    b = npimage[:,:,0].copy()
00294 #    r = npimage[:,:,2].copy()
00295 #    npimage[:,:,0] = r
00296 #    npimage[:,:,2] = b
00297 #    return npimage
00298 #
00299 #image   = hg.cvLoadImage('1frame489.png')
00300 #npimg   = ut.cv2np(image)
00301 #swapped = swap_br(npimg.copy())
00302 #cvimg   = ut.np2cv(swapped)
00303 #
00304 #hg.cvNamedWindow('hai', 1)
00305 #hg.cvShowImage('hai', cvimg)
00306 #hg.cvWaitKey(10)
00307 #
00308 #
00309 
00310 
00311 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00328 
00329 


laser_interface
Author(s): Hai Nguyen and Travis Deyle. Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:45:51