<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
在本教學中,我們將構建一個程式,該程式可以使用流行的計算機視覺庫 OpenCV 確定物件的方向(即以度為單位的旋轉角度)。
最常見的現實世界用例之一是當您想要開發機械臂的取放系統時。確定一個物體在傳送帶上的方向是確定合適的抓取、撿起物體並將其放置在另一個位置的關鍵。
接受一個名為input_img.jpg
的影象,並輸出一個名為output_img.jpg
的帶標記的影象。部分程式碼來自官方的OpenCV實現。
import cv2 as cv from math import atan2, cos, sin, sqrt, pi import numpy as np def drawAxis(img, p_, q_, color, scale): p = list(p_) q = list(q_) ## [visualization1] angle = atan2(p[1] - q[1], p[0] - q[0]) # angle in radians hypotenuse = sqrt((p[1] - q[1]) * (p[1] - q[1]) + (p[0] - q[0]) * (p[0] - q[0])) # Here we lengthen the arrow by a factor of scale q[0] = p[0] - scale * hypotenuse * cos(angle) q[1] = p[1] - scale * hypotenuse * sin(angle) cv.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv.LINE_AA) # create the arrow hooks p[0] = q[0] + 9 * cos(angle + pi / 4) p[1] = q[1] + 9 * sin(angle + pi / 4) cv.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv.LINE_AA) p[0] = q[0] + 9 * cos(angle - pi / 4) p[1] = q[1] + 9 * sin(angle - pi / 4) cv.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv.LINE_AA) ## [visualization1] def getOrientation(pts, img): ## [pca] # Construct a buffer used by the pca analysis sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0,0]), int(mean[0,1])) ## [pca] ## [visualization] # Draw the principal components cv.circle(img, cntr, 3, (255, 0, 255), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0,0] * eigenvalues[0,0], cntr[1] + 0.02 * eigenvectors[0,1] * eigenvalues[0,0]) p2 = (cntr[0] - 0.02 * eigenvectors[1,0] * eigenvalues[1,0], cntr[1] - 0.02 * eigenvectors[1,1] * eigenvalues[1,0]) drawAxis(img, cntr, p1, (255, 255, 0), 1) drawAxis(img, cntr, p2, (0, 0, 255), 5) angle = atan2(eigenvectors[0,1], eigenvectors[0,0]) # orientation in radians ## [visualization] # Label with the rotation angle label = " Rotation Angle: " + str(-int(np.rad2deg(angle)) - 90) + " degrees" textbox = cv.rectangle(img, (cntr[0], cntr[1]-25), (cntr[0] + 250, cntr[1] + 10), (255,255,255), -1) cv.putText(img, label, (cntr[0], cntr[1]), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv.LINE_AA) return angle # Load the image img = cv.imread("input_img.jpg") # Was the image there? if img is None: print("Error: File not found") exit(0) cv.imshow('Input Image', img) # Convert image to grayscale gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # Convert image to binary _, bw = cv.threshold(gray, 50, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) # Find all the contours in the thresholded image contours, _ = cv.findContours(bw, cv.RETR_LIST, cv.CHAIN_APPROX_NONE) for i, c in enumerate(contours): # Calculate the area of each contour area = cv.contourArea(c) # Ignore contours that are too small or too large if area < 3700 or 100000 < area: continue # Draw each contour only for visualisation purposes cv.drawContours(img, contours, i, (0, 0, 255), 2) # Find the orientation of each shape getOrientation(c, img) cv.imshow('Output Image', img) cv.waitKey(0) cv.destroyAllWindows() # Save the output image to the current directory cv.imwrite("output_img.jpg", img)
紅線表示每個物體的正x軸。藍線表示每個物體的正y軸。
全域性正x軸從左到右橫貫影象。整體正z軸指向這一頁外。全域性正y軸從影象的底部垂直指向影象的頂部。
使用右手法則來測量旋轉,你將你的四個手指(食指到小指)筆直地指向全域性正x軸的方向。
然後逆時針旋轉四個手指90度。指尖指向y軸正方向,大拇指指向紙外z軸正方向。
如果我們想計算一個物件的方向,並確保結果總是在0到180度之間,我們可以使用以下程式碼:
# This programs calculates the orientation of an object. # The input is an image, and the output is an annotated image # with the angle of otientation for each object (0 to 180 degrees) import cv2 as cv from math import atan2, cos, sin, sqrt, pi import numpy as np # Load the image img = cv.imread("input_img.jpg") # Was the image there? if img is None: print("Error: File not found") exit(0) cv.imshow('Input Image', img) # Convert image to grayscale gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # Convert image to binary _, bw = cv.threshold(gray, 50, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) # Find all the contours in the thresholded image contours, _ = cv.findContours(bw, cv.RETR_LIST, cv.CHAIN_APPROX_NONE) for i, c in enumerate(contours): # Calculate the area of each contour area = cv.contourArea(c) # Ignore contours that are too small or too large if area < 3700 or 100000 < area: continue # cv.minAreaRect returns: # (center(x, y), (width, height), angle of rotation) = cv2.minAreaRect(c) rect = cv.minAreaRect(c) box = cv.boxPoints(rect) box = np.int0(box) # Retrieve the key parameters of the rotated bounding box center = (int(rect[0][0]),int(rect[0][1])) width = int(rect[1][0]) height = int(rect[1][1]) angle = int(rect[2]) if width < height: angle = 90 - angle else: angle = -angle label = " Rotation Angle: " + str(angle) + " degrees" textbox = cv.rectangle(img, (center[0]-35, center[1]-25), (center[0] + 295, center[1] + 10), (255,255,255), -1) cv.putText(img, label, (center[0]-50, center[1]), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,0), 1, cv.LINE_AA) cv.drawContours(img,[box],0,(0,0,255),2) cv.imshow('Output Image', img) cv.waitKey(0) cv.destroyAllWindows() # Save the output image to the current directory cv.imwrite("min_area_rec_output.jpg", img)
到此這篇關於詳解Python使用OpenCV如何確定一個物件的方向的文章就介紹到這了,更多相關Python OpenCV確定物件方向內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45