===============================================
import cv2
import numpy as np
alpha_slider_max = 20
title_window = 'Conteggio eventi'
def on_trackbar(val):
#img = originale.copy()
#cv2.rectangle(img,(0,0),(100,100),(255,255,255),-1)
t1 = cv2.getTrackbarPos(trackbar_name, title_window)
t2 = cv2.getTrackbarPos(trackbar_name_2, title_window)
t3 = cv2.getTrackbarPos(trackbar_name_3, title_window)
par1 = cv2.getTrackbarPos(trackbar_name_4, title_window)
par2 = cv2.getTrackbarPos(trackbar_name_5, title_window)
#print(str(t1))
#print(str(t2))
#contrasto
img = originale.copy()
contrast = t3
f = 131*(contrast+127)/(127*(131-contrast))
alpha_c = f
gamma_c = 127*(1-f)
img_con = cv2.addWeighted(img,alpha_c,img,0,gamma_c)
#img_con = f*(img)+127*(1-f)
cv2.imshow("contrasto",img_con)
#img=originale.copy()
gray = cv2.cvtColor(img_con, cv2.COLOR_BGR2GRAY)
img_blur = cv2.medianBlur(gray, 5)
circles = cv2.HoughCircles(img_blur, cv2.HOUGH_GRADIENT, 1, img.shape[0]/64, param1=par1, param2=par2, minRadius=t1+1, maxRadius=t2+1)
conta = 0
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
conta=conta+1
cv2.circle(img_con, (i[0], i[1]), 2, (0, 0, 255), 2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, "{:.0f}".format(conta) ,(10,15), font, 0.5,(255,0,0),2,1)
cv2.imshow(title_window,img_con)
#print ("Numero identificazioni :"+str(conta))
#print(str(val))
global originale
originale = cv2.imread('cr39film.png', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(originale, cv2.COLOR_BGR2GRAY)
cv2.namedWindow(title_window)
trackbar_name = 'MinRadius x %d' % alpha_slider_max
trackbar_name_2 = 'MaxRadius x %d' % alpha_slider_max
trackbar_name_3 = 'Contrasto x %d' % 127
trackbar_name_4 = 'Par 1 x %d' % alpha_slider_max
trackbar_name_5 = 'Par 2 x %d' % alpha_slider_max
cv2.createTrackbar(trackbar_name_2, title_window , 5, alpha_slider_max, on_trackbar)
cv2.createTrackbar(trackbar_name, title_window , 1, alpha_slider_max, on_trackbar)
cv2.createTrackbar(trackbar_name_3, title_window , 1, 127, on_trackbar)
cv2.createTrackbar(trackbar_name_4, title_window , 1, 50, on_trackbar)
cv2.createTrackbar(trackbar_name_5, title_window , 1, 5, on_trackbar)
on_trackbar(5)
cv2.waitKey()
cv2.destroyAllWindows()