Opencv-python 找到圖像輪廓並繪制,cv2.findContours()函數,求輪廓外接矩形,cv2.boundingrect()


一、查找圖像輪廓
- opencv-python中查找圖像輪廓的API為:findContours函數 該函數接受二值圖作為參數,根據參數,可查找物體外輪廓、內外輪廓,保存輪廓點、壓縮等等...
  • 如:contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
def findContours(image, mode, method, contours=None, hierarchy=None, offset=None): # real signature unknown; restored from __doc__
    """
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
    .   @brief Finds contours in a binary image.
    .   
    .   The function retrieves contours from the binary image using the algorithm @cite Suzuki85 . The contours
    .   are a useful tool for shape analysis and object detection and recognition. See squares.cpp in the
    .   OpenCV sample directory.
    .   @note Since opencv 3.2 source image is not modified by this function.
    .   
    .   @param image Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero
    .   pixels remain 0's, so the image is treated as binary . You can use #compare, #inRange, #threshold ,
    .   #adaptiveThreshold, #Canny, and others to create a binary image out of a grayscale or color one.
    .   If mode equals to #RETR_CCOMP or #RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).
    .   @param contours Detected contours. Each contour is stored as a vector of points (e.g.
    .   std::vector<std::vector<cv::Point> >).
    .   @param hierarchy Optional output vector (e.g. std::vector<cv::Vec4i>), containing information about the image topology. It has
    .   as many elements as the number of contours. For each i-th contour contours[i], the elements
    .   hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices
    .   in contours of the next and previous contours at the same hierarchical level, the first child
    .   contour and the parent contour, respectively. If for the contour i there are no next, previous,
    .   parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
    .   @param mode Contour retrieval mode, see #RetrievalModes
    .   @param method Contour approximation method, see #ContourApproximationModes
    .   @param offset Optional offset by which every contour point is shifted. This is useful if the
    .   contours are extracted from the image ROI and then they should be analyzed in the whole image
    .   context.
    """
    pass
參數 作用
cv2.RETR_EXTERNAL 只查找外輪廓
cv2.RETR_LIST 檢測所有輪廓,保存到一個arry(鏈表)
cv2.RETR_CCOMP 建立兩個等級的輪廓(外/內),只組織兩層
cv2.RETR_TREE 檢測所有輪廓,重構嵌套輪廓全部層次
cv2.CHAIN_APPROX_NONE 存儲所有邊界點
cv2.CHAIN_APPROX_SIMPLE 壓縮垂直、水平、對角方向,只保留端點
cv2.CHAIN_APPROX_TX89_L1 使用teh-Chini近似算法
cv2.CHAIN_APPROX_TC89_KCOS 使用teh-Chini近似算法

cv2.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset ]]]]])
第一個參數是指明在哪幅圖像上繪制輪廓;
第二個參數是輪廓本身,在Python中是一個list。
第三個參數指定繪制輪廓list中的哪條輪廓,如果是-1,則繪制其中的所有輪廓。后面的參數很簡單。其中thickness表明輪廓線的寬度,如果是-1(cv2.FILLED),則為填充模式

參考文章

二、外接矩形
#找到輪廓 contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

contours多維數組,包含多個輪廓cnt

x, y, w, h = cv2.boudingrect(cnt) # 獲得外接矩形

參數說明:x,y, w, h 分別表示外接矩形的x軸和y軸的坐標,以及矩形的寬和高, cnt表示輸入的輪廓值


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM