【发布时间】: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))
【问题讨论】: