欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python手動實現(xiàn)Hough圓變換的示例代碼

 更新時間:2022年01月17日 16:00:52   作者:aRossoneri  
Hough圓變換的原理相信大家都非常清楚了,但是手動實現(xiàn)的比較少。這篇文章將為大家介紹手動實現(xiàn)Hough圓變換的示例代碼,需要的可以了解一下

Hough圓變換的原理很多博客都已經(jīng)說得非常清楚了,但是手動實現(xiàn)的比較少,所以本文直接貼上手動實現(xiàn)的代碼。

這里使用的圖片是一堆硬幣:

 首先利用通過計算梯度來尋找邊緣,代碼如下:

def detect_edges(image):
    h = image.shape[0]
    w = image.shape[1]
    sobeling = np.zeros((h, w), np.float64)
    sobelx = [[-3, 0, 3],
              [-10, 0, 10],
              [-3, 0, 3]]
    sobelx = np.array(sobelx)
 
    sobely = [[-3, -10, -3],
              [0, 0, 0],
              [3, 10, 3]]
    sobely = np.array(sobely)
    gx = 0
    gy = 0
    testi = 0
    for i in range(1, h - 1):
        for j in range(1, w - 1):
            edgex = 0
            edgey = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    edgex += image[k + i, l + j] * sobelx[1 + k, 1 + l]
                    edgey += image[k + i, l + j] * sobely[1 + k, 1 + l]
            gx = abs(edgex)
            gy = abs(edgey)
            sobeling[i, j] = gx + gy
            # if you want to imshow ,run codes below first
            # if sobeling[i,j]>255:
            #  sobeling[i, j]=255
            # sobeling[i, j] = sobeling[i,j]/255
    return sobeling

需要注意的是,這里使用的kernel內(nèi)的數(shù)值比較大,所以得到了結(jié)果圖中的某些位置的數(shù)值超過255,但并不影響顯示,但如果想通過cv2.imshow來顯示,就需要將超過255的地方設(shè)為255即可(已經(jīng)在代碼中用注釋標(biāo)出),結(jié)果如下:

接下來就是要進(jìn)行Hough圓變換,先看代碼:

def hough_circles(edge_image, edge_thresh, radius_values):
    h = edge_image.shape[0]
    w = edge_image.shape[1]
    # print(h,w)
    edgimg = np.zeros((h, w), np.int64)
    for i in range(h):
        for j in range(w):
            if edge_image[i][j] > edge_thresh:
                edgimg[i][j] = 255
            else:
                edgimg[i][j] = 0
 
    accum_array = np.zeros((len(radius_values), h, w))
    # return edgimg , []
    for i in range(h):
        print('Hough Transform進(jìn)度:', i, '/', h)
        for j in range(w):
            if edgimg[i][j] != 0:
                for r in range(len(radius_values)):
                    rr = radius_values[r]
                    hdown = max(0, i - rr)
                    for a in range(hdown, i):
                        b = round(j+math.sqrt(rr*rr - (a - i) * (a - i)))
                        if b>=0 and b<=w-1:
                            accum_array[r][a][b] += 1
                            if 2 * i - a >= 0 and 2 * i - a <= h - 1:
                                accum_array[r][2 * i - a][b] += 1
                        if 2 * j - b >= 0 and 2 * j - b <= w - 1:
                            accum_array[r][a][2 * j - b] += 1
                        if 2 * i - a >= 0 and 2 * i - a <= h - 1 and 2 * j - b >= 0 and 2 * j - b <= w - 1:
                            accum_array[r][2 * i - a][2 * j - b] += 1
 
    return edgimg, accum_array

其中輸入是我們之前得到的邊緣圖,以及確定強(qiáng)邊緣的閾值,以及一個包含著我們估計的半徑的數(shù)組;返回值是強(qiáng)邊緣圖以及參數(shù)域矩陣。代碼中首先遍歷邊緣圖,通過閾值留下那些較強(qiáng)的位置,這里的閾值需要自己根據(jù)自己的輸入圖進(jìn)行調(diào)節(jié)。接著就是進(jìn)行Hough變換,這里的候選半徑集合需要根據(jù)自己的輸入圖進(jìn)行調(diào)節(jié)。在繪制參數(shù)域的過程中,只遍歷了所需正方形區(qū)域(大小為 r*r)的 1/4,這是因為在坐出參數(shù)域上的一個點之后,由于圓的對稱性,就可以找到與之對稱的另外三個點,無需額外進(jìn)行遍歷。

最后一步就是從參數(shù)域矩陣中提取出結(jié)果圓,代碼如下,其中篩選閾值需要根據(jù)你的輸入圖像自己調(diào)節(jié):

def find_circles(image, accum_array, radius_values, hough_thresh):
    returnlist = []
    hlist = []
    wlist = []
    rlist = []
    returnimg = deepcopy(image)
    for r in range(accum_array.shape[0]):
        print('Find Circles 進(jìn)度:', r, '/', accum_array.shape[0])
        for h in range(accum_array.shape[1]):
            for w in range(accum_array.shape[2]):
                if accum_array[r][h][w] > hough_thresh:
 
                    tmp = 0
                    for i in range(len(hlist)):
                        if abs(w-wlist[i])<10 and abs(h-hlist[i])<10:
                            tmp = 1
                            break
 
                    if tmp == 0:
                        #print(accum_array[r][h][w])
                        rr = radius_values[r]
                        flag = '(h,w,r)is:(' + str(h) + ',' + str(w) + ',' + str(rr) + ')'
                        returnlist.append(flag)
                        hlist.append(h)
                        wlist.append(w)
                        rlist.append(rr)
 
    print('圓的數(shù)量:', len(hlist))
 
    for i in range(len(hlist)):
        center = (wlist[i], hlist[i])
        rr = rlist[i]
 
        color = (0, 255, 0)
        thickness = 2
        cv2.circle(returnimg, center, rr, color, thickness)
 
    return returnlist, returnimg

注意一下在這一步中需要將那些圓心相近的圓剔除掉,只保留一個結(jié)果。

接著是main函數(shù),這沒啥好說的:

def main(argv):
    img_name = argv[0]
 
    img = cv2.imread('data/' + img_name + '.png', cv2.IMREAD_COLOR)
    # print(img.shape[0], img.shape[1])
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
    # print(gray_image.shape[0], gray_image.shape[1])
    img1 = detect_edges(gray_image)
    cv2.imwrite('output/' + img_name + "_after_find_detect.png", img1)
 
    thresh = 1500
    # 需要注意的是,在img1中有些地方的像素值是高于255的,這是由于之前的kernel內(nèi)的數(shù)更大
    # 但這并不影響圖像的顯示
    # 因此這里的thresh要大于255
    radius_values = []
    for i in range(10):
        radius_values.append(20 + i)
 
    edgeimg, accum_array = hough_circles(img1, thresh, radius_values)
    cv2.imwrite('output/' + img_name + "_after_binary.png", edgeimg)
    # Findcircle
    hough_thresh = 70
    resultlist, resultimg = find_circles(img, accum_array, radius_values, hough_thresh)
 
    print(resultlist)
    cv2.imwrite('output/' + img_name + "_circles.png", resultimg)
 
 
if __name__ == '__main__':
    sys.argv.append("coins")
    main(sys.argv[1:])
    # TODO

下面是我的運行結(jié)果:

到此這篇關(guān)于Python手動實現(xiàn)Hough圓變換的示例代碼的文章就介紹到這了,更多相關(guān)Python Hough圓變換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Numpy實現(xiàn)矩陣運算及線性代數(shù)應(yīng)用

    Numpy實現(xiàn)矩陣運算及線性代數(shù)應(yīng)用

    這篇文章主要介紹了Numpy實現(xiàn)矩陣運算及線性代數(shù)應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python使用pandas讀寫excel文件的方法實例

    python使用pandas讀寫excel文件的方法實例

    pandas是一個十分強(qiáng)大的數(shù)據(jù)處理工具,最近需要處理數(shù)據(jù)并輸入到excel,簡單列舉它的用法,這篇文章主要給大家介紹了關(guān)于python使用pandas讀寫excel文件的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Python爬取百度翻譯實現(xiàn)中英互譯功能

    Python爬取百度翻譯實現(xiàn)中英互譯功能

    這篇文章主要介紹了利用Python爬蟲爬取百度翻譯,從而實現(xiàn)中英文互譯的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-01-01
  • Python機(jī)器學(xué)習(xí)利用鳶尾花數(shù)據(jù)繪制ROC和AUC曲線

    Python機(jī)器學(xué)習(xí)利用鳶尾花數(shù)據(jù)繪制ROC和AUC曲線

    這篇文章主要為大家介紹了Python機(jī)器學(xué)習(xí)利用鳶尾花數(shù)據(jù)繪制ROC和AUC曲線實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 使用Python對mongo數(shù)據(jù)庫中字符串型正負(fù)數(shù)值比較大小

    使用Python對mongo數(shù)據(jù)庫中字符串型正負(fù)數(shù)值比較大小

    這篇文章主要介紹了使用Python對mongo數(shù)據(jù)庫中字符串型正負(fù)數(shù)值比較大小,
    2023-04-04
  • python如何使用replace做多字符替換

    python如何使用replace做多字符替換

    這篇文章主要介紹了python如何使用replace做多字符替換,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 淺析Python中的多重繼承

    淺析Python中的多重繼承

    這篇文章主要介紹了Python中的多重繼承,是Python學(xué)習(xí)中的基本知識,代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • Python?常用模塊threading和Thread模塊之線程池

    Python?常用模塊threading和Thread模塊之線程池

    這篇文章主要介紹了Python?threading和Thread模塊之線程池,線程池如消費者,負(fù)責(zé)接收任務(wù),并將任務(wù)分配到一個空閑的線程中去執(zhí)行。并不關(guān)心是哪一個線程執(zhí)行的這個任務(wù),具體介紹需要的小伙伴可以參考下面文章詳細(xì)內(nèi)容
    2022-06-06
  • 基于Python實現(xiàn)粒子濾波效果

    基于Python實現(xiàn)粒子濾波效果

    這篇文章主要介紹了基于Python實現(xiàn)粒子濾波效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • python使用form-data形式上傳文件請求的方法

    python使用form-data形式上傳文件請求的方法

    Python中的multipart/form-data是一種HTTP POST請求的數(shù)據(jù)格式,用于上傳文件或二進(jìn)制數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于python使用form-data形式上傳文件請求的相關(guān)資料,需要的朋友可以參考下
    2023-04-04

最新評論