back_project_success_detect.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 
00008 cv.NamedWindow("back_projection", cv.CV_WINDOW_AUTOSIZE)
00009 cv.NamedWindow("back_modified", cv.CV_WINDOW_AUTOSIZE)
00010 cv.NamedWindow("back_modified2", cv.CV_WINDOW_AUTOSIZE)
00011 
00012 def hs_histogram(src, patch):
00013     # Convert to HSV
00014     hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
00015     cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
00016     hsv_patch= cv.CreateImage(cv.GetSize(patch), 8, 3)
00017 
00018     # Extract the H and S planes
00019     h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
00020     h_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1)
00021     h_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1)
00022     s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
00023     s_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1)
00024     s_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1)
00025     v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
00026 
00027     cv.Split(hsv, h_plane, s_plane, v_plane, None)
00028     cv.Split(hsv, h_plane_img, s_plane_img, None, None)
00029     cv.Split(hsv_patch, h_plane_patch, s_plane_patch, None, None)
00030     #cv.Split(src, h_plane, s_plane, v_plane, None)
00031     planes = [h_plane_patch, s_plane_patch]#, s_plane, v_plane]
00032 
00033     h_bins = 30
00034     s_bins = 32
00035     v_bins = 30
00036     hist_size = [h_bins, s_bins]
00037     # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
00038     h_ranges = [0, 180]
00039     # saturation varies from 0 (black-gray-white) to
00040     # 255 (pure spectrum color)
00041     s_ranges = [0, 255]
00042     v_ranges = [0, 255]
00043     ranges = [h_ranges, s_ranges]#, s_ranges, v_ranges]
00044     scale = 10
00045     hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
00046     cv.CalcHist([cv.GetImage(i) for i in planes], hist)
00047     (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)
00048 
00049     hist_img = cv.CreateImage((h_bins*scale, s_bins*scale), 8, 3)
00050 
00051     back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1)
00052     cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist)
00053     
00054     # for h in range(h_bins):
00055     #     for s in range(s_bins):
00056     #         bin_val = cv.QueryHistValue_2D(hist, h, s)
00057     #         intensity = cv.Round(bin_val * 255 / max_value)
00058     #         cv.Rectangle(hist_img,
00059     #                      (h*scale, s*scale),
00060     #                      ((h+1)*scale - 1, (s+1)*scale - 1),
00061     #                      cv.RGB(intensity, intensity, intensity), 
00062     #                      cv.CV_FILLED)
00063     return back_proj_img, hist
00064 
00065 def back_project_hs(src, patch):
00066     # Convert to HSV
00067     hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
00068     cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
00069     hsv_patch= cv.CreateImage(cv.GetSize(patch), 8, 3)
00070     cv.CvtColor(patch, hsv_patch, cv.CV_BGR2HSV)
00071 
00072     # Extract the H and S planes
00073     h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
00074     h_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1)
00075     h_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1)
00076     s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
00077     s_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1)
00078     s_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1)
00079     v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
00080 
00081     cv.Split(hsv, h_plane, s_plane, v_plane, None)
00082     cv.Split(hsv, h_plane_img, s_plane_img, None, None)
00083     cv.Split(hsv_patch, h_plane_patch, s_plane_patch, None, None)
00084     #cv.Split(src, h_plane, s_plane, v_plane, None)
00085     planes = [h_plane_patch, s_plane_patch]#, s_plane, v_plane]
00086     # planes = [s_plane_patch]#, s_plane, v_plane]
00087 
00088     h_bins = 30
00089     s_bins = 32
00090     hist_size = [h_bins, s_bins]
00091     # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
00092     h_ranges = [0, 180]
00093     s_ranges = [0, 255]
00094     # saturation varies from 0 (black-gray-white) to
00095     # 255 (pure spectrum color)
00096     ranges = [h_ranges, s_ranges]#, s_ranges, v_ranges]
00097     #ranges = [s_ranges]#, s_ranges, v_ranges]
00098     scale = 1
00099     hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
00100     #hist = cv.CreateHist([s_bins], cv.CV_HIST_ARRAY, ranges, 1)
00101     cv.CalcHist([cv.GetImage(i) for i in planes], hist)
00102     
00103     (min_value, max_value, _, _) = cv.GetMinMaxHistValue(hist)
00104 
00105     #cv.NormalizeHist(hist, 20*250.0)
00106     print "min hist value is :", min_value
00107     print "max hist value is :", max_value
00108 
00109     back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1)
00110 
00111     #cv.NormalizeHist(hist, 2000)
00112 
00113     cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist)
00114     back_modified = cv.CreateImage(cv.GetSize(src), 8, 1)
00115     back_modified2 = cv.CreateImage(cv.GetSize(src), 8, 1)
00116 
00117     # cv.Dilate(back_proj_img, back_proj_img)
00118     # cv.Erode(back_proj_img, back_proj_img)
00119     #cv.Smooth(back_proj_img, back_modified)
00120     
00121     #cv.AdaptiveThreshold(back_proj_img, back_modified, 255, adaptive_method=cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C)
00122     #cv.Threshold(back_proj_img, back_modified, 250, 255, cv.CV_THRESH_BINARY)
00123     #cv.MorphologyEx(back_modified,back_modified2, None, None, cv.CV_MOP_CLOSE, 3)
00124     #cv.MorphologyEx(back_modified,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)    
00125     # cv.MorphologyEx(back_proj_img,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)
00126     #cv.MorphologyEx(back_modified2,back_modified2, None, None, cv.CV_MOP_OPEN, 2)    
00127 
00128 
00129 
00130 
00131     cv.MorphologyEx(back_proj_img,back_modified, None, None, cv.CV_MOP_OPEN, 1)
00132     cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_CLOSE, 2)    
00133     cv.Threshold(back_modified, back_modified, 250, 255, cv.CV_THRESH_BINARY)
00134 
00135     # cv.MorphologyEx(back_proj_img,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)
00136     # cv.MorphologyEx(back_modified2,back_modified2, None, None, cv.CV_MOP_OPEN, 2)    
00137 
00138 
00139 
00140 
00141     #cv.FloodFill(back_modified, (320, 240), cv.Scalar(255), cv.Scalar(30), cv.Scalar(30), flags=8)
00142 
00143     # for i in xrange (10):
00144     #     cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_OPEN, 3)
00145     #     cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_CLOSE, 1)    
00146 
00147     
00148     #cv.SubRS(back_modified, 255, back_modified)
00149 
00150     # cv.CalcBackProject([s_plane_img], back_proj_img, hist)
00151     # cv.Scale(back_proj_img, back_proj_img, 30000)
00152     cv.ShowImage("back_projection", back_proj_img)
00153     cv.ShowImage("back_modified", back_modified)
00154     cv.ShowImage("back_modified2", back_modified2)
00155 
00156     cv.WaitKey(0)
00157 
00158 
00159     
00160     #return back_proj_img, hist
00161     return back_modified, hist
00162     #return , hist
00163 
00164 if __name__ == '__main__':
00165     folder = sys.argv[1]
00166     
00167     cv.NamedWindow("Source", cv.CV_WINDOW_AUTOSIZE)
00168     cv.NamedWindow("final", cv.CV_WINDOW_AUTOSIZE)
00169     src2 = cv.LoadImageM(folder+'object'+str(0).zfill(3)+'_try'+str(0).zfill(3)+'_after_pr2.png')
00170     patch_images = []
00171 
00172     avg_noise = cv.CreateImage(cv.GetSize(src2), 8, 1)
00173     cv.Zero(avg_noise)
00174     for k in xrange(1):
00175         patch_images.append(cv.LoadImageM('/home/mkillpack/Desktop/patch2.png'))
00176     #for i in [4]:
00177     for i in xrange(9):
00178         for j in xrange(100):
00179             print folder+'object'+str(i).zfill(3)+'_try'+str(j).zfill(3)+'_after_pr2.png'
00180             src = cv.LoadImageM(folder+'object'+str(i).zfill(3)+'_try'+str(j).zfill(3)+'_after_pr2.png')
00181             cv.ShowImage("Source", src)
00182 
00183             back_proj_img, hist1 = back_project_hs(src, patch_images[0])
00184             back_proj_img2, hist2 = back_project_hs(src2, patch_images[0])
00185             scratch = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
00186             scratch2 = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
00187             
00188             # do something clever with ands ors and diffs 
00189             cv.Zero(scratch)
00190             cv.Zero(scratch2)
00191 
00192 
00193             #idea is to have a background model from back_proj_img2, or at least an emtpy single shot
00194             ###cv.Sub(back_proj_img, back_proj_img2, scratch)
00195 
00196 
00197             #cv.SubRS(back_proj_img, 255, scratch)
00198             ###cv.SubRS(back_proj_img2, 255, scratch2)
00199             #cv.Sub(back_proj_img, back_proj_img2, scratch2) #opposite noise, but excludes object 
00200             cv.Sub(back_proj_img2, back_proj_img, scratch2) #noise, but includes object if failed, 
00201                                                             #would need to learn before then update selectively 
00202                                                             #Maybe want both added in the end. 
00203             cv.Sub(scratch2, avg_noise, scratch)            
00204             cv.Or(avg_noise, scratch2, avg_noise)
00205 
00206             ##adding this part fills in wherever the object has been too, heatmaps?
00207             #cv.Sub(back_proj_img2, back_proj_img, scratch)
00208             #cv.Or(avg_noise, scratch, avg_noise)
00209             #
00210 
00211 
00212             #cv.Sub(back_proj_img2, avg_noise, back_proj_img2)
00213             #cv.Sub(scratch,, back_proj_img2)
00214             cv.ShowImage("final", scratch)
00215             #cv.Sub(scratch, avg_noise, scratch2)
00216 
00217 
00218 
00219             #cv.And(scratch, back_proj_img2, scratch2)
00220             #cv.SubRS(scratch2, 255, scratch)                
00221             #cv.ShowImage("final", back_proj_img)
00222 
00223             print cv.CompareHist(hist1, hist2, cv.CV_COMP_BHATTACHARYYA)
00224 
00225             #making a mask
00226             #mask = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)
00227 
00228             #cv.SubRS(back_proj_img2, 255, back_proj_img2)
00229             #cv.SubRS(back_proj_img, 255, back_proj_img, mask=back_proj_img2)
00230             #cv.SubRS(back_proj_img, 255, back_proj_img)
00231 
00232             #cv.MorphologyEx(back_proj_img,back_proj_img, None, None, cv.CV_MOP_OPEN, 8)
00233             #cv.MorphologyEx(back_proj_img,back_proj_img, None, None, cv.CV_MOP_CLOSE, 8)
00234             #cv.ShowImage("back_projection", back_proj_img2)
00235             #cv.WaitKey(0)
00236 
00237             cv.Scale(back_proj_img, back_proj_img, 1/255.0)
00238             print "here's the sum :", cv.Sum(scratch2)
00239 


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