Python Forum
how to detect irregular circles in python with opencv - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: how to detect irregular circles in python with opencv (/thread-26201.html)



how to detect irregular circles in python with opencv - johansigaran - Apr-24-2020

I want to create a vision system for the detection of defect in SMD capacitors, the defect is called "pinhole" and they are small holes in the surface of the chip that are generated at the time of construction. my objective is to create an algorithm that is able of detecting these holes and with this, discard the chips that have this defect

For the moment I have created two codes:

the first one converts the original image to a binary image so that I can clear the circles, the code and the result is as follows. after obtaining the image in a binary way, I don't know how to make my algorithm detect that there is a circle, according to what I have investigated with "HOUGH" it can only be applied to images in grayscale

import cv2
import numpy as np
from matplotlib import pyplot as plt


img = cv2.imread("C:/Users/Johanna Menendez/Documents/UNIR/TFM/chips/20190905_124734.jpg", 0)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
newImg = cv2.resize(img, (0,0), fx=0.50, fy=0.50)
(thresh, blackAndWhiteImage) = cv2.threshold(newImg, 127, 255, cv2.THRESH_BINARY)
numpy_vertical_concat = np.concatenate((newImg, blackAndWhiteImage), axis=1)


cv2.imshow('binary', numpy_vertical_concat)

cv2.waitKey(0)
[python]
binary
https://ibb.co/j8c3GCt

-The second is with contours detection, the problem with this is that it not only detects circles and likewise after this I don't know how to make my algorithm detect that it is a circle

[/
import cv2
import numpy as np
from matplotlib import pyplot as plt


img = cv2.imread("C:/Users/Johanna Menendez/Documents/UNIR/TFM/chips/20190905_124734.jpg", 0)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
newImg = cv2.resize(img, (0,0), fx=0.50, fy=0.50)
gray = cv2.cvtColor(newImg, cv2.COLOR_RGB2GRAY)
(thresh, blackAndWhiteImage) = cv2.threshold(gray, 130, 255, cv2.THRESH_BINARY)
(contours, hierarchy) = cv2.findContours(blackAndWhiteImage, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContours(newImg, contours, -1, (0, 255, 0), 2)
#numpy_vertical_concat = np.concatenate((newImg, img), axis=1)

cv2.imshow('binary', img)

cv2.waitKey(0)
]

[python]
contours detection
https://ibb.co/G0ZmGRk


could help me find out how to know the way for my algorithm to detect the circles please?


RE: how to detect irregular circles in python with opencv - deanhystad - Apr-24-2020

If cv2.HoughCircles only works with grayscale why not convert you image to grayscale?
gray_img = cv2.cvtColor(color_img,cv2.COLOR_GRAY2BGR)



RE: how to detect irregular circles in python with opencv - johansigaran - Apr-24-2020

because just converting it hough does not only recognize the circle that is the defect, it recognizes circles that do not exist, so I try to highlight only the defect by binarizing the image or with contour detection


RE: how to detect irregular circles in python with opencv - deanhystad - Apr-24-2020

Any way you can filter the results to determine which are which? Defects are in the range of … kind of thing? Or are there so many false positives that the results are worthless?


RE: how to detect irregular circles in python with opencv - johansigaran - May-01-2020

yes, there so many false positives that the results are worthless,for this reason i no longer use this function, I think it is because the chips are of ceramic and it has a gloss on the surface, but I cannot make it not a false positive even though it qualifies the image and converts it to grayscale