1 | initial version |
I solved problem.
#!/usr/bin/python3
#OpenCV 4.3.0, Raspberry pi3B/+, 4b/4g/8g, Thonny 3.7.3
#Date: 6th July, 2020
import numpy as np
import cv2 as cv
import sys
def main():
fn = 'bottle_eggs.png'
src = cv.imread(fn)
img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
img = cv.medianBlur(img, 11)
cimg = src.copy() # numpy function
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 79, 23, 9, 26)
counter = 0
if circles is not None: # Check if circles have been found and only then iterate over these and add them to the image
_a, b, _c = circles.shape
for i in range(b):
cv.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv.LINE_AA) # draw center of circle
counter += 1
print('total both bottle and eggs {}'.format(counter))
cv.imwrite('bottles_eggs.jpg', cimg)
cv.imshow('detected circles', cimg)
cv.imshow("source", src)
cv.waitKey(0)
print('Done')
if __name__ == '__main__':
main()
cv.destroyAllWindows()
Output:
2 | No.2 Revision |
I solved problem.
#!/usr/bin/python3
#OpenCV 4.3.0, Raspberry pi3B/+, 4b/4g/8g, Thonny 3.7.3
#Date: 6th July, 2020
import numpy as np
import cv2 as cv
import sys
def main():
fn = 'bottle_eggs.png'
src = cv.imread(fn)
img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
img = cv.medianBlur(img, 11)
cimg = src.copy() # numpy function
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 79, 23, 9, 26)
counter = 0
if circles is not None: # Check if circles have been found and only then iterate over these and add them to the image
_a, b, _c = circles.shape
for i in range(b):
cv.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv.LINE_AA) # draw center of circle
counter += 1
print('total both bottle and eggs {}'.format(counter))
cv.imwrite('bottles_eggs.jpg', cimg)
cv.imshow('detected circles', cimg)
cv.imshow("source", src)
cv.waitKey(0)
print('Done')
if __name__ == '__main__':
main()
cv.destroyAllWindows()
Output:
Output 50 circles:
3 | No.3 Revision |
I solved problem.problem. I play around with HoughCircles
value 79, 23. And medianBlur
to 11(odd number not even numbers)
#!/usr/bin/python3
#OpenCV 4.3.0, Raspberry pi3B/+, 4b/4g/8g, Thonny 3.7.3
#Date: 6th July, 2020
import numpy as np
import cv2 as cv
import sys
def main():
fn = 'bottle_eggs.png'
src = cv.imread(fn)
img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
img = cv.medianBlur(img, 11)
cimg = src.copy() # numpy function
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 79, 23, 9, 26)
counter = 0
if circles is not None: # Check if circles have been found and only then iterate over these and add them to the image
_a, b, _c = circles.shape
for i in range(b):
cv.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv.LINE_AA) # draw center of circle
counter += 1
print('total both bottle and eggs {}'.format(counter))
cv.imwrite('bottles_eggs.jpg', cimg)
cv.imshow('detected circles', cimg)
cv.imshow("source", src)
cv.waitKey(0)
print('Done')
if __name__ == '__main__':
main()
cv.destroyAllWindows()
Output 50 circles: