Ask Your Question
0

object counting in python open cv

asked 2020-01-25 23:34:55 -0600

can anyone tell how to count number of bottle in this image ? image description

edit retag flag offensive close merge delete

Comments

can you take a look at the faq, please, and try to improve your question ?

what have you tried, so far ?

berak gravatar imageberak ( 2020-01-26 03:16:41 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-02-02 05:18:25 -0600

supra56 gravatar image

Here is code:

#!/usr/bin/python3.7.3
#OpenCV 4.2, Raspberry pi 3/3b/34b, Buster ver
#Date: 2nd February, 2020

import numpy as np
import cv2 as cv

import sys

def main():
    fn = 'bottles.png'

    src = cv.imread(fn)
    img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    img = cv.medianBlur(img, 5)
    cimg = src.copy() # numpy function

    circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 69, 21, 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]), circles[0][i][2], (0, 0, 255), 2, cv.LINE_AA)
            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(f'counter ;', counter)
        cv.imshow("detected circles", cimg)

    cv.imshow("source", src)
    cv.waitKey(0)
    print('Done')


if __name__ == '__main__':
    main()
    cv.destroyAllWindows()

Output:

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-01-25 23:34:55 -0600

Seen: 9,399 times

Last updated: Feb 02 '20