我有几张苗圃的图片.我想运行一个代码来切割每个方形锅并存储它.
To do this, first convert it to gray image then to reduce noise bluer it and finally convert it to binary using threshold.
现在我试图使用cv.findContours找到每个正方形的几何位置,但似乎只能返回图像角落中的点,而不是找到每个正方形的角落.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
edges = cv2.Canny(blurred, 10, 50)
thresh = cv2.adaptiveThreshold(gray, 400, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 7, 4)
lines = cv2.HoughLines(thresh, rho=1, theta=np.pi/180, threshold=100)
contours, hierarchy = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
squares = []
for contour in contours:
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
if len(approx) == 4:
x, y, w, h = cv2.boundingRect(approx)
aspect_ratio = float(w) / h
if 0.98 <= aspect_ratio <= 1: # Adjust this range as per your requirement
squares.append(approx)
# Draw squares on the original image
for square in squares:
cv2.drawContours(image, [square], -1, (0, 255, 0), 2)
结果的问题是我得到的很多点并不完全是每个锅的边界. 我很感激您的建议或帮助,我如何优化它
我已经搜索过之前的帖子,但大多数都是在光线和对比度足够的情况下进行的,但在这种情况下不适用