0

我正在使用此处找到的附加代码来查找对象的方向:

import cv2
import numpy as np

# load image as HSV and select saturation
img = cv2.imread("wing2.png")
hh, ww, cc = img.shape

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold the grayscale image
ret, thresh = cv2.threshold(gray, 0, 255, 0)
#ret, thresh = cv2.threshold(gray, 165, 255, cv2.THRESH_BINARY)

# find outer contour
cntrs = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#cntrs = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]


# get rotated rectangle from outer contour
rotrect = cv2.minAreaRect(cntrs[0])
box = cv2.boxPoints(rotrect)
box = np.int0(box)

# draw rotated rectangle on copy of img as result
result = img.copy()
cv2.drawContours(result, [box], 0, (0, 0, 255), 2)

# get angle from rotated rectangle
angle = rotrect[-1]

# from https://www.pyimagesearch.com/2017/02/20/text-skew-correction-opencv-python/
# the `cv2.minAreaRect` function returns values in the
# range [-90, 0); as the rectangle rotates clockwise the
# returned angle trends to 0 -- in this special case we
# need to add 90 degrees to the angle
if angle < -45:
    angle = -(90 + angle)

# otherwise, just take the inverse of the angle to make
# it positive
else:
    angle = -angle

print(angle, "deg")

# write result to disk
cv2.imwrite("wing2_rotrect.png", result)

cv2.imshow("THRESH", thresh)
cv2.imshow("RESULT", result)
cv2.imshow("gray", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

但我有两个主要问题,

1. 我无法找到 - 使用代码 - 其他图像中的对象的方向(附有示例图像),但只能找到线程中找到的原始图像的方向。

在此处输入图像描述 在此处输入图像描述

2. 我没有成功转换代码以实时找到相机源中对象的方向 - 可以在此视频中看到:https ://www.youtube.com/watch?v=Dxdy6Rzo7d0

由于我是 OpenCV 的新手,我没有解决这个问题的线索,我将非常感谢您的帮助,

谢谢!

阿维谢

4

0 回答 0