簡體   English   中英

OpenCV 4.4.0 錯誤:(-215:斷言失敗)._img:empty() 在 function 'cv::imwrite'

[英]OpenCV 4.4.0 error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

我想檢測一些圖像中的圓圈並裁剪這些圖像。 以下代碼給出了標題中解決的錯誤。 讓我感到奇怪的是,即使仍然顯示錯誤,我的輸入文件夾中的第一張圖片實際上也被相應地裁剪了。 但是,同一文件夾中的任何其他圖像都被忽略了(可能是錯誤消息中斷了循環?)

我已經添加了“print(images)”行,以確保輸入文件夾實際上包含所有必需的圖像,並且它成功地打印了所選文件夾中圖像的名稱。

誰能幫我解決這個問題?

如果需要任何其他信息或者這個問題太不准確,請告訴我!

img_raw = r'C:/Users/Chris/Documents/Extrahierte Bilder/Beispiel' # raw images
img_circles = r'C:/Users/Chris/Documents/Extrahierte Bilder/Beispiel' # cropped circles

images = os.listdir(img_raw)
print(images)

for image in images:

# define output name
output_name = 'cropped_' + str(image)
# define path to image:
path = str(img_raw) + '/' + str(image)
# read image:
image = cv2.imread(path)
# create copy of image:
output = image.copy()
# convert copy to gray scale
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 800)

if circles is not None:
    
    for c in circles[0, :]:
        c = c.astype(int)
        # crop the image:
        crop = gray[c[1]-c[2]:c[1]+c[2], c[0]-c[2]:c[0]+c[2]]
        # write cropped image to file:
        cv2.imwrite(img_circles + '/' + str(output_name), crop)    
                    
else:
    
    print('No circle(s) detected for ' + str(image))

您需要注意的事情,文件路徑中不能存在空格,使用 os.sep repalce '/' 可能有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM