简体   繁体   English

Opencv Python 如何检查齿轮的方向?

[英]Opencv Python How to check the direction of the gear?

I'm checking the direction of the gear, whether it is "up or down", "and print the result to the screen".我正在检查齿轮的方向,是“向上还是向下”,“并将结果打印到屏幕上”。 The background color is always black.背景颜色始终为黑色。

This gear is in the "up" position:该齿轮处于“向上”位置:

https://imgur.com/a/DON8GJs https://imgur.com/a/DON8GJs

This gear is in the "down" position:该齿轮处于“向下”位置:

https://imgur.com/a/4ODZQAt https://imgur.com/a/4ODZQAt

I did try to binary the two images and canny edge detection but all I've found is the result of those algorithms, nothing more.!我确实尝试对两个图像进行二值化和精明的边缘检测,但我发现的只是这些算法的结果,仅此而已。! I'm wondering what should I need to do to check the direction of the gear?我想知道我需要做什么来检查齿轮的方向? Your help would be greatly appreciated.!您的帮助将不胜感激。!

The down position has a distinct shape, you can use shapematch to detect the presence of this shape. down 位置有一个明显的形状,你可以使用shapematch来检测这个形状的存在。
To do that, you'll need a reference shape.为此,您需要一个参考形状。 I created this by detecting the edges, saved that image and used MS paint to only leave the shape needed.我通过检测边缘来创建它,保存该图像并使用 MS 绘制仅留下所需的形状。
The code below shows you how to detect the shape.下面的代码向您展示了如何检测形状。 It prints the position of the gear to the terminal and draws the shape if it is in the down position.它将齿轮的位置打印到终端,并在其处于向下位置时绘制形状。

Shapematch is able to handle rotation, but you might need to test and tweak some of the settings if you want the use this in some sort of automation. Shapematch 能够处理旋转,但如果您想在某种自动化中使用它,您可能需要测试和调整一些设置。

Result:结果: 在此处输入图片说明

Image of reference gear: https://i.stack.imgur.com/s6E9C.jpg参考齿轮图片: https : //i.stack.imgur.com/s6E9C.jpg

Code:代码:

import numpy as np 
import cv2

# load image of reference shape
image_reference = cv2.imread("ReferenceGear.jpg",0)
# threshold to remove artefacts
ret, img_ref = cv2.threshold(image_reference, 200, 255,0)
# detect contours in image
im, ref_cnts, hierarchy = cv2.findContours(img_ref, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# store the contour of the reference shape in a variable
ref_cnt = ref_cnts[0]


# load image 
image = cv2.imread("gear.png")
# detect edges in image
edges = cv2.Canny(image,50,50)
# detect contours of edges in image
im, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# for each edge-contour in the image: try to match with te reference.
# if the value is very small, it is a good match. store the result in a variable
found = False
for cnt in contours:
    ret = cv2.matchShapes(cnt, ref_cnt,3,0.0)
    if ret < 0.001:
        cv2.drawContours(image, [cnt], 0, (255), 2)
        found = True
        break

if found:
    print("Gear is in down position")
else:
    print("Gear is in up position")

# show image and reference
cv2.imshow("image", image)
cv2.imshow("image_reference", image_reference)
# release resources
cv2.waitKey(0)
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM