当前,我正在使用轮廓在图像中查找所有形状。查找所有形状的效果很好,但是我想将这些形状(圆形和正方形)转换为HoughCircles和BoundingRects,以便获得像素尺寸。有没有简单的方法可以做到这一点?
代码:
import cv2
import matplotlib.pyplot as plt
import numpy as np
# read the image
image = cv2.imread("shapes3.png")
# convert to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
#edge detection
edgeDet = cv2.Canny(gray, 75, 200)
# create a binary thresholded image
#_, binary = cv2.threshold(edgeDet, 200, 200, cv2.THRESH_BINARY_INV)
# show it
plt.imshow(edgeDet, cmap="gray")
plt.show()
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw all contours
contour_list = []
for contour in contours:
approx = cv2.approxPolyDP(contour,0.01*cv2.arcLength(contour,True),True)
area = cv2.contourArea(contour)
if ((len(approx) > 8) & (area > 30) ):
contour_list.append(contour)
image = cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
plt.imshow(image)
plt.show()
print(contours)
输出:
[array([[[ 58, 54]],
[[ 57, 55]],
[[ 55, 55]],
[[ 55, 56]],
[[ 54, 57]],
[[ 54, 443]],
[[ 55, 444]],
[[ 55, 445]],
[[445, 445]],
[[445, 55]],
[[442, 55]],
[[441, 54]]], dtype=int32), array([[[ 57, 55]],...]]