Count the number of "C" in the picture by column

for c in contours:

    # contour data (from top left)
    x1,y1,w,h = cv2.boundingRect(c)
    if w < 65 or h < 65:
        continue
    x2,y2 = x1+w,y1+h


    # percent area
    percent = 100*w*h/750000

    # if the contour is too small, ignore it
    if percent < 0.2:
            continue

    # if the contour is too large, ignore it
    elif percent > 60:
            continue

    cv2.rectangle(frame0,(int(x1),int(y1)),(int(x2),int(y2)),(0,255,0),2)


    img = frame1[y1:y2, x1:x2]

# display
cv2.namedWindow("Object Detection")
cv2.imshow("Object Detection",frame0)

# key delay and action
key = cv2.waitKey(1) & 0xFF

# esc ==  27 == quit
# q   == 113 == quit
if key in (27,113):
cv2.waitKey(0)    
cv2.destroyAllWindows()

cv2.waitKey(0)

cv2.destroyAllWindows()

How to Count the number of “C” in the picture by column?

you can use opencv_contrib/edge_drawing.py at 4.5.2 · opencv/opencv_contrib · GitHub

and modify the code to count C’s using ellipses. sorting ellipses by their centers will help to find counts on each column ( hope you will implement it )

here is a naivy starter code

#!/usr/bin/python

'''
This example illustrates how to use cv.ximgproc.EdgeDrawing class.
Usage:
    ed.py [<image_name>]
    image argument defaults to board.jpg
'''

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2 as cv
import random as rng
import sys

rng.seed(12345)

def main():
    try:
        fn = sys.argv[1]
    except IndexError:
        fn = 'd:/test/cler.jpeg'

    src = cv.imread(cv.samples.findFile(fn))
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    cv.imshow("source", src)

    esrc = src.copy()

    ed = cv.ximgproc.createEdgeDrawing()

    # Detect edges
    # you should call this before detectLines() and detectEllipses()
    ed.detectEdges(gray)
    ellipses = ed.detectEllipses()

    circles_count = 0
    #Draw detected circles and ellipses
    if ellipses is not None: # Check if circles and ellipses have been found and only then iterate over these and add them to the image
        ellipses = np.uint16(np.around(ellipses))
        for i in range(len(ellipses)):
            color = (0, 0, 255)
            if not ellipses[i][0][2] == 0: // this means detected ellipse is a circle
                circles_count = circles_count +1
                cv.ellipse(esrc, (ellipses[i][0][0], ellipses[i][0][1]), (ellipses[i][0][2]+ellipses[i][0][3],ellipses[i][0][2]+ellipses[i][0][4]),ellipses[i][0][5],0, 360, color, 1, cv.LINE_AA)
    print('estimated C count : ', circles_count/2)
    cv.imshow("detected ellipses", esrc)
    cv.waitKey(0)
    print('Done')


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

detected ellipses

1 Like

Thank you for kindness. Now i can solve the problem.
thank you