co_ords = np.column_stack(np.where(img > 0))
angle = cv2.minAreaRect(co_ords)[-1]
if angle < -45:
angle = -(90 angle)
else:
angle = -angle
co_ords = np.column_stack(np.where(img > 0))回傳
[[ 0 0 0]
[ 0 0 1]
[ 0 0 2]
...
[7014 4960 0]
[7014 4960 1]
[7014 4960 2]]
angle = cv2.minAreaRect(co_ords)[-1]
此代碼回傳錯誤
第 43 行錯誤錯誤 OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\convhull.cpp:143: error: (-215:Assertion failed) total >= 0 && (depth == CV_32F || depth == CV_32S) 在函式'cv::convexHull'
為什么錯誤顯示這條路徑D:\a\opencv-python\我沒有這樣的以及如何解決這個錯誤?
uj5u.com熱心網友回復:
minAreaRect需要二維坐標,即 [x,y] 坐標陣列,形式為array([[x,y], [x,y], [x,y]...])
你給它提供了一個陣列 3D 坐標,即 y、x 和分量(0 代表藍色,1 代表綠色,2 代表紅色)坐標。
img 是 BGR 影像。所以一個3D陣列,每個img[y,x,c]都是0..255 int。img[100,50,1]第 100 行第 50 列像素的綠色分量也是如此。
因此,如果您詢問這些組件中的哪些大于 100,您會得到您所要求的。大于 100 的 (y,x,c) 串列。
現在,根據您真正想做的事情,您可能只想在使用 minAreaRect 之前跳過該組件(并交換 x 和 y,因為,cf Christoph 的評論,minAreaRect 期待x,y坐標,而np.where給出y,x,components)
angle = cv2.minAreaRect(co_ords[:,(1,0)])[-1]
列出每個 y,x,c 的 x,y 部分img[y,x,c]>=100。這意味著[x,y]對于每個像素,紅色、綠色或藍色分量≥100。請注意[x,y],如果多個分量≥100(例如白色像素),則該串列中的某些分量可能會重復或增加三倍。它不會改變 minAreaRect 的結果,但肯定會改變計算時間。
您真正想要做的可能是決定您需要應用于np.where像素而不是組件的真實條件。
例如
co_ords = np.column_stack(np.where(img[:,:,0]>=100))[:,(1,0)]
選擇藍色分量≥100的像素。由于給 np.where 的是一個二維陣列,(與 相同的形狀img[:,:,0]),你得到二維坐標。(請注意,由于 Christoph 的評論,我們仍然需要交換 x 和 y。但是這次沒有要洗掉的組件)
或者
co_ords = np.column_stack(np.where(img.sum(axis=2)>=300))[:,(1,0)]
其中選擇平均分量≥100(灰度≥100)的坐標。
但是,無論您的標準是什么,您都需要一個像素坐標串列,不包括所關注的組件。
如果你想避免 x,y 交換,你也可以放棄它:只是你得到的角度發生了變化(90°-θ 而不是 θ)
所以你也可以,例如
co_ords = np.column_stack(np.where(img.sum(axis=2)>=300))
angle = 90 - cv2.minAreaRect(co_ords)[-1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/519707.html
