hist_analyzer.py
Go to the documentation of this file.
00001 #!/usr/bin/env python  
00002 # Calculating and displaying 2D Hue-Saturation histogram of a color image
00003 import roslib
00004 roslib.load_manifest('opencv2')
00005 import sys
00006 import cv
00007 import numpy as np
00008 from collections import deque
00009 
00010 cv.NamedWindow("avg_noise", cv.CV_WINDOW_AUTOSIZE)
00011 cv.NamedWindow("back_modified", cv.CV_WINDOW_AUTOSIZE)
00012 cv.NamedWindow("back_modified2", cv.CV_WINDOW_AUTOSIZE)
00013 
00014 class HistAnalyzer:
00015 
00016     def __init__(self, background_noise, mask):
00017         self.background_noise = background_noise
00018         self.h_bins = 30
00019         self.s_bins = 32
00020         self.h_ranges = [0, 180]
00021         self.s_ranges = [0, 255]
00022         self.ranges = [self.h_ranges, self.s_ranges]
00023         self.hist = None
00024         self.mask = mask
00025         self.avg_noise = None
00026 
00027     def calc_hist(self):
00028         self.hist = cv.CreateHist([self.h_bins, self.s_bins], cv.CV_HIST_ARRAY, self.ranges, 1)
00029         hsv = cv.CreateImage(cv.GetSize(self.background_noise[0]), 8, 3)
00030         h_plane = cv.CreateMat(self.background_noise[0].height, self.background_noise[0].width, cv.CV_8UC1)
00031         s_plane = cv.CreateMat(self.background_noise[0].height, self.background_noise[0].width, cv.CV_8UC1)
00032         for i in xrange(len(self.background_noise)):
00033             cv.CvtColor(self.background_noise[i], hsv, cv.CV_BGR2HSV)
00034             cv.Split(hsv, h_plane, s_plane, None, None)            
00035             planes = [h_plane, s_plane]#, s_plane, v_plane]
00036             cv.CalcHist([cv.GetImage(i) for i in planes], self.hist, True, self.mask)            
00037         #cv.NormalizeHist(self.hist, 1.0)
00038 
00039     def check_for_hist(self):
00040         if self.hist == None:
00041             print "YOU CAN'T CALCULATE NOISE WITH HIST MODEL OF TABLETOP"
00042             exit
00043 
00044     def calc_noise(self):
00045         self.check_for_hist()
00046         self.avg_noise = cv.CreateImage(cv.GetSize(self.background_noise[0]), 8, 1)
00047         cv.Zero(self.avg_noise)
00048 
00049         for i in xrange(len(self.background_noise)-1):
00050             back_proj_img1, hist1 = self.back_project_hs(self.background_noise[i])
00051             back_proj_img2, hist2 = self.back_project_hs(self.background_noise[i+1])
00052 
00053             scratch = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
00054             scratch2 = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
00055             
00056             # do something clever with ands ors and diffs 
00057             cv.Zero(scratch)
00058             cv.Zero(scratch2)
00059             cv.Sub(back_proj_img2, back_proj_img1, scratch2) #noise, but includes object if failed, 
00060 
00061             cv.Sub(scratch2, self.avg_noise, scratch)            
00062             cv.Or(self.avg_noise, scratch2, self.avg_noise)
00063             cv.WaitKey(100)
00064 
00065     def compare_imgs(self, img1, img2):
00066         back_proj_img, hist1 = self.back_project_hs(img1)
00067         back_proj_img2, hist2 = self.back_project_hs(img2)
00068 
00069         scratch = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
00070         scratch2 = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
00071         cv.Zero(scratch)
00072         cv.Zero(scratch2)
00073 
00074         #cv.Sub(back_proj_img, back_proj_img2, scratch2) #opposite noise, but excludes object 
00075         cv.Sub(back_proj_img2, back_proj_img, scratch2) #noise, but includes object if failed, 
00076         cv.Sub(scratch2, ha.avg_noise, scratch)
00077         
00078         return scratch
00079         
00080 
00081     def back_project_hs(self, img):
00082         self.check_for_hist()
00083         hsv = cv.CreateImage(cv.GetSize(img), 8, 3)
00084         scratch = cv.CreateImage(cv.GetSize(img), 8, 1)
00085         back_proj_img = cv.CreateImage(cv.GetSize(img), 8, 1)
00086         cv.CvtColor(img, hsv, cv.CV_BGR2HSV)
00087         h_plane_img = cv.CreateImage(cv.GetSize(img), 8, 1)
00088         s_plane_img = cv.CreateImage(cv.GetSize(img), 8, 1)
00089         cv.Split(hsv, h_plane_img, s_plane_img, None, None)            
00090         cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, self.hist)
00091         cv.MorphologyEx(back_proj_img, back_proj_img, None, None, cv.CV_MOP_OPEN, 1)
00092         cv.MorphologyEx(back_proj_img, back_proj_img, None, None, cv.CV_MOP_CLOSE, 2)    
00093         cv.Threshold(back_proj_img, back_proj_img, 250, 255, cv.CV_THRESH_BINARY)
00094 
00095         return back_proj_img, self.hist
00096 
00097 if __name__ == '__main__':
00098     folder = sys.argv[1]+'/background_noise/'
00099     background_noise = deque() #[]
00100 
00101     cv.NamedWindow("Source", cv.CV_WINDOW_AUTOSIZE)
00102     cv.NamedWindow("final", cv.CV_WINDOW_AUTOSIZE)
00103 
00104     for i in xrange(130):
00105         background_noise.append(cv.LoadImage(folder+'file'+str(i).zfill(3)+'.png'))
00106     mask = cv.LoadImage(sys.argv[2], 0)
00107 
00108     ha = HistAnalyzer(background_noise, mask)
00109     ha.calc_hist()
00110     ha.calc_noise()
00111 
00112     back_sum_ls = deque() #[]
00113 
00114     for i in xrange(130):
00115         img = cv.LoadImage(folder+'file'+str(i).zfill(3)+'.png')
00116         result = ha.compare_imgs(img, ha.background_noise[0])
00117         back_sum_ls.append(float(cv.Sum(result)[0]))
00118 
00119     avg = np.mean(back_sum_ls)
00120     std = np.std(back_sum_ls)
00121 
00122     print "avg and std are :", avg, std
00123 
00124     n = 0
00125     sum_val = 0
00126     #test_sum_ls = []
00127 
00128     for i in xrange(9):
00129         for j in xrange(100):
00130             #print sys.argv[1]+'/object'+str(i).zfill(3)+'_try'+str(j).zfill(3)
00131             try:
00132                 img = cv.LoadImageM(sys.argv[1]+'/object'+str(i).zfill(3)+'_try'+str(j).zfill(3)+'_after_pr2.png')
00133                 #img = cv.LoadImageM(sys.argv[1]+'/object'+str(i).zfill(3)+'_try'+str(j).zfill(3)+'_before_pr2.png')
00134                 result = ha.compare_imgs(img, ha.background_noise[-1])
00135                 n = n+1
00136                 sum_val = sum_val + float(cv.Sum(result)[0])
00137 
00138                 #print "here's the sum :", cv.Sum(result)[0]
00139                 cv.ShowImage("Source", img)
00140                 cv.ShowImage("final", result)
00141 
00142                 cv.WaitKey(-1)
00143 
00144                 loc_sum = float(cv.Sum(result)[0])
00145                 #if loc_sum > avg-5*std and loc_sum < avg+5*std:
00146                 if loc_sum < avg+5*std:
00147                     print "success ! \t:", loc_sum, "\t compared to \t", avg, 5*std
00148                     #ha.background_noise.popleft()
00149                     #ha.background_noise.append(img)
00150                 else:
00151                     print "epic fail ! \t:", loc_sum, "\t compared to \t", avg, 5*std
00152                 
00153             except:
00154                 print "no file like that, probably outside of index range"
00155         print "recalculating hist and noise..."
00156         #ha.calc_hist()
00157         #ha.calc_noise()
00158         print "done!"
00159 
00160 
00161     print "average error for objects present :", sum_val/n


pr2_playpen
Author(s): Marc Killpack / mkillpack3@gatech.edu, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 12:18:32