We convert image to HSV and then determine lower and upper boundaries to create a mask using cv2.inRange()
. This step isolates the yellow objects
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 208, 94], dtype = "uint8")
upper = np.array([179, 255, 232], dtype = "uint8")
mask = cv2.inRange(image, lower, upper)
import numpy as np import cv2 image = cv2.imread('1.png') original = image.copy() image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) lower = np.array([0, 208, 94], dtype = "uint8") upper = np.array([179, 255, 232], dtype = "uint8") mask = cv2.inRange(image, lower, upper) # Find contours cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Extract contours depending on OpenCV version cnts = cnts[0] if len(cnts) == 2 else cnts[1] # Iterate through contours and filter by the number of vertices for c in cnts: perimeter = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * perimeter, True) if len(approx) > 5: cv2.drawContours(original, [c], -1, (36, 255, 12), -1) cv2.imshow('mask', mask) cv2.imshow('original', original) cv2.imwrite('mask.png', mask) cv2.imwrite('original.png', original) cv2.waitKey()
by Adrian Rosebrock on July 21, 2014,July 7, 2019 at 1:19 pm,July 8, 2017 at 1:35 am,July 13, 2018 at 5:07 am
Take a look at the function signature below:
cv2.HoughCircles(image, method, dp, minDist)
Great. Let’s jump into some code:
# import the necessary packages import numpy as np import argparse import cv2 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required = True, help = "Path to the image") args = vars(ap.parse_args())
Let’s go ahead and load the image:
# load the image, clone it for output, and then convert it to grayscale image = cv2.imread(args["image"]) output = image.copy() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Fire up a shell, and execute the following command:
$ python detect_circles.py--image images / simple.png
Let’s move on to something else:
$ python detect_circles.py--image images / soda.png
Read and display an image of round plastic chips of various colors. Besides having plenty of circles to detect, there are a few interesting things going on in this image from a circle detection point-of-view:,Notice how some chips are on top of each other and some others that are close together and almost touching each other. Overlapping object boundaries and object occlusion are usually challenging scenarios for object detection.,The circle centers seem correctly positioned and their corresponding radii seem to match well to the actual chips. But still quite a few chips were missed. Try increasing the 'Sensitivity' even more, to 0.92.,The imfindcircles function searches for circles with a range of radii. Search for circles with radii in the range of 20 to 25 pixels. Before that, it is a good practice to ask whether the objects are brighter or darker than the background. To answer that question, look at the grayscale version of this image.
rgb = imread('coloredChips.png');
imshow(rgb)
d = drawline;
pos = d.Position; diffPos = diff(pos); diameter = hypot(diffPos(1), diffPos(2))
diameter = 45.3448
gray_image = rgb2gray(rgb); imshow(gray_image)
[centers, radii] = imfindcircles(rgb, [20 25], 'ObjectPolarity', 'dark')