opencv+python 提取國徽輪廓

簡單的說,就是最近須要作圖像處理相關的項目,以前沒關注過這個領域,忽然接觸,仍是不少細節思路不明白,中間想了不少臨時方案,解決過程,最後探索的結果是用不上,不過也有部分東西能夠留下思路做爲借鑑。python

圖像提取輪廓,慶幸找到用 Python 和 OpenCV 檢測圖片上的條形碼這篇文章,學了一些處理的思路和方法。這個也是整個處理過程的基礎。這裏簡單說一個探索過程,就是提取輪廓,目標國徽。api

opencv版本不同,用到的api仍是有區別的,這裏用到的版本是2.4.13ui

截取一張國徽照片,參考上面的文章,先作灰度,模糊,二值化,再作閉運算,以後4次腐蝕,4次膨脹,以後查找輪廓,在原圖畫一個輪廓,看起來不是很理想。code

def get_guohui2():
    #獲取國徽輪廓
    img = cv2.imread("pic/guohui0.jpg")
    
    #灰度
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #模糊
    blurred = cv2.blur(gray, (9, 9))
    #二值化
    (_, thresh) = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
    cv2.imshow("image", thresh)
    cv2.waitKey(0)
    
    #閉運算
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40, 20))
    close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

    # perform a series of erosions and dilations
    #腐蝕、膨脹
    close = cv2.erode(close, None, iterations = 4)
    close = cv2.dilate(close, None, iterations = 4)
    
    cv2.imshow("image", close)
    cv2.waitKey(0)
    
    #查找輪廓
    (contours, _) = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
    
    cv2.imshow("image", img)
    cv2.waitKey(0)

後面改了先開運算再閉運算,再調整一些參數,看起來效果好多了orm

def get_guohui2():
    #獲取國徽輪廓
    img = cv2.imread("pic/guohui0.jpg")
    
    #灰度
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #模糊
    blurred = cv2.blur(gray, (3, 3))
    #二值化
    (_, thresh) = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
    cv2.imshow("image", thresh)
    cv2.waitKey(0)
    
    #開運算
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
    open = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

    # perform a series of erosions and dilations
    #腐蝕、膨脹
    open = cv2.erode(open, None, iterations = 4)
    open = cv2.dilate(open, None, iterations = 4)
    
    cv2.imshow("image", open)
    cv2.waitKey(0)
    
    #閉運算
    close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel)
    cv2.imshow("image", close)
    cv2.waitKey(0)
    
    #查找輪廓
    (contours, _) = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
    
    cv2.imshow("image", img)
    cv2.waitKey(0)

可是還有一些缺陷,不夠完整,後面想到,多是邊緣不夠明顯,因而找了怎麼銳化邊緣,找到OpenCV圖像處理 空間域圖像加強(圖像銳化 1 基於拉普拉斯算子)。裏面都是C++的代碼,參考了一下,用了矩陣,小改就完成了。銳化後沒作模糊操做。blog

def get_guohui2():
    #獲取國徽輪廓
    img = cv2.imread("pic/guohui0.jpg")
    
    #銳化操做
    kernel = np.matrix('0 -1 0; -1 5 -1; 0 -1 0')
    dst = cv2.filter2D(img,-1,kernel)

    cv2.imshow("Result", dst)
    cv2.waitKey(0)
    
    #灰度
    gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
    
    #二值化
    (_, thresh) = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    cv2.imshow("image", thresh)
    cv2.waitKey(0)
    
    #開運算
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
    open = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

    # perform a series of erosions and dilations
    #腐蝕、膨脹
    open = cv2.erode(open, None, iterations = 4)
    open = cv2.dilate(open, None, iterations = 4)
    
    cv2.imshow("image", open)
    cv2.waitKey(0)
    
    #閉運算
    close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel)
    cv2.imshow("image", close)
    cv2.waitKey(0)
    
    #查找輪廓
    (contours, _) = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    area = cv2.contourArea(contours[0])
    print area
    
    cv2.drawContours(img, [contours[1]], -1, (0, 255, 0), 3)
    
    cv2.imshow("image", img)
    cv2.waitKey(0)

完。圖片

相關文章
相關標籤/搜索