Detect multiple circles in an image

2024/10/10 8:17:47

I am trying to detect the count of water pipes in this picture. For this, I am trying to use OpenCV and Python-based detection. The results, I am getting is a little confusing to me because the spread of circles is way too large and inaccurate.

enter image description here

The code

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())# 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)#detect circles in the image
#circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, param1=40,minRadius=10,maxRadius=35)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 8.5,70,minRadius=0,maxRadius=70)#print(len(circles[0][0]))
# ensure at least some circles were found
if circles is not None:# convert the (x, y) coordinates and radius of the circles to integerscircles = np.round(circles[0, :]).astype("int")# count = count+1   # print(count) # loop over the (x, y) coordinates and radius of the circlesfor (x, y, r) in circles:# draw the circle in the output image, then draw a rectangle# corresponding to the center of the circlecv2.circle(output, (x, y), r, (0, 255, 0), 4)cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)# show the output image# cv2.imshow("output", np.hstack([output]))cv2.imwrite('output.jpg',np.hstack([output]),[cv2.IMWRITE_JPEG_QUALITY, 70])cv2.waitKey(0)

After I run this, I do see a lot of circles detected, however, the results are complete haywire. My question is, how do I improve this detection. Which parameters are specifically needed to optimize in the HoughCircles method to achieve greater accuracy? Or, should I take the approach of annotating hundreds of similar images via bounding boxes and then train them over a full-blown CNN like Yolo to perform detection?

enter image description here

Taking the approach mentioned in answer number 2 from here Measuring the diameter pictures of holes in metal parts, photographed with telecentric, monochrome camera with opencv . I got this output. This looks close to performing a count but misses on lot of actual pipes during the brightness transformation of the image.

enter image description here

Answer

The most important parameters for your HoughCircles call are:

  1. param1: because you are using cv2.HOUGH_GRADIENT, param1 is the higher threshold for the edge detection algorithm and param1 / 2 is the lower threshold.
  2. param2: it represents the accumulator threshold, so the lower the value, the more circles will be returned.
  3. minRadius and maxRadius: the blue circles in the example have a diameter of roughly 20 pixels, so using 70 pixels for maxRadius is the reason why so many circles are being returned by the algorithm.
  4. minDist: the minimum distance between the centers of two circles.

The parameterization defined below:

circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,minDist=6,dp=1.1,param1=150,param2=15,minRadius=6,maxRadius=10)

returns:

enter image description here

https://en.xdnf.cn/q/69912.html

Related Q&A

Need guidance with FilteredSelectMultiple widget

I am sorry if it question might turn to be little broad, but since I am just learning django (and I am just hobbyist developer) I need some guidance which, I hope, will help someone like me in the futu…

Django: determine which user is deleting when using post_delete signal

I want admins to be notified when certain objects are deleted but I also want to determine which user is performing the delete.Is it possible?This is the code:# models.py # signal to notify admins whe…

Double inheritance causes metaclass conflict

I use two django packages - django-mptt (utilities for implementing Modified Preorder Tree Traversal) and django-hvad (model translation).I have a model class MenuItem and I want to it extends Translat…

Mask area outside of imported shapefile (basemap/matplotlib)

Im plotting data on a basemap of the eastern seaboard of the U. S. and Canada through Matplotlib. In addition to the base layer (a filled contour plot), I overlayed a shapefile of this focus region ato…

Python Glob.glob: a wildcard for the number of directories between the root and the destination

Okay Im having trouble not only with the problem itself but even with trying to explain my question. I have a directory tree consisting of about 7 iterations, so: rootdir/a/b/c/d/e/f/destinationdirThe …

Get datetime format from string python

In Python there are multiple DateTime parsers which can parse a date string automatically without providing the datetime format. My problem is that I dont need to cast the datetime, I only need the dat…

Generating an optimal binary search tree (Cormen)

Im reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in…

Pydub from_mp3 gives [Errno 2] No such file or directory

I find myself in front of a wall here, simply trying to load an audio file into pydub for converting it keeps on throwing a "[Errno 2] No such file or directory" error.Naturally I have spent …

Compile Python 3.6.2 on Debian Jessie segfaults on sharedmods

Im trying to compile Python 3.6.2 on a Debian Jessie box with the options./configure --prefix="/opt/python3" \ --enable-optimizations \--with-lto \ --enable-profiling \ --enable-unicode=ucs4 …

Escape space in filepath

Im trying to write a python tool that will read a logfile and process itOne thing it should do is use the paths listed in the logfile (its a logfile for a backup tool)/Volumes/Live_Jobs/Live_Jobs/*SCAN…