我正在一个图像处理项目中,它包含人形图作为输入图像,我想检测图像中的圆和所绘制图形的裁剪面(一次检测文件夹中的所有图像,因为数据集很大),我该怎么办?我为此使用OpenCV python,下面给出了一次仅检测一张图像中的圆圈的代码:
import cv2
import numpy as np
img1 = cv2.imread('C:\\Users\\LENOVO\\introvert\\1.jpeg')
img = cv2.imread('C:\\Users\\LENOVO\\introvert\\1.jpeg',0)
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
# Create mask
height,width = img.shape
mask = np.zeros((height,width), np.uint8)
edges = cv2.Canny(thresh, 100, 200)
cv2.imshow('detected ',gray)
cimg=cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 10000, param1 = 50, param2 = 30, minRadius = 0, maxRadius = 0)
for i in circles[0,:]:
i[2]=i[2]+4
# Draw on mask
cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),thickness=-1)
#Copy that image using that mask
masked_data = cv2.bitwise_and(img1, img1, mask=mask)
# Apply Threshold
_,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY)
# Find Contour
contours = cv2.findContours (thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])
# Crop masked_data
crop = masked_data[y:y+h,x:x+w]
#Code to close Window
cv2.imshow('detected Edge',img1)
cv2.imshow('Cropped face',crop)
cv2.waitKey(0)
cv2.imwrite('C:\\Users\\LENOVO\\introvert\\cropped1.jpeg',crop)
cv2.destroyAllWindows()