我寫了以下代碼:import cv2 import lxml.etree as xmlimport osimport shutilfor filename in os.listdir(paths['labels']): with open(paths['labels']+filename,'r'): img2 = cv2.imread(filename, cv2.IMREAD_COLOR) # Reading same image in another # variable and converting to gray scale. img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) # Converting image to a binary image # ( black and white only image). #_, threshold = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY) blur = cv2.GaussianBlur(img,(5,5),0) _, threshold = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) # Detecting contours in image. contours, _= cv2.findContours(threshold, cv2.RETR_TREE, #here it finds the total number of contours, i.e, number of rectangles in the image file cv2.CHAIN_APPROX_SIMPLE)我在第 18 行中收到錯誤:(blur = cv2.GaussianBlur(img,(5,5),0))錯誤:(-215:Assertion failed) dims <= 2 && step[0] > 0 in function 'cv::Mat::locateROI'代碼之前工作正常,錯誤突然出現。我嘗試將圖像擴展名從jpg更改為png,但錯誤仍然存在。
1 回答

拉莫斯之舞
TA貢獻1820條經驗 獲得超10個贊
當您的代碼未收到正確的圖像(即圖像的路徑可能無效或圖像本身存在一些問題)時,最常發生此錯誤。問題是,您可以嘗試檢查您的圖像是否正確加載:
cv2.imshow('image',img)
此命令用于在OpenCV中顯示圖像,我認為您顯然很熟悉。因此,在使用高斯模糊之前,請使用此命令來驗證您提供的圖像是否正確加載。
如果圖像沒有顯示,那么您可以確定您正面臨上述問題之一!您現在將在此行中收到斷言失敗錯誤。
PS:ROI的完整形式是感興趣的區域。在您的例子中,OpenCV無法找到它將執行高斯模糊的感興趣區域。
添加回答
舉報
0/150
提交
取消