我是計算機視覺領域的新手,我目前正在 python 上處理 0 和 1 的 numpy 陣列,如下所示:

我試圖找到由等于 1 的單元格形成的形狀的輪廓,結果應該是這樣的:

我希望能夠按照一定的順序(例如逆時針)以綠色突出顯示每個元素的位置。我嘗試按照我在網上找到的一些示例在 python 中使用 OpenCV 的findContours函式,但我沒有讓它作業:
# Import
import numpy as np
import cv2
# Find contours
tableau_poche = np.array([[1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[1., 1., 1., 1., 1., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0.]])
tableau_poche = np.int8(tableau_poche)
contours, hierarchy = cv2.findContours(tableau_poche, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
我收到以下訊息:
錯誤:OpenCV(4.0.1) C:\ci\opencv-suite_1573470242804\work\modules\imgproc\src\thresh.cpp:1492: 錯誤:(-210:不支持的格式或格式組合)在函式 'cv::臨界點'
Actually, I don't know if I am supposed to use this OpenCV function (maybe the "matplotlib.pyplot.contour()" function can solve my problem too ...) or if it's possible to use it on the numpy array I have. In a near future, I might be interested by the convexityDefects function of OpenCV on my numpy array.
uj5u.com熱心網友回復:
你有型別問題。此 OpenCV 函式僅適用于無符號整數uint8。您的陣列使用有符號整數int8。
只需替換:
tableau_poche = np.int8(tableau_poche)
通過
tableau_poche = tableau_poche.astype(np.uint8)
并且findContour()意志奏效。
正如評論中指出的那樣,您可以通過更改findContour(). 通過使用cv2.CHAIN_APPROX_NONE而不是cv2.CHAIN_APPROX_SIMPLE,它將為您提供輪廓的所有點。但是,對于距離為 1 的任何像素,它在垂直、水平和對角線上都有效。因此,它不是 100% 的問題,因為它“削減”了角落。有關選項ContourApproximationModes的更多資訊,請參閱此處的檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/450095.html
標籤:python numpy opencv image-processing computer-vision
