<bdo id='sYLLA'></bdo><ul id='sYLLA'></ul>

    <i id='sYLLA'><tr id='sYLLA'><dt id='sYLLA'><q id='sYLLA'><span id='sYLLA'><b id='sYLLA'><form id='sYLLA'><ins id='sYLLA'></ins><ul id='sYLLA'></ul><sub id='sYLLA'></sub></form><legend id='sYLLA'></legend><bdo id='sYLLA'><pre id='sYLLA'><center id='sYLLA'></center></pre></bdo></b><th id='sYLLA'></th></span></q></dt></tr></i><div id='sYLLA'><tfoot id='sYLLA'></tfoot><dl id='sYLLA'><fieldset id='sYLLA'></fieldset></dl></div>
    <tfoot id='sYLLA'></tfoot>

      <small id='sYLLA'></small><noframes id='sYLLA'>

    1. <legend id='sYLLA'><style id='sYLLA'><dir id='sYLLA'><q id='sYLLA'></q></dir></style></legend>

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

        时间:2023-07-03
          <legend id='PNZy9'><style id='PNZy9'><dir id='PNZy9'><q id='PNZy9'></q></dir></style></legend>
            <tbody id='PNZy9'></tbody>
          <tfoot id='PNZy9'></tfoot>
            <i id='PNZy9'><tr id='PNZy9'><dt id='PNZy9'><q id='PNZy9'><span id='PNZy9'><b id='PNZy9'><form id='PNZy9'><ins id='PNZy9'></ins><ul id='PNZy9'></ul><sub id='PNZy9'></sub></form><legend id='PNZy9'></legend><bdo id='PNZy9'><pre id='PNZy9'><center id='PNZy9'></center></pre></bdo></b><th id='PNZy9'></th></span></q></dt></tr></i><div id='PNZy9'><tfoot id='PNZy9'></tfoot><dl id='PNZy9'><fieldset id='PNZy9'></fieldset></dl></div>
            • <bdo id='PNZy9'></bdo><ul id='PNZy9'></ul>

                <small id='PNZy9'></small><noframes id='PNZy9'>

                  本文介绍了如何找到对象(形状)的方向?- Python Opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的图像总是这样:

                  但我需要将它们旋转成这样:

                  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. 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.

                  推荐答案

                  这是 Python/OpenCV 中的一种方法.

                  Here is one way in Python/OpenCV.

                  • 阅读图片

                  • Read the image

                  转为灰度

                  阈值

                  获取外轮廓

                  从外轮廓获取 minAreaRect 点和角度

                  Get minAreaRect points and angle from outer contour

                  获取旋转矩形的顶点

                  画出旋转的矩形

                  根据需要校正角度

                  打印角度

                  保存带有旋转矩形的图像

                  Save the image with the rotated rectangle drawn on it

                  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()
                  


                  返回角度:0.8814040422439575 度

                  带有旋转矩形的图像:


                  Angle Returned: 0.8814040422439575 deg

                  Image with Rotated Rectangle:

                  这篇关于如何找到对象(形状)的方向?- Python Opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 grid_2d_graph 在 networkx 中绘制 MxM 节点的方形网格时移除旋转效果 下一篇:在 Python 中以角度(旋转)绘制文本

                  相关文章

                  <legend id='BDNcP'><style id='BDNcP'><dir id='BDNcP'><q id='BDNcP'></q></dir></style></legend>
                1. <i id='BDNcP'><tr id='BDNcP'><dt id='BDNcP'><q id='BDNcP'><span id='BDNcP'><b id='BDNcP'><form id='BDNcP'><ins id='BDNcP'></ins><ul id='BDNcP'></ul><sub id='BDNcP'></sub></form><legend id='BDNcP'></legend><bdo id='BDNcP'><pre id='BDNcP'><center id='BDNcP'></center></pre></bdo></b><th id='BDNcP'></th></span></q></dt></tr></i><div id='BDNcP'><tfoot id='BDNcP'></tfoot><dl id='BDNcP'><fieldset id='BDNcP'></fieldset></dl></div>

                  1. <tfoot id='BDNcP'></tfoot>
                      <bdo id='BDNcP'></bdo><ul id='BDNcP'></ul>

                    <small id='BDNcP'></small><noframes id='BDNcP'>