# -*- coding: utf-8 -*- """ Created on Tue Mar 15 10:36:30 2022 @author: Kenan """ import cv2 as cv import numpy as np import math as M import os folder_path=r"U:\bwsyncshare\images_input" filenames_of_images = [f for f in os.listdir(folder_path) if f.endswith('.png')] for i, filename_of_image in enumerate(filenames_of_images): full_path_of_image = os.path.join(folder_path, filename_of_image) image_bgr = cv.imread(full_path_of_image, cv.IMREAD_COLOR) # load original image image_grayscale = cv.cvtColor(image_bgr, cv.COLOR_BGR2GRAY) image = image_bgr image_b,image_g,image_r = cv.split(image_bgr) # Split colour channels and get grayscale images # For better accuracy, binary images are used before finding contours --> apply treshold image_gray_binary = image.copy() perc = np.percentile(image_r, 95) # calculate 95 % percentile ret, tresh=cv.threshold(image_r, perc, 255, cv.THRESH_BINARY) # set pixels above specified percentile to 255, the resto to 0 cv.imshow("grayscale", image_grayscale) cv.imshow("red", image_r) params = cv.SimpleBlobDetector_Params() params.minThreshold=1 params.maxThreshold=255 params.thresholdStep=1 params.filterByColor=False params.filterByArea=True params.minArea = 300 params.filterByInertia=False params.filterByConvexity=False params.filterByCircularity=False detector = cv.SimpleBlobDetector_create(params) keypoints = detector.detect(image_grayscale) number_of_detected_leds = len(keypoints) for k in keypoints: print(k.pt[0]) # x print(k.pt[1]) #y print(k.size) #diameter area = M.pi/4*k.size**2 print(area) print('') blank = np.zeros((1, 1)) blobs = cv.drawKeypoints(image, keypoints, blank, (0, 0, 255),cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) cv.imshow("Blobs Using Area", blobs) cv.waitKey(0) cv.destroyAllWindows()