opencv库中houghcircle函数中的dp参数究竟如何工作? [英] How exactly does the dp parameter in the houghcircle function in the opencv library work?

查看:614
本文介绍了opencv库中houghcircle函数中的dp参数究竟如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 .houghcircles() 功能,但我不完全了解 dp 会影响结果。



查找 http: //i.imgur.com/iGyr7TG.jpg



提前感谢。

解决方案

Verba docent,

Exempla trahunt。



-Demo



可配置的UI面板允许通过移动几个滑块来调整参数






  import sys 
import cv2
import math
import numpy
from scipy.ndimage import label
pi_4 = 4 * math.pi

def nothing_asCallback(x):
pass

def GUI_openCV_circles():
# -------------------------------------------------- --------------------------- GUI-< image>
frame = cv2.imread(openCV_CircleDetection_IMG_LASSO_AREA.JPG)
demo = frame [:800,:800 ,:]
#------------- -------------------------------------------------- ----------------- GUI-< window> -s
cv2.namedWindow(DEMO.IN,cv2.cv.CV_WINDOW_AUTOSIZE)
cv2 .namedWindow(DEMO.Canny,cv2.cv.CV_WINDOW_AUTOSIZE)
cv2.namedWindow(DEMO.Canny.Circles,cv2.cv.CV_WINDOW_AUTOSIZE)
#-------- -------------------------------------------------- ---------------------- GUI-< state> -initial-value(s)
aKeyPRESSED = None#.init

aCanny_LoTreshold = 127
aCanny_LoTreshold_PREVIOUS = -1
aCanny_HiTreshold = 255
aCanny_HiTreshold_PREVIOUS = -1

aHough_dp = 1
aHough_dp_PREVIOUS = -1
aHough_minDistance = 10
aHough_minDistance_PREVIOUS = -1
aHough_param1_aCannyHiTreshold = 255
aHough_param1_aCannyHiTreshold_PREVIOUS = -1
aHough_param2_aCentreDetectTreshold = 20
aHough_param2_aCentreDetectTreshold_PREVIOUS = -1
aHough_minRadius = 10
aHough_minRadius_PREVIOUS = -1
aHough_maxRadius = 30
aHough_maxRadius_PREVIOUS = -1
#---------------------- -------------------------------------------------- -------- GUI-< ACTOR> -s
cv2.createTrackbar(Lo_Treshold,DEMO.Canny,aCanny_LoTreshold,255,nothing_asCallback)
cv2.createTrackbar(Hi_Treshold ,DEMO.Canny,aCanny_HiTreshold,255,nothing_asCallback)

cv2.createTrackbar(dp,DEMO.Canny.Circles,aHough_dp,255,nothing_asCallback)
cv2。 createTrackbar(minDistance,DEMO.Canny.Circles,aHough_minDistance,255,nothing_asCallback)
cv2.createTrackbar(param1_HiTreshold,DEMO.Canny.Circles,aHough_param1_aCannyHiTreshold,255,nothing_asCallback)
cv2.createTrackbar(param2_CentreDetect,DEMO.Canny.Circles,aHough_param2_aCentreDetectTreshold,255,nothing_asCallback)
cv2.createTrackbar(minRadius,DEMO.Canny.Circles,aHough_minRadius,255,nothing_asCallback)
cv2.createTrackbar(maxRadius,DEMO.Canny.Circles,aHough_maxRadius,255,nothing_asCallback)

cv2.imshow(DEMO.IN,demo)#static ...
#---------------------------------------------- ---------------------------------- GUI-mainloop()
print----- -------------------------------------------------- --------------------按[ESC]退出
while(True):
#--------- -------------------------------------------------- --------------------- GUI- [ESCAPE]?
if aKeyPRESSED == 27:
break
#------------------------------ -------------------------------------------------- < vars> -DETECT-delta(s)
aCanny_LoTreshold = cv2.getTrackbarPos(Lo_Treshold,DEMO.Canny)
aCanny_HiTreshold = cv2.getTrackbarPos(Hi_Treshold,DEMO.Canny )

if(aCanny_LoTreshold!= aCanny_LoTreshold_PREVIOUS
或aCanny_HiTreshold!= aCanny_HiTreshold_PREVIOUS
):
#--------------- ----------- = FLAG
aCannyRefreshFLAG = True
#------------------------- - = RE-SYNC
aCanny_LoTreshold_PREVIOUS = aCanny_LoTreshold
aCanny_HiTreshold_PREVIOUS = aCanny_HiTreshold
else:
#-------------------- ------ = Un-FLAG
aCannyRefreshFLAG = False

aHough_dp = cv2.getTrackbarPos(dp,DEMO.Canny.Circles)
aHough_minDistance = cv2 .getTrackbarPos(minDistance,DEMO.Canny.Circles)
aHough_param1_aCannyHiTreshold = cv2.getTrackbarPos(param1_HiTreshold,DEMO.Canny.Circles)
aHough_param2_aCentreDetectTreshold = cv2.getTrackbarPos(param2_CentreDetect ,DEMO.Canny.Circles)
aHough_minRadius = cv2.getTrackbarPos(minRadius,DEMO.Canny.Circles)
aHough_maxRadius = cv2.getTrackbarPos(maxRadius,DEMO.Canny。圆)

如果(aHough_dp!= aHough_dp_PREVIOUS
或aHough_minDistance!= aHough_minDistance_PREVIOUS
或aHough_param1_aCannyHiTreshold!= aHough_param1_aCannyHiTreshold_PREVIOUS
或aHough_param2_aCentreDetectTreshold!= aHough_param2_aCentreDetectTreshold_PREVIOUS
或aHough_minRadius != aHough_minRadius_PREVIOUS
或aHough_maxRadius!= aHough_maxRadius_PREVIOUS
):
#-------------------------- = FLAG
aHoughRefreshFLAG = True
#------------------------------------- --------- = RE-SYNC
aHough_dp_PREVIOUS = aHough_dp
aHough_minDistance_PREVIOUS = aHough_minDistance
aHough_param1_aCannyHiTreshold_PREVIOUS = aHough_param1_aCannyHiTreshold
aHough_param2_aCentreDetectTreshold_PREVIOUS = aHough_param2_aCentreDetectTreshold
aHough_minRadius_PREVIOUS = aHough_minRadius
aHough_maxRadius_PREVIOUS = aHough_maxRadius
else:
#-------------------------- = Un-FLAG
aHoughRefreshFLAG = False
#------------------------------------------ -------------------------------------- REFRESH-process-pipe-line(with recent<状态> < vars> )
if(aCannyRefreshFLAG):

edges = cv2.Canny(demo,aCanny_LoTreshold,
aCanny_HiTreshold

# -------------------------------------------------- ----------------------- GUI-SHOW-Canny() - < edges> -onRefreshFLAG
cv2.imshow(DEMO.Canny ,edges)
pass

if(aCannyRefreshFLAG或aHoughRefreshFLAG):

circles = cv2.HoughCircles(edges,cv2.cv.CV_HOUGH_GRADIENT,
aHough_dp ,
aHough_minDistance,
param1 = aHough_param1_aCannyHiTreshold,
param2 = aHough_param2_aCentreDetectTreshold,
minRadius = aHough_minRadius,
maxRadius = aHough_maxRadius

# -------------------------------------------------- ---------------------------- GUI-SHOW-HoughCircles() - < edges> -onRefreshFLAG
demoWithCircles = cv2。 cvtColor(demo,cv2.COLOR_BGR2RGB)#.re-init<< src
demoWithCircles = cv2.cvtColor(demoWithCircles,cv2.COLOR_RGB2BGR)

for aCircle in circles [0]:
cv2.circle(demoWithCircles,(int(aCircle [0] ),int(aCircle [1]),
aCircle [2],
(0,255,0),
1

pass
pass
cv2.imshow(DEMO.Canny.Circles,demoWithCircles)
pass
#---------------------- -------------------------------------------------- --------< vars> -UPDATE-< state>
#ref。上面in.onRefreshFLAG重新同步部分
#------------------------------------- ------------------------------------------- GUI输入? [ESCAPE]
aKeyPRESSED = cv2.waitKey(1)& 0xFF
pass
#--------------------------------------- ------------------------------------------ GUI-< window> -s / DESTROY
cv2.destroyWindow(DEMO.IN)
cv2.destroyWindow(DEMO.Canny)
cv2.destroyWindow(DEMO.Canny.Circles)
# -------------------------------------------------- ------------------------------ GUI-< window> -s
pass

def main():
GUI_openCV_circles()
return 0

如果__name__ =='__main__':
main()


I'm trying to understand the .houghcircles() function and I'm not fully understanding how dp affects the results.

Looking up http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=houghcircles#houghcircles, it says:

dp – Inverse ratio of the accumulator resolution to the image resolution.
For example,
if dp=1 , the accumulator has the same resolution as the input image.
If dp=2 , the accumulator has half as big width and height.

Say you have a 1000x1000 image. You then set dp to 3, so the accumulator sees only 1/3 the image? Or is it something like, the image size remains the same but the number of pixels seen is less? i.e. ever 3 pixels in the original image correlate to 1 pixel that the accumulator sees, like the original image is "blurred"?

I've managed to detect accurately the number of red apples in a bowl by understanding the other parameters and then fiddling with dp.

Perhaps my understanding of the accumulator is incorrect as well since the two are correlated? From what I understand, it is the number of times a region is "hit", but I am not sure how it calculates a "hit region"

My code takes a bowl of apples, converts to HSV, grabs the reddish hues, converts it to either black or white (not greyscale format) using bitWiseOr, then do some .erode() / .dilate() to reduce noise, then .houghcircles().

My resulting image looks like this http://i.imgur.com/iGyr7TG.jpg

Thanks in advance.

解决方案

Verba docent,
Exempla trahunt.

Best seen on an interactive GUI-Demo

A configurable UI-panel allows to adjust parameter(s) by moving a few sliders


import sys
import cv2
import math
import numpy
from scipy.ndimage import label
pi_4 = 4*math.pi

def nothing_asCallback(x):
    pass

def GUI_openCV_circles():
    # --------------------------------------------------------------------------------GUI-<image>
    frame = cv2.imread(  "openCV_CircleDetection_IMG_LASSO_AREA.JPG" )
    demo  = frame[:800,:800,:]
    # --------------------------------------------------------------------------------GUI-<window>-s
    cv2.namedWindow( "DEMO.IN",             cv2.cv.CV_WINDOW_AUTOSIZE )
    cv2.namedWindow( "DEMO.Canny",          cv2.cv.CV_WINDOW_AUTOSIZE )
    cv2.namedWindow( "DEMO.Canny.Circles",  cv2.cv.CV_WINDOW_AUTOSIZE )
    # --------------------------------------------------------------------------------GUI-<state>-initial-value(s)
    aKeyPRESSED                                     = None              # .init

    aCanny_LoTreshold                               = 127
    aCanny_LoTreshold_PREVIOUS                      =  -1
    aCanny_HiTreshold                               = 255
    aCanny_HiTreshold_PREVIOUS                      =  -1

    aHough_dp                                       =   1
    aHough_dp_PREVIOUS                              =  -1
    aHough_minDistance                              =  10
    aHough_minDistance_PREVIOUS                     =  -1
    aHough_param1_aCannyHiTreshold                  = 255
    aHough_param1_aCannyHiTreshold_PREVIOUS         =  -1
    aHough_param2_aCentreDetectTreshold             =  20
    aHough_param2_aCentreDetectTreshold_PREVIOUS    =  -1
    aHough_minRadius                                =  10
    aHough_minRadius_PREVIOUS                       =  -1
    aHough_maxRadius                                =  30
    aHough_maxRadius_PREVIOUS                       =  -1
    # --------------------------------------------------------------------------------GUI-<ACTOR>-s
    cv2.createTrackbar( "Lo_Treshold",          "DEMO.Canny",          aCanny_LoTreshold,                      255, nothing_asCallback )
    cv2.createTrackbar( "Hi_Treshold",          "DEMO.Canny",          aCanny_HiTreshold,                      255, nothing_asCallback )

    cv2.createTrackbar( "dp",                   "DEMO.Canny.Circles",  aHough_dp,                              255, nothing_asCallback )
    cv2.createTrackbar( "minDistance",          "DEMO.Canny.Circles",  aHough_minDistance,                     255, nothing_asCallback )
    cv2.createTrackbar( "param1_HiTreshold",    "DEMO.Canny.Circles",  aHough_param1_aCannyHiTreshold,         255, nothing_asCallback )
    cv2.createTrackbar( "param2_CentreDetect",  "DEMO.Canny.Circles",  aHough_param2_aCentreDetectTreshold,    255, nothing_asCallback )
    cv2.createTrackbar( "minRadius",            "DEMO.Canny.Circles",  aHough_minRadius,                       255, nothing_asCallback )
    cv2.createTrackbar( "maxRadius",            "DEMO.Canny.Circles",  aHough_maxRadius,                       255, nothing_asCallback )

    cv2.imshow( "DEMO.IN",          demo )                              # static ...
    # --------------------------------------------------------------------------------GUI-mainloop()
    print " --------------------------------------------------------------------------- press [ESC] to exit "
    while( True ):
        # --------------------------------------------------------------------------------GUI-[ESCAPE]?
        if aKeyPRESSED == 27:
            break
        # --------------------------------------------------------------------------------<vars>-DETECT-delta(s)
        aCanny_LoTreshold = cv2.getTrackbarPos( "Lo_Treshold", "DEMO.Canny" )
        aCanny_HiTreshold = cv2.getTrackbarPos( "Hi_Treshold", "DEMO.Canny" )

        if (    aCanny_LoTreshold      != aCanny_LoTreshold_PREVIOUS
            or  aCanny_HiTreshold      != aCanny_HiTreshold_PREVIOUS
            ):
            # --------------------------= FLAG
            aCannyRefreshFLAG           = True
            # --------------------------= RE-SYNC
            aCanny_LoTreshold_PREVIOUS  = aCanny_LoTreshold
            aCanny_HiTreshold_PREVIOUS  = aCanny_HiTreshold
        else:
            # --------------------------= Un-FLAG
            aCannyRefreshFLAG           = False

        aHough_dp                           = cv2.getTrackbarPos( "dp",                 "DEMO.Canny.Circles" )
        aHough_minDistance                  = cv2.getTrackbarPos( "minDistance",        "DEMO.Canny.Circles" )
        aHough_param1_aCannyHiTreshold      = cv2.getTrackbarPos( "param1_HiTreshold",  "DEMO.Canny.Circles" )
        aHough_param2_aCentreDetectTreshold = cv2.getTrackbarPos( "param2_CentreDetect","DEMO.Canny.Circles" )
        aHough_minRadius                    = cv2.getTrackbarPos( "minRadius",          "DEMO.Canny.Circles" )
        aHough_maxRadius                    = cv2.getTrackbarPos( "maxRadius",          "DEMO.Canny.Circles" )

        if (    aHough_dp                            != aHough_dp_PREVIOUS
            or  aHough_minDistance                   != aHough_minDistance_PREVIOUS
            or  aHough_param1_aCannyHiTreshold       != aHough_param1_aCannyHiTreshold_PREVIOUS
            or  aHough_param2_aCentreDetectTreshold  != aHough_param2_aCentreDetectTreshold_PREVIOUS    
            or  aHough_minRadius                     != aHough_minRadius_PREVIOUS
            or  aHough_maxRadius                     != aHough_maxRadius_PREVIOUS
            ):
            # --------------------------= FLAG
            aHoughRefreshFLAG           = True                  
            # ----------------------------------------------= RE-SYNC
            aHough_dp_PREVIOUS                              =  aHough_dp                          
            aHough_minDistance_PREVIOUS                     =  aHough_minDistance                 
            aHough_param1_aCannyHiTreshold_PREVIOUS         =  aHough_param1_aCannyHiTreshold     
            aHough_param2_aCentreDetectTreshold_PREVIOUS    =  aHough_param2_aCentreDetectTreshold
            aHough_minRadius_PREVIOUS                       =  aHough_minRadius                   
            aHough_maxRadius_PREVIOUS                       =  aHough_maxRadius                   
        else:
            # --------------------------= Un-FLAG
            aHoughRefreshFLAG           = False
        # --------------------------------------------------------------------------------REFRESH-process-pipe-line ( with recent <state> <vars> )
        if ( aCannyRefreshFLAG ):

            edges   = cv2.Canny(        demo,   aCanny_LoTreshold,
                                                aCanny_HiTreshold
                                        )
            # --------------------------------------------------------------------------------GUI-SHOW-Canny()-<edges>-onRefreshFLAG
            cv2.imshow( "DEMO.Canny",   edges )
            pass

        if ( aCannyRefreshFLAG or aHoughRefreshFLAG ):

            circles = cv2.HoughCircles( edges,  cv2.cv.CV_HOUGH_GRADIENT,
                                                aHough_dp,
                                                aHough_minDistance,
                                                param1      = aHough_param1_aCannyHiTreshold,
                                                param2      = aHough_param2_aCentreDetectTreshold,
                                                minRadius   = aHough_minRadius,
                                                maxRadius   = aHough_maxRadius
                                        )
            # --------------------------------------------------------------------------------GUI-SHOW-HoughCircles()-<edges>-onRefreshFLAG
            demoWithCircles = cv2.cvtColor( demo,            cv2.COLOR_BGR2RGB )                          # .re-init <<< src
            demoWithCircles = cv2.cvtColor( demoWithCircles, cv2.COLOR_RGB2BGR )

            for aCircle in circles[0]:
                cv2.circle( demoWithCircles,    ( int( aCircle[0] ), int( aCircle[1] ) ),
                                                aCircle[2],
                                                (0,255,0),
                                                1
                            )
                pass
            pass
            cv2.imshow( "DEMO.Canny.Circles", demoWithCircles )
        pass        
        # --------------------------------------------------------------------------------<vars>-UPDATE-<state>
        # ref. above in .onRefreshFLAG RE-SYNC sections
        # --------------------------------------------------------------------------------GUI-INPUT ? [ESCAPE]
        aKeyPRESSED = cv2.waitKey(1) & 0xFF
    pass
    # --------------------------------------------------------------------------------GUI-<window>-s / DESTROY
    cv2.destroyWindow( "DEMO.IN" )
    cv2.destroyWindow( "DEMO.Canny" )
    cv2.destroyWindow( "DEMO.Canny.Circles" )
    # --------------------------------------------------------------------------------GUI-<window>-s
    pass

def main():
    GUI_openCV_circles()
    return 0

if __name__ == '__main__':
    main()

这篇关于opencv库中houghcircle函数中的dp参数究竟如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆