Creating a histogram and updating the black/white points

Hi, I’ve created a histogram of an image but when i use a trackbar to change say Histsize or Lower/Upper levels it create an extra histogram rather than updating the one i have. SO, please how can i update/refresh a histogram using trackbars…?

please explain / show code.

sure he’s my code…

if name == ‘main’:
cv2.namedWindow(‘controls’)
img = cv2.imread(‘d:/trial1.png’, 0) # reads image data in greyscale
cv2.createTrackbar(‘Lower’,‘controls’, 120, 255,Hist)
cv2.createTrackbar(‘Upper’, ‘controls’,130, 255,Hist)
cv2.createTrackbar(‘HistSize’, ‘controls’, 255, 255, Hist)

while (1):
    LowerL = cv2.getTrackbarPos('Lower', 'controls')
    UpperL = cv2.getTrackbarPos('Upper', 'controls')
    HistSize = cv2.getTrackbarPos('HistSize', 'controls')

    # find frequency of pixels in range 0-255
    HistSize2=UpperL-LowerL
    histr = cv2.calcHist([img], [0], None, [HistSize2], [LowerL, UpperL])
    # show the plotting graph of an image
    plt.plot(histr)
    plt.show()
    cv2.imshow('controls', img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
       cv2.waitKey(0)
        cv2.destroyAllWindows()

i thought… that this would refresh the histogram when i change the size or the upper/lower limits…

indentation problem ?

 while (1):
        LowerL = cv2.getTrackbarPos('Lower', 'controls')
        UpperL = cv2.getTrackbarPos('Upper', 'controls')
        HistSize = cv2.getTrackbarPos('HistSize', 'controls')

        # find frequency of pixels in range 0-255
        HistSize2=UpperL-LowerL
        histr = cv2.calcHist([img], [0], None, [HistSize2], [LowerL, UpperL])
        plt.plot(histr)
        plt.show()
        cv2.imshow('controls', img)

        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break
            cv2.waitKey(0)
            cv2.destroyAllWindows()