简体   繁体   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'

I want to detect circles in a few images and have these images cropped.我想检测一些图像中的圆圈并裁剪这些图像。 The following code gives the error addressed in the title.以下代码给出了标题中解决的错误。 What makes me wonder is that the first picture in my input folder is actually being cropped accordingly even though the error is still being displayed.让我感到奇怪的是,即使仍然显示错误,我的输入文件夹中的第一张图片实际上也被相应地裁剪了。 Any other images in the same folder are being ignored though (probably bc the error message interrupts the loop?)但是,同一文件夹中的任何其他图像都被忽略了(可能是错误消息中断了循环?)

I already added the "print(images)" line in order to assure that the input folder actually contains all the required images and it successfully does print the names of the images in the chosen folder.我已经添加了“print(images)”行,以确保输入文件夹实际上包含所有必需的图像,并且它成功地打印了所选文件夹中图像的名称。

Can anyone help me sorting this issue out?谁能帮我解决这个问题?

If any additional information is required or if this question was too inaccurate, please let me know!如果需要任何其他信息或者这个问题太不准确,请告诉我!

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))

things you need to attention, space can not exist in your file path, using os.sep repalce '/' may works.您需要注意的事情,文件路径中不能存在空格,使用 os.sep repalce '/' 可能有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cv2.error : (-215:Assertion failed) !_img.empty() in function 'imwrite' - Cv2.error : (-215:Assertion failed) !_img.empty() in function 'imwrite' 删除以下内容:错误:(-215:Assertion failed) !_img.empty() in function 'cv::imwrite' - remove following: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite' 如何删除此错误 :(-215:Assertion failed) !_img.empty() in function 'cv::imwrite' - How to remove this error :(-215:Assertion failed) !_img.empty() in function 'cv::imwrite' 在 Google-Colab 中出现错误(错误:(-215:Assertion failed) !_img.empty() in function 'imwrite') - Getting error in Google-Colab (error: (-215:Assertion failed) !_img.empty() in function 'imwrite') 错误: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:715: 错误: (-215:Assertion failed) !_img.empty() in function 'imwrite' - error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'imwrite' OpenCV(4.2.0),imwrite() 到子文件夹会破坏模块; cv2.error: (-215:Assertion failed).ssize:empty() in function 'cv::resize'`' - OpenCV(4.2.0),imwrite() to a subfolder breaks the module; cv2.error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'`' OpenCV(4.4.0) 错误: (-215:Assertion failed) isMap() in function 'cv::FileNode::operator []' - OpenCV(4.4.0) error: (-215:Assertion failed) isMap() in function 'cv::FileNode::operator []' 错误:函数“imwrite”中的 !_img.empty() - Error: !_img.empty() in function 'imwrite' cv2.error: OpenCV(4.5.2).error: (-215:Assertion failed)._src:empty() in function 'cv::cvtColor' - cv2.error: OpenCV(4.5.2) .error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' 错误:OpenCV(4.1.0) 错误:(-215:Assertion failed) !ssize.empty() in function 'cv::resize' - error: OpenCV(4.1.0) error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM