我正在尝试在图像中找到不同的形状。检测形状后,将打印每个形状的质心。直到这个阶段我才能实现。现在,我想找到并显示具有相同质心值的不同形状。我被困在那里。或者我可以阅读并解决这些质心值。谁能帮我
from __future__ import print_function
import numpy as np
import cv2
import argparse
# Using Argument Parser to get the location of image
ap = argparse.ArgumentParser()
##ap.add_argument('-i', '--image', required=True, help='Path to image')
args = ap.parse_args()
# load the image on disk and then display it
image = cv2.imread('/IMAGE PATH')
cv2.imshow("Original", image)
# convert the color image into grayscale
grayScale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find edges in the image using canny edge detection method
# Calculate lower threshold and upper threshold using sigma = 0.33
sigma = 0.33
v = np.median(grayScale)
low = int(max(0, (1.0 - sigma) * v))
high = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(grayScale, low, high)
# After finding edges we have to find contours
# Contour is a curve of points with no gaps in the curve
# It will help us to find location of shapes
# cv2.RETR_EXTERNAL is passed to find the outermost contours (because we
want to outline the shapes)
# cv2.CHAIN_APPROX_SIMPLE is removing redundant points along a line
(_, cnts, _) = cv2.findContours(edged,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
'''
We are going to use contour approximation method to find vertices of
geometric shapes. The alogrithm is also known as Ramer Douglas Peucker
alogrithm.
In OpenCV it is implemented in cv2.approxPolyDP method.abs
detectShape() function below takes a contour as parameter and
then returns its shape
'''
def detectShape(cnt):
shape = 'unknown'
# calculate perimeter using
peri = cv2.arcLength(c, True)
# apply contour approximation and store the result in vertices
vertices = cv2.approxPolyDP(c, 0.04 * peri, True)
# if the shape has 4 vertices, it is either a square or
# a rectangle
if len(vertices) == 4:
# using the boundingRect method calculate the width and height
# of enclosing rectange and then calculte aspect ratio
x, y, width, height = cv2.boundingRect(vertices)
aspectRatio = float(width) / height
# a square will have an aspect ratio that is approximately
# equal to one, otherwise, the shape is a rectangle
if aspectRatio >= 0.95 and aspectRatio <= 1.05:
shape = "square"
else:
shape = "rectangle"
# if the shape is a pentagon, it will have 5 vertices
## elif len(vertices) == 5:
## shape = "pentagon"
# otherwise, we assume the shape is a circle
else:
shape = "circle"
# return the name of the shape
return shape
# Now we will loop over every contour
# call detectShape() for it and
# write the name of shape in the center of image
# loop over the contours
for c in cnts:
# compute the moment of contour
M = cv2.moments(c)
# From moment we can calculte area, centroid etc
# The center or centroid can be calculated as follows
cX = int(M['m10'] / (M['m00']+1))
cY = int(M['m01'] / (M['m00']+1))
## centroids=[(int((M["m10"]) // (M["m00"]+1)), int((M["m01"] //
##(M["m00"]+1))))]
centroids=[cX,cY]
ar=M['m00']
if (ar>110):
print('centrout',centroids[0])
# call detectShape for contour c
shape = detectShape(c)
print(shape)
print('area',ar)
# Outline the contours
cv2.drawContours(image, [c], -1, (0, 255, 0), 1)
# show the output image
cv2.circle(image, (cX, cY), 3, (255, 0, 0), -1)
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()