00001
00002
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
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
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
00031 planes = [h_plane_patch, s_plane_patch]
00032
00033 h_bins = 30
00034 s_bins = 32
00035 v_bins = 30
00036 hist_size = [h_bins, s_bins]
00037
00038 h_ranges = [0, 180]
00039
00040
00041 s_ranges = [0, 255]
00042 v_ranges = [0, 255]
00043 ranges = [h_ranges, s_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
00055
00056
00057
00058
00059
00060
00061
00062
00063 return back_proj_img, hist
00064
00065 def back_project_hs(src, patch):
00066
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
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
00085 planes = [h_plane_patch, s_plane_patch]
00086
00087
00088 h_bins = 30
00089 s_bins = 32
00090 hist_size = [h_bins, s_bins]
00091
00092 h_ranges = [0, 180]
00093 s_ranges = [0, 255]
00094
00095
00096 ranges = [h_ranges, s_ranges]
00097
00098 scale = 1
00099 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
00100
00101 cv.CalcHist([cv.GetImage(i) for i in planes], hist)
00102
00103 (min_value, max_value, _, _) = cv.GetMinMaxHistValue(hist)
00104
00105
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
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
00118
00119
00120
00121
00122
00123
00124
00125
00126
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
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
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
00161 return back_modified, hist
00162
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
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
00189 cv.Zero(scratch)
00190 cv.Zero(scratch2)
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 cv.Sub(back_proj_img2, back_proj_img, scratch2)
00201
00202
00203 cv.Sub(scratch2, avg_noise, scratch)
00204 cv.Or(avg_noise, scratch2, avg_noise)
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 cv.ShowImage("final", scratch)
00215
00216
00217
00218
00219
00220
00221
00222
00223 print cv.CompareHist(hist1, hist2, cv.CV_COMP_BHATTACHARYYA)
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 cv.Scale(back_proj_img, back_proj_img, 1/255.0)
00238 print "here's the sum :", cv.Sum(scratch2)
00239