###前提・実現したいこと
こんにちは。
https://github.com/akisato-/pySaliencyMap
上記のサイトで公開されている、
入力した画像をSaliencyMap化するソースコード(main.pyとpySaliencyMap.py)に関して質問させていただきます。
SaliencyMapを作成するにあたって、その過程における画像を保存して仕組みを理解したいと考えております。
以下のソースコードにおける、ICM、CCM、OCMの三つをcv2.imshowしましたが、真っ黒な画像が表示されてしまいました。
それぞれ、色顕著、輝度顕著、方向顕著を示しているので、
下記のような画像が得られると予想したのですが、
解決方法はございますでしょうか。
https://www.dropbox.com/s/02ndfntnjtjlkm9/saliency.png?dl=0
###該当のソースコード
python
1 2#文字数制限を超えてしまうため、pySaliencyMap.pyの前半部分を省略して掲載しております。 3#また、注目していただきたいところを示してあります. 4 5# Name: pySaliencyMap.py 6import cv2 7import numpy as np 8import pySaliencyMapDefs 9#省略あり 10 # feature maps 11 ## constructing a Gaussian pyramid 12 def FMCreateGaussianPyr(self, src): 13 dst = list() 14 dst.append(src) 15 for i in range(1,9): 16 nowdst = cv2.pyrDown(dst[i-1]) 17 dst.append(nowdst) 18 return dst 19 ## taking center-surround differences 20 def FMCenterSurroundDiff(self, GaussianMaps): 21 dst = list() 22 for s in range(2,5): 23 now_size = GaussianMaps[s].shape 24 now_size = (now_size[1], now_size[0]) ## (width, height) 25 tmp = cv2.resize(GaussianMaps[s+3], now_size, interpolation=cv2.INTER_LINEAR) 26 nowdst = cv2.absdiff(GaussianMaps[s], tmp) 27 dst.append(nowdst) 28 tmp = cv2.resize(GaussianMaps[s+4], now_size, interpolation=cv2.INTER_LINEAR) 29 nowdst = cv2.absdiff(GaussianMaps[s], tmp) 30 dst.append(nowdst) 31 return dst 32 ## constructing a Gaussian pyramid + taking center-surround differences 33 def FMGaussianPyrCSD(self, src): 34 GaussianMaps = self.FMCreateGaussianPyr(src) 35 dst = self.FMCenterSurroundDiff(GaussianMaps) 36 return dst 37 ## intensity feature maps 38 def IFMGetFM(self, I): 39 return self.FMGaussianPyrCSD(I) 40 ## color feature maps 41 def CFMGetFM(self, R, G, B): 42 # max(R,G,B) 43 tmp1 = cv2.max(R, G) 44 RGBMax = cv2.max(B, tmp1) 45 RGBMax[RGBMax <= 0] = 0.0001 # prevent dividing by 0 46 # min(R,G) 47 RGMin = cv2.min(R, G) 48 # RG = (R-G)/max(R,G,B) 49 RG = (R - G) / RGBMax 50 # BY = (B-min(R,G)/max(R,G,B) 51 BY = (B - RGMin) / RGBMax 52 # clamp nagative values to 0 53 RG[RG < 0] = 0 54 BY[BY < 0] = 0 55 # obtain feature maps in the same way as intensity 56 RGFM = self.FMGaussianPyrCSD(RG) 57 BYFM = self.FMGaussianPyrCSD(BY) 58 # return 59 return RGFM, BYFM 60 ## orientation feature maps 61 def OFMGetFM(self, src): 62 # creating a Gaussian pyramid 63 GaussianI = self.FMCreateGaussianPyr(src) 64 # convoluting a Gabor filter with an intensity image to extract oriemtation features 65 GaborOutput0 = [ np.empty((1,1)), np.empty((1,1)) ] # dummy data: any kinds of np.array()s are OK 66 GaborOutput45 = [ np.empty((1,1)), np.empty((1,1)) ] 67 GaborOutput90 = [ np.empty((1,1)), np.empty((1,1)) ] 68 GaborOutput135 = [ np.empty((1,1)), np.empty((1,1)) ] 69 for j in range(2,9): 70 GaborOutput0.append( cv2.filter2D(GaussianI[j], cv2.CV_32F, self.GaborKernel0) ) 71 GaborOutput45.append( cv2.filter2D(GaussianI[j], cv2.CV_32F, self.GaborKernel45) ) 72 GaborOutput90.append( cv2.filter2D(GaussianI[j], cv2.CV_32F, self.GaborKernel90) ) 73 GaborOutput135.append( cv2.filter2D(GaussianI[j], cv2.CV_32F, self.GaborKernel135) ) 74 # calculating center-surround differences for every oriantation 75 CSD0 = self.FMCenterSurroundDiff(GaborOutput0) 76 CSD45 = self.FMCenterSurroundDiff(GaborOutput45) 77 CSD90 = self.FMCenterSurroundDiff(GaborOutput90) 78 CSD135 = self.FMCenterSurroundDiff(GaborOutput135) 79 # concatenate 80 dst = list(CSD0) 81 dst.extend(CSD45) 82 dst.extend(CSD90) 83 dst.extend(CSD135) 84 # return 85 return dst 86 ## motion feature maps 87 def MFMGetFM(self, src): 88 # convert scale 89 I8U = np.uint8(255 * src) 90 cv2.waitKey(10) 91 # calculating optical flows 92 if self.prev_frame is not None: 93 farne_pyr_scale= pySaliencyMapDefs.farne_pyr_scale 94 farne_levels = pySaliencyMapDefs.farne_levels 95 farne_winsize = pySaliencyMapDefs.farne_winsize 96 farne_iterations = pySaliencyMapDefs.farne_iterations 97 farne_poly_n = pySaliencyMapDefs.farne_poly_n 98 farne_poly_sigma = pySaliencyMapDefs.farne_poly_sigma 99 farne_flags = pySaliencyMapDefs.farne_flags 100 flow = cv2.calcOpticalFlowFarneback(\ 101 prev = self.prev_frame, \ 102 next = I8U, \ 103 pyr_scale = farne_pyr_scale, \ 104 levels = farne_levels, \ 105 winsize = farne_winsize, \ 106 iterations = farne_iterations, \ 107 poly_n = farne_poly_n, \ 108 poly_sigma = farne_poly_sigma, \ 109 flags = farne_flags, \ 110 flow = None \ 111 ) 112 flowx = flow[...,0] 113 flowy = flow[...,1] 114 else: 115 flowx = np.zeros(I8U.shape) 116 flowy = np.zeros(I8U.shape) 117 # create Gaussian pyramids 118 dst_x = self.FMGaussianPyrCSD(flowx) 119 dst_y = self.FMGaussianPyrCSD(flowy) 120 # update the current frame 121 self.prev_frame = np.uint8(I8U) 122 # return 123 return dst_x, dst_y 124 125 # conspicuity maps 126 ## standard range normalization 127 def SMRangeNormalize(self, src): 128 minn, maxx, dummy1, dummy2 = cv2.minMaxLoc(src) 129 if maxx!=minn: 130 dst = src/(maxx-minn) + minn/(minn-maxx) 131 else: 132 dst = src - minn 133 return dst 134 ## computing an average of local maxima 135 def SMAvgLocalMax(self, src): 136 # size 137 stepsize = pySaliencyMapDefs.default_step_local 138 width = src.shape[1] 139 height = src.shape[0] 140 # find local maxima 141 numlocal = 0 142 lmaxmean = 0 143 for y in range(0, height-stepsize, stepsize): 144 for x in range(0, width-stepsize, stepsize): 145 localimg = src[y:y+stepsize, x:x+stepsize] 146 lmin, lmax, dummy1, dummy2 = cv2.minMaxLoc(localimg) 147 lmaxmean += lmax 148 numlocal += 1 149 # averaging over all the local regions 150 return lmaxmean / numlocal 151 ## normalization specific for the saliency map model 152 def SMNormalization(self, src): 153 dst = self.SMRangeNormalize(src) 154 lmaxmean = self.SMAvgLocalMax(dst) 155 normcoeff = (1-lmaxmean)*(1-lmaxmean) 156 return dst * normcoeff 157 ## normalizing feature maps 158 def normalizeFeatureMaps(self, FM): 159 NFM = list() 160 for i in range(0,6): 161 normalizedImage = self.SMNormalization(FM[i]) 162 nownfm = cv2.resize(normalizedImage, (self.width, self.height), interpolation=cv2.INTER_LINEAR) 163 NFM.append(nownfm) 164 return NFM 165 ## intensity conspicuity map 166 def ICMGetCM(self, IFM): 167 NIFM = self.normalizeFeatureMaps(IFM) 168 ICM = sum(NIFM) 169 return ICM 170 ## color conspicuity map 171 def CCMGetCM(self, CFM_RG, CFM_BY): 172 # extracting a conspicuity map for every color opponent pair 173 CCM_RG = self.ICMGetCM(CFM_RG) 174 CCM_BY = self.ICMGetCM(CFM_BY) 175 # merge 176 CCM = CCM_RG + CCM_BY 177 # return 178 return CCM 179 ## orientation conspicuity map 180 def OCMGetCM(self, OFM): 181 OCM = np.zeros((self.height, self.width)) 182 for i in range (0,4): 183 # slicing 184 nowofm = OFM[i*6:(i+1)*6] # angle = i*45 185 # extracting a conspicuity map for every angle 186 NOFM = self.ICMGetCM(nowofm) 187 # normalize 188 NOFM2 = self.SMNormalization(NOFM) 189 # accumulate 190 OCM += NOFM2 191 return OCM 192 ## motion conspicuity map 193 def MCMGetCM(self, MFM_X, MFM_Y): 194 return self.CCMGetCM(MFM_X, MFM_Y) 195 196 # core 197 def SMGetSM(self, src): 198 # definitions 199 size = src.shape 200 width = size[1] 201 height = size[0] 202 # check 203# if(width != self.width or height != self.height): 204# sys.exit("size mismatch") 205 # extracting individual color channels 206 R, G, B, I = self.SMExtractRGBI(src) 207 # extracting feature maps 208 IFM = self.IFMGetFM(I) 209 CFM_RG, CFM_BY = self.CFMGetFM(R, G, B) 210 OFM = self.OFMGetFM(I) 211 MFM_X, MFM_Y = self.MFMGetFM(I) 212################################################################### 213 # extracting conspicuity maps 214 ICM = self.ICMGetCM(IFM) 215 cv2.imshow('ICM.jpg',ICM) 216 CCM = self.CCMGetCM(CFM_RG, CFM_BY) 217 cv2.imshow('CCM.jpg',CCM) 218 OCM = self.OCMGetCM(OFM) 219 cv2.imshow('OCM.jpg',OCM) 220 MCM = self.MCMGetCM(MFM_X, MFM_Y) 221################################################################### 222 # adding all the conspicuity maps to form a saliency map 223 wi = pySaliencyMapDefs.weight_intensity 224 wc = pySaliencyMapDefs.weight_color 225 wo = pySaliencyMapDefs.weight_orientation 226 wm = pySaliencyMapDefs.weight_motion 227 SMMat = wi*ICM + wc*CCM + wo*OCM + wm*MCM 228 # normalize 229 normalizedSM = self.SMRangeNormalize(SMMat) 230 normalizedSM2 = normalizedSM.astype(np.float32) 231 smoothedSM = cv2.bilateralFilter(normalizedSM2, 7, 3, 1.55) 232 self.SM = cv2.resize(smoothedSM, (width,height), interpolation=cv2.INTER_NEAREST) 233 # return 234 return self.SM
###試したこと
cv2.imshowもcv2.imwriteと同じく、8bitのみしか使用できないのかとも考え
ICM = ICM.astype(np.uint8)
を使って変換しましたが、結果は変わりませんでした。
変換前は、float32でした。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/11/24 15:35
退会済みユーザー
2017/11/24 16:07 編集
退会済みユーザー
2017/11/24 16:14
退会済みユーザー
2017/11/24 16:22
退会済みユーザー
2017/11/24 16:23 編集
退会済みユーザー
2017/11/24 16:40
退会済みユーザー
2017/11/24 16:46
退会済みユーザー
2017/11/26 02:06
退会済みユーザー
2017/11/26 02:08
退会済みユーザー
2017/11/27 16:24