opencv库中houghcircle函数中的dp参数究竟是如何工作的?

opencv库中houghcircle函数中的dp参数究竟是如何工作的?,opencv,computer-vision,artificial-intelligence,Opencv,Computer Vision,Artificial Intelligence,我试图理解.houghcirles()函数,但我不完全理解dp如何影响结果 抬头看,上面写着: dp–累加器分辨率与图像的反比 分辨率。例如,如果dp=1,则累加器具有相同的分辨率 分辨率为输入图像。如果dp=2,则累加器的分辨率为 宽度和高度都很大 假设您有一个1000x1000图像。然后将dp设置为3,这样累加器只能看到图像的1/3?还是说,图像大小保持不变,但看到的像素数减少了?i、 e.原始图像中有3个像素与累加器看到的1个像素相关,就像原始图像“模糊”一样 通过了解其他参数,然后摆弄d

我试图理解
.houghcirles()
函数,但我不完全理解
dp
如何影响结果

抬头看,上面写着:

dp
–累加器分辨率与图像的反比 分辨率。
例如,如果
dp=1
,则累加器具有相同的分辨率 分辨率为输入图像。
如果
dp=2
,则累加器的分辨率为 宽度和高度都很大

假设您有一个1000x1000图像。然后将dp设置为3,这样累加器只能看到图像的1/3?还是说,图像大小保持不变,但看到的像素数减少了?i、 e.原始图像中有3个像素与累加器看到的1个像素相关,就像原始图像“模糊”一样

通过了解其他参数,然后摆弄
dp
,我成功地准确地检测到了碗中红苹果的数量

也许我对累加器的理解也不正确,因为两者是相关的?据我所知,这是一个区域被“击中”的次数,但我不确定它如何计算“击中区域”

我的代码取一碗苹果,转换成HSV,抓取红色色调,使用bitWiseOr将其转换成黑色或白色(不是灰度格式),然后执行一些
.correase()
/
.deflate()
以减少噪音,然后执行
.houghcircles()

我的结果图像如下所示

提前感谢。

讲解员,
特拉亨特范例

最好在交互式GUI演示中看到 可配置的UI面板允许通过移动几个滑块来调整参数


导入系统 进口cv2 输入数学 进口numpy 从scipy.ndimage导入标签 pi_4=4*math.pi def nothing_asCallback(x): 通过 def GUI_openCV_circles(): #---------------------------------------------------------------GUI- frame=cv2.imread(“openCV\u CircleDetection\u IMG\u LASSO\u AREA.JPG”) 演示=帧[:800,:800,:] #--------------------------------------------------------------GUI--s cv2.namedWindow(“DEMO.IN”,cv2.cv.cv\u WINDOW\u AUTOSIZE) cv2.namedWindow(“DEMO.Canny”,cv2.cv.cv\u WINDOW\u AUTOSIZE) cv2.namedWindow(“DEMO.Canny.Circles”,cv2.cv.cv\u WINDOW\u AUTOSIZE) #---------------------------------------------------------------GUI--初始值 aKeyPRESSED=None#.init aCanny_LoTreshold=127 aCanny\u LoTreshold\u PREVIOUS=-1 aCanny_HiTreshold=255 阿坎尼•希特勒霍尔德•上一任=-1 aHough_dp=1 aHough_dp_PREVIOUS=-1 A思维距离=10 aHough_minDistance_PREVIOUS=-1 aHough_param1_aCannyHiTreshold=255 aHough_param1_acannyhitreshall_PREVIOUS=-1 aHough_param2_acentredetractshold=20 aHough_param2_acentredestreshold_PREVIOUS=-1 aHough_minRadius=10 aHough\U minRadius\U PREVICE=-1 A最大半径=30 A最大半径前一个=-1 #--------------------------------------------------------------GUI--s cv2.createTrackbar(“Lo_Treshold”,“DEMO.Canny”,aCanny_LoTreshold,255,nothing\u asCallback) cv2.createTrackbar(“Hi_Treshold”,“DEMO.Canny”,aCanny_HiTreshold,255,nothing\u asCallback) cv2.createTrackbar(“dp”,“DEMO.Canny.Circles”,aHough\u dp,255,nothing\u asCallback) cv2.createTrackbar(“MindEstance”,“DEMO.Canny.Circles”,aHough_MindEstance,255,nothing_asCallback) cv2.createTrackbar(“param1_HiTreshold”,“DEMO.Canny.Circles”,aHough_param1_acannyhitreshall,255,无任何回扣) cv2.createTrackbar(“param2_CentreDetect”,“DEMO.Canny.Circles”,aHough_param2_acentredetestreshold,255,无任何回调) cv2.createTrackbar(“minRadius”,“DEMO.Canny.Circles”,aHough\u minRadius,255,nothing\u asCallback) cv2.createTrackbar(“maxRadius”,“DEMO.Canny.Circles”,aHough\u maxRadius,255,nothing\u asCallback) imshow(“DEMO.IN”,DEMO)#静态。。。 #---------------------------------------------------------------GUI-mainloop() 打印“-------------------------------------------------------------按[ESC]退出” 虽然(正确): #---------------------------------------------------------------GUI-[ESCAPE]? 如果aKeyPRESSED==27: 打破 #----------------------------------------------------------------检测增量 aCanny_LoTreshold=cv2.getTrackbarPos(“lou Treshold”,“DEMO.Canny”) aCanny_HiTreshold=cv2.getTrackbarPos(“Hi_Treshold”,“DEMO.Canny”) 如果(aCanny\u LoTreshold!=aCanny\u LoTreshold\u上一个 或者阿卡尼·希特勒霍尔德!=阿卡尼·希特勒霍尔德 ): #------------------------------=旗帜 aCannyRefreshFLAG=True #------------------------------=重新同步 aCanny\u LoTreshold\u PREVIOUS=aCanny\u LoTreshold aCanny\u HiTreshold\u PREVIOUS=aCanny\u HiTreshold 其他: #------------------------------=Un标志
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()