简体   繁体   English

如何找到 object(形状)的方向? - Python Opencv

[英]How to find the orientation of an object (shape)? - Python Opencv

My images are always like this:我的图像总是这样: 原始图像

原始图像

But I need to rotate them to be like this:但我需要将它们旋转成这样: 旋转图像 旋转图像

But to do that, I need to find the orientation of the object, knowing that the thinner part of the object has to be on the left side.但要做到这一点,我需要找到 object 的方向,知道 object 的较薄部分必须在左侧。 In summary, the images are wings and the start of the wing has to be on the left side and the end of the wing has to be on the right side.总之,图像是机翼,机翼的起点必须在左侧,机翼的末端必须在右侧。

I hope someone can give me a suggestion, I've tried a bunch of different strategies but with no good result so far.我希望有人能给我一个建议,我尝试了很多不同的策略,但到目前为止都没有好的结果。

Here is one way in Python/OpenCV.这是 Python/OpenCV 中的一种方法。

  • Read the image阅读图片

  • Convert to grayscale转换为灰度

  • Threshold临界点

  • Get outer contour获取外轮廓

  • Get minAreaRect points and angle from outer contour从外轮廓获取 minAreaRect 点和角度

  • Get vertices of rotated rectangle获取旋转矩形的顶点

  • Draw the rotated rectangle绘制旋转的矩形

  • Correct the angle as needed根据需要校正角度

  • Print the angle打印角度

  • Save the image with the rotated rectangle drawn on it保存带有绘制在其上的旋转矩形的图像


Input: 输入:

在此处输入图像描述

 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) # find outer contour cntrs = cv2.findContours(thresh, cv2.RETR_EXTERNAL, 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.waitKey(0) cv2.destroyAllWindows()

Angle Returned: 0.8814040422439575 deg 返回角度:0.8814040422439575 度

Image with Rotated Rectangle:带有旋转矩形的图像:

在此处输入图像描述

You have to compute the main axis (it is based on PCA).您必须计算主轴(它基于 PCA)。 It will give you a good idea of the main orientation, then you can rotate your image accordingly.它会让您对主要方向有一个很好的了解,然后您可以相应地旋转图像。

As it was pointed as a comment, you now have to test that the thin part is on the right side of the image, and for that you use the centroid/barycenter: if the centroid is on the left of the bounding box, then the wing is well oriented.正如评论指出的那样,您现在必须测试薄部分是否位于图像的右侧,为此您使用质心/重心:如果质心位于边界框的左侧,则机翼方向很好。

Here is the complete algorithm:这是完整的算法:

  • Compute the main orientation (main axis, computed with PCA)计算主方向(主轴,用 PCA 计算)
  • Rotate your image according to the main axis orientation.根据主轴方向旋转图像。
  • Compute the bounding box and centroid/barycenter计算边界框和质心/重心
  • If the centroid is on the left, then your image is well oriented, else rotate it about 180°.如果质心在左侧,则您的图像方向良好,否则将其旋转约 180°。

Here are the results...这是结果... 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

I used this python library for oriented object detection many times for similar tasks.对于类似的任务,我多次使用这个python 库进行定向 object 检测。 This is a trainable neural network detecting the positions and orientations of objects.这是一个可训练的神经网络,用于检测物体的位置和方向。 When you know the orientation angle, you can rotate the object to the desired angle using opencv.当您知道方向角度后,您可以使用 opencv 将 object 旋转到所需的角度。 You will need to label some images to train the network.您将需要 label 一些图像来训练网络。

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

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