【问题标题】:OpenCV 4.4.0 error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'OpenCV 4.4.0 错误: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
【发布时间】:2021-04-14 01:10:23
【问题描述】:

我想检测几张图片中的圆圈并裁剪这些图片。 以下代码给出了标题中解决的错误。 让我想知道的是,即使仍然显示错误,我的输入文件夹中的第一张图片实际上也被相应地裁剪了。 但是,同一文件夹中的任何其他图像都被忽略了(可能是错误消息中断了循环?)

我已经添加了“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))

【问题讨论】:

    标签: python opencv


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-06-07
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 2020-10-13
      • 2019-12-07
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多