def __init__(self, which): self.which = which self.face_cascade = cv2.CascadeClassifier( 'haarcascade_frontalface_default.xml') self.eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') detector_params = cv2.SimpleBlobDetector_Params() detector_params.filterByArea = True detector_params.maxArea = 300 self.detector = cv2.SimpleBlobDetector_create(detector_params)
def apply_blob_detector(image): """apply the simple blob detection algorithm :param image: the image to do do blob detection on :type image: cv2 image :return: the blobs from the image :rtype: cv2 blobs """ params = cv2.SimpleBlobDetector_Params() # Change thresholds params.minThreshold = 0 params.maxThreshold = 100 params.thresholdStep = 5 # Filter by Area. params.filterByArea = True params.minArea = 300 # Filter by Circularity params.filterByCircularity = True params.minCircularity = 0.1 # Filter by Convexity params.filterByConvexity = True params.minConvexity = 0.75 # Filter by Inertia params.filterByInertia = True params.minInertiaRatio = 0.01 # Set up the detector with default parameters. detector = cv2.SimpleBlobDetector_create(params) # Detect blobs. blobs = detector.detect(image) return blobs
def main(args=None): """ Main entry point. Args: args : list A of arguments as if they were input in the command line. """ parser = get_parser() args = parser.parse_args(args) if not args.input: print( "There was no input file set! Please use --input path_to_file or use --help for more information." ) else: if not os.path.isfile(args.input): print("%s is no file!" % args.input) else: # start program # Read image im = cv2.imread(args.input, cv2.IMREAD_GRAYSCALE) # Apply threshold ret, im = cv2.threshold(im, float(args.threshold), 255, cv2.THRESH_BINARY) kernel = np.ones((6, 6), np.uint8) erosion = cv2.erode(im, kernel, iterations=1) opening = cv2.morphologyEx(im, cv2.MORPH_OPEN, kernel) im = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) # Setup SimpleBlobDetector parameters. params = cv2.SimpleBlobDetector_Params() # filter by color if args.color: params.filterByColor = True params.blobColor = int(args.color) else: params.filterByColor = False # Filter by Circularity if args.circularity: params.filterByCircularity = True params.minCircularity = float(args.circularity) else: params.filterByCircularity = False # Filter by Convexity if args.convexity: params.filterByConvexity = True params.minConvexity = float(args.convexity) else: params.filterByConvexity = False # Filter by Inertia if args.inertia: params.filterByInertia = True params.minInertiaRatio = float(args.inertia) else: params.filterByInertia = False # Filter by Size if args.min or args.max: params.filterByArea = True params.minArea = int(args.min) params.maxArea = int(args.max) else: params.filterByArea = False # Create a detector with the parameters detector = cv2.SimpleBlobDetector_create(params) # Detect blobs. keypoints = detector.detect(im) # Draw detected blobs as red circles. # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle # corresponds to the size of blob im_with_keypoints = cv2.drawKeypoints( im, keypoints, np.array([]), (0, 0, 255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # Show keypoints print("%s: %i" % (args.input, len(keypoints))) if args.output: cv2.imwrite(args.output, im_with_keypoints) if args.plot: plt.imshow(im_with_keypoints) plt.show()
def process_image(msg): try: # convert sensor_msgs/Image to OpenCV Image bridge = CvBridge() # original Image orig = bridge.imgmsg_to_cv2(msg, "bgr8") # drawImg = orig # Resized Image # Resize the Single-Channel Image resized = cv2.resize(src=orig, dsize=None, fx=0.5, fy=0.5) drawImg = resized # Convert to Single Channel gray = cv2.cvtColor(src=resized, code=cv2.COLOR_BGR2GRAY) drawImg = cv2.cvtColor(src=gray, code=cv2.COLOR_GRAY2BGR) # Applying a thresholding to the image threshVal = 150 ret, thresh = cv2.threshold(src=gray, thresh=threshVal, maxval=255, type=cv2.THRESH_BINARY) drawImg = cv2.cvtColor(src=thresh, code=cv2.COLOR_GRAY2BGR) # detect the outer pump circle pumpRadiusRange = (PUMP_DIAMETER / 2 - 2, PUMP_DIAMETER / 2 + 2) pumpCircles = cv2.HoughCircles(image=thresh, method=cv2.HOUGH_GRADIENT, dp=1, minDist=PUMP_DIAMETER, param2=2, minRadius=pumpRadiusRange[0], maxRadius=pumpRadiusRange[1]) # Plotting the circles with Error Checking plotCircles(img=drawImg, circles=pumpCircles, color=(255, 0, 0)) if (pumpCircles is None): raise Exception("No pump circles found") elif len(pumpCircles[0]) != 1: raise Exception( "Wrong # of pump circles found: {} expected {} ".format( len(pumpCircles[0]), 1)) else: pumpCircle = pumpCircles[0][0] # detect the blobs inside the pump body pistonArea = 3.14159 * PISTON_DIAMETER**2 / 4 blobParams = cv2.SimpleBlobDetector_Params() blobParams.filterByArea = True blobParams.minArea = 0.8 * pistonArea blobParams.maxArea = 1.2 * pistonArea blobDetector = cv2.SimpleBlobDetector_create(blobParams) blobs = blobDetector.detect(thresh) # OpenCV check drawImg = cv2.drawKeypoints( image=drawImg, keypoints=blobs, outImage=(), color=(0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) if len(blobs) != PISTON_COUNT: raise Exception("Wrong # of pistons: found {} expected {}".format( len(blobs), PISTON_COUNT)) # pistonCenters = [(int(b.pt[0], int(b.pt[1]))) for b in blobs] # Finally showing the size ShowImage(drawImg) except Exception as err: print(err)
from cv2 import cv2 import numpy as np # Line 8: Calculates frames, width & height # Line 9: How frames are getting compressed # Line 10: Takes compressed frames and does magic with the frames # Line 11: Init blob detector # depending if monitor is present or not, video will change out for FPS needs vid = cv2.VideoCapture( r'/Users/bchass/Documents/git/ComputerVision/videos/road.mp4') size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc(*'DIVX') video = cv2.VideoWriter(r'E:/6.avi', fourcc, 30, size) params = cv2.SimpleBlobDetector_Params() # Check fps and resolution if vid.isOpened(): FPS = vid.get(cv2.CAP_PROP_FPS) print('FPS: ', FPS) print('Resolution: ', size) while (1): ret, frame = vid.read() if not ret: break frame = cv2.convertScaleAbs(frame) params = cv2.SimpleBlobDetector_Params()
def init_blob_detector(self): detector_params = cv2.SimpleBlobDetector_Params() detector_params.filterByArea = True detector_params.maxArea = 1500 self.blob_detector = cv2.SimpleBlobDetector_create(detector_params)
import math import cv2.cv2 as cv from imgproc._utils import * from imgproc._windows import show_debug_image from tools import draw_points log = logging.getLogger(__name__) GREEN_COLOR = (0, 255, 0) RED_COLOR = (0, 0, 255) BORDER_SIZE = 10 PROCESS_PIC_SIZE = m_to_num(1) blob_detector_params = cv.SimpleBlobDetector_Params() blob_detector_params.filterByArea = False blob_detector_params.filterByCircularity = True blob_detector_params.filterByInertia = False blob_detector_params.filterByConvexity = False blob_detector_params.filterByColor = False blob_detector_params.blobColor = 255 blob_detector = cv.SimpleBlobDetector_create(blob_detector_params) multithreading = not config.debug if multithreading: executor = concurrent.futures.ThreadPoolExecutor(max_workers=2) def process(img): show_debug_image(img, 0, 0, "image")