前提・実現したいこと
ハフ変換を利用して円を認識する処理を書いているのですが、使用する画像が黒背景に白前景であるため、ハフ変換後がわかりやすいように、白背景に黒前景にしたいため、
cv2.bitwise_not
を用いて画像の反転をしたいのですが、
この画像を反転すると
このようにすべて真っ黒になってしまいます。
これは何が原因でしょうか?
よろしくお願いいたします。
該当のソースコード
python
1for i in range (1550,1560): 2 3 fimg = cv2.imread('DSC0'+str(i)+'.jpg',0) 4 fheight = fimg.shape[0] 5 fwidth = fimg.shape[1] 6 fresize_img = cv2.resize(fimg,(int(fwidth*0.5),int(fheight*0.5))) 7 8 #ganma 9 gfimg_blur = cv2.GaussianBlur(fresize_img,(499,499),0) 10 gfheight = gfimg_blur.shape[0] 11 gfwidth = gfimg_blur.shape[1] 12 np.clip(gfheight, 1,254) 13 np.clip(gfwidth,1,254) 14 gfb = gfimg_blur/255.0 15 gfg = np.log(0.5)/np.log(gfb) 16 gfimg2 = 255.0*np.power(fresize_img/255.0,gfg) 17 cv2.imwrite('ganma-'+str(i)+'.jpg',gfimg2) 18 19 #contrast 20 gfclahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) 21 gfcl2 = gfclahe.apply(gfimg2.astype(np.uint8)) 22 cv2.imwrite('cl-'+str(i)+'.jpg',gfcl2) 23 24 #threshold 25 threshold = 100 26 ret, fimg_thresh = cv2.threshold(gfcl2, threshold, 255, cv2.THRESH_BINARY) 27 cv2.imwrite('th-'+str(i)+'.jpg', fimg_thresh) 28 29 #morphology 30 fkernel = np.ones((5,5),np.uint8) 31 frontclosing = cv2.morphologyEx(fimg_thresh,cv2.MORPH_CLOSE,fkernel) 32 cv2.imwrite('mor-'+str(i)+'.jpg',frontclosing) 33 34 35 #Background subtraction 36 fgbg = cv2.bgsegm.createBackgroundSubtractorMOG() 37 fgmask = fgbg.apply(backclosing) 38 fgmask = fgbg.apply(frontclosing) 39 cv2.imwrite('front-'+str(i)+'.jpg',fgmask) 40 41 #remove 42 def remove_objects(img, lower_size=None, upper_size=None): 43 nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(img) 44 sizes = stats[1:, -1] 45 _img = np.zeros((labels.shape)) 46 for i in range(1, nlabels): 47 if (lower_size is not None) and (upper_size is not None): 48 if lower_size < sizes[i - 1] and sizes[i - 1] < upper_size: 49 _img[labels == i] = 255 50 elif (lower_size is not None) and (upper_size is None): 51 if lower_size < sizes[i - 1]: 52 _img[labels == i] = 255 53 elif (lower_size is None) and (upper_size is not None): 54 if sizes[i - 1] < upper_size: 55 _img[labels == i] = 255 56 return _img 57 #org_img= cv2.imread('front-'+str(i)+'.jpg',0) 58 img_remove = remove_objects(fgmask,lower_size=50, upper_size=None) 59 cv2.imwrite('remove-'+str(i)+'.jpg',img_remove) 60 61 #inversion 62 inv = cv2.bitwise_not(img_remove) 63 cv2.imwrite('inv-'+str(i)+'.jpg',inv) 64 65 #haugh 66 wb = excel.Workbook() 67 ws = wb.active 68 ws['A1'] = "width" 69 ws['B1'] = "height" 70 ws['C1'] = "r-pixel" 71 ws['D1'] = "d-pixel" 72 ws['E1'] = "r-μm" 73 ws['F1'] = "d-μm" 74 ws['G1'] = "area-μm" 75 ws.title = "Sheet_1" 76 img_ga = cv2.GaussianBlur(inv,(5,5),0) 77 circles = cv2.HoughCircles(img_ga.astype(np.uint8),cv2.HOUGH_GRADIENT,1,30,param1=10,param2=18,minRadius=25,maxRadius=200) 78 if circles is not None and len(circles) > 0: 79 circles = np.uint16(np.around(circles)) 80 for i in circles[0,:]: 81 cv2.circle(inv,(i[0],i[1]),i[2],(0,255,0),2) 82 cv2.circle(inv,(i[0],i[1]),2,(0,0,255),3) 83 84 ws.append((float(i[0]), float(i[1]),float(i[2]),float(i[2]*2),float(i[2]*41.1*pow(10,-3)),float(i[2]*2*41.1*pow(10,-3)),float((i[2]*41.1)*(i[2]*41.1)*math.pi))) 85 86 wb.save(r"hahu"+str(i)+".xlsx") 87 88 cv2.imwrite('hahu'+str(i)+'.jpg',inv)
回答1件
あなたの回答
tips
プレビュー