前提・実現したいこと
前回の質問と同じような内容です。
ハフ変換を行う際に、前処理の結果を引き継いで処理したいです。
何か方法があれば教えていただけますでしょうか。よろしくお願いいたします。
発生している問題・エラーメッセージ
error Traceback (most recent call last) <ipython-input-16-59ad1902ff98> in <module> 116 ws.title = "Sheet_1" 117 img_ga = cv2.GaussianBlur(inv,(5,5),0) --> 118 circles = cv2.HoughCircles(img_ga,cv2.HOUGH_GRADIENT,1,50,param1=10,param2=20,minRadius=5,maxRadius=80) 119 circles = np.uint16(np.around(circles)) 120 for i in circles[0,:]: error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-71670poj\opencv\modules\imgproc\src\hough.cpp:2253: error: (-215:Assertion failed) !_image.empty() && _image.type() == CV_8UC1 && (_image.isMat() || _image.isUMat()) in function 'cv::HoughCircles'
該当のソースコード
python
1 img = cv2.imread('DSC0'+str(i)+'.jpg',0) 2 height = img.shape[0] 3 width = img.shape[1] 4 resize_img = cv2.resize(img,(int(width*0.5),int(height*0.5))) 5 6 #ganma 7 img_blur = cv2.GaussianBlur(resize_img,(499,499),0) 8 height = img_blur.shape[0] 9 width = img_blur.shape[1] 10 np.clip(height, 1,254) 11 np.clip(width,1,254) 12 b = img_blur/255.0 13 g = np.log(0.5)/np.log(b) 14 img2 = 255.0*np.power(resize_img/255.0,g) 15 cv2.imwrite('ganma-'+str(i)+'.jpg',img2) 16 17 #contrast 18 img3 = cv2.imread('ganma-'+str(i)+'.jpg',0) 19 clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) 20 cl2 = clahe.apply(img3.astype(np.uint8)) 21 cv2.imwrite('cl-'+str(i)+'.jpg',cl2) 22 23 #threshold 24 threshold = 100 25 ret, img_thresh = cv2.threshold(cl1, threshold, 255, cv2.THRESH_BINARY) 26 cv2.imwrite('th-'+str(i)+'.jpg', img_thresh) 27 28 #morphology 29 kernel = np.ones((5,5),np.uint8) 30 closing = cv2.morphologyEx(img_thresh,cv2.MORPH_CLOSE,kernel) 31 cv2.imwrite('mor-'+str(i)+'.jpg',closing) 32 frontimg = closing 33 34 #Background subtraction 35 fgbg = cv2.bgsegm.createBackgroundSubtractorMOG() 36 fgmask = fgbg.apply(backimg) 37 fgmask = fgbg.apply(frontimg) 38 cv2.imwrite('front-'+str(i)+'.jpg',fgmask) 39 40 #remove 41 def remove_objects(img, lower_size=None, upper_size=None): 42 nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(img) 43 sizes = stats[1:, -1] 44 _img = np.zeros((labels.shape)) 45 for i in range(1, nlabels): 46 if (lower_size is not None) and (upper_size is not None): 47 if lower_size < sizes[i - 1] and sizes[i - 1] < upper_size: 48 _img[labels == i] = 255 49 elif (lower_size is not None) and (upper_size is None): 50 if lower_size < sizes[i - 1]: 51 _img[labels == i] = 255 52 elif (lower_size is None) and (upper_size is not None): 53 if sizes[i - 1] < upper_size: 54 _img[labels == i] = 255 55 return _img 56 #org_img= cv2.imread('front-'+str(i)+'.jpg',0) 57 img_remove = remove_objects(fgmask,lower_size=50, upper_size=None) 58 cv2.imwrite('remove-'+str(i)+'.jpg',img_remove) 59 60 #inversion 61 inv = cv2.bitwise_not(img_remove) 62 63 #haugh 64 wb = excel.Workbook() 65 ws = wb.active 66 ws['A1'] = "width" 67 ws['B1'] = "height" 68 ws['C1'] = "r-pixel" 69 ws['D1'] = "d-pixel" 70 ws['E1'] = "r-μm" 71 ws['F1'] = "d-μm" 72 ws['G1'] = "area-μm" 73 ws.title = "Sheet_1" 74 img_ga = cv2.GaussianBlur(inv,(5,5),0) 75 circles = cv2.HoughCircles(img_ga,cv2.HOUGH_GRADIENT,1,50,param1=10,param2=20,minRadius=5,maxRadius=80) 76 circles = np.uint16(np.around(circles)) 77 for i in circles[0,:]: 78 cv2.circle(inv,(i[0],i[1]),i[2],(0,255,0),2) 79 cv2.circle(inv,(i[0],i[1]),2,(0,0,255),3) 80 81 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))) 82 83 wb.save(r"hahu"+str(i)+".xlsx") 84 85 cv2.imwrite('hahu'+str(i)+'.jpg',inv)
試したこと
とりあえずハフ変換の部分を前回の質問での回答を参考に
python
1circles = cv2.HoughCircles(img_ga.astype(np.unit8),cv2.HOUGH_GRADIENT,1,50,param1=10,param2=20,minRadius=5,maxRadius=80)
としたところ
AttributeError: module 'numpy' has no attribute 'unit8'
とエラーが出てしまい頓挫しています。
回答1件
あなたの回答
tips
プレビュー