python opencv 试图用矩量法找到轮廓的中心
python opencv trying to find center of contours with moments method
这是我的代码。
import cv2
#detect all centers of objects
#mark all centers
#return modified image
def markCenter(canny,img):
#get contours using cv2 method - "findContours"
contours,hierarchy = cv2.findContours(canny,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
#sort contour by area
contours = sorted(contours, key=lambda x: cv2.contourArea(x))
contours = sorted(contours, key=cv2.contourArea,reverse=True)
#iterate over contour list
for cnt in contours:
#get center of contour using cv2 method - "moments"
M = cv2.moments(cnt)
#parse returned data from "moments"
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# draw the center of the shape on the image
print(cX,cY) #print coordinates
print(img[cX,cY]) #print value of pixel in center of contour
cv2.circle(img, (cX, cY), 1, (255, 255, 255), -1) #mark the center of the contour
return img #return modified image
def main():
#read image
img = cv2.imread("resources/result_shapedet.jpg")
#show image
cv2.imshow("original",img)
#convert image to greyscale
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#blurr image
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 1)
#detect edges of objects with canny method
imgCanny = cv2.Canny(imgBlur, 40, 40)
#show image with marked centers
cv2.imshow("centers",markCenter(imgCanny,img))
#never close image windows
cv2.waitKey(0)
if __name__=="__main__":
main()
函数 markCenter
找到等高线及其中心。
该函数设法找到实际上可以标记中心,但是当我试图找到中心像素的值时,我从另一个像素得到错误的 answer/answer。
这是为什么,我该如何解决?
Numpy 使用 rows/cols(又称 y/x),而 OpenCV 使用 x/y。当你打印时,你使用的是 numpy,所以 print(img[cX,cY]) 需要变成 print(img[cY,cX])
可以在此处找到这种常见混淆的一个很好的例子:
还要记住,OpenCV 使用的是 BGR,所以如果你使用 RGB 作为参考值来识别颜色,你需要反转元素。
img_RGB = img[::-1]
这是我的代码。
import cv2
#detect all centers of objects
#mark all centers
#return modified image
def markCenter(canny,img):
#get contours using cv2 method - "findContours"
contours,hierarchy = cv2.findContours(canny,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
#sort contour by area
contours = sorted(contours, key=lambda x: cv2.contourArea(x))
contours = sorted(contours, key=cv2.contourArea,reverse=True)
#iterate over contour list
for cnt in contours:
#get center of contour using cv2 method - "moments"
M = cv2.moments(cnt)
#parse returned data from "moments"
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# draw the center of the shape on the image
print(cX,cY) #print coordinates
print(img[cX,cY]) #print value of pixel in center of contour
cv2.circle(img, (cX, cY), 1, (255, 255, 255), -1) #mark the center of the contour
return img #return modified image
def main():
#read image
img = cv2.imread("resources/result_shapedet.jpg")
#show image
cv2.imshow("original",img)
#convert image to greyscale
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#blurr image
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 1)
#detect edges of objects with canny method
imgCanny = cv2.Canny(imgBlur, 40, 40)
#show image with marked centers
cv2.imshow("centers",markCenter(imgCanny,img))
#never close image windows
cv2.waitKey(0)
if __name__=="__main__":
main()
函数 markCenter
找到等高线及其中心。
该函数设法找到实际上可以标记中心,但是当我试图找到中心像素的值时,我从另一个像素得到错误的 answer/answer。
这是为什么,我该如何解决?
Numpy 使用 rows/cols(又称 y/x),而 OpenCV 使用 x/y。当你打印时,你使用的是 numpy,所以 print(img[cX,cY]) 需要变成 print(img[cY,cX])
可以在此处找到这种常见混淆的一个很好的例子:
还要记住,OpenCV 使用的是 BGR,所以如果你使用 RGB 作为参考值来识别颜色,你需要反转元素。
img_RGB = img[::-1]