# Number of Columns Detector

**URL:** <https://forum.opencv.org/t/number-of-columns-detector/7531>\
**Category:** Python\
**Tags:** contours\
**Created:** [February 11, 2022, 5:14pm UTC](https://forum.opencv.org/t/number-of-columns-detector/7531 "2022-02-11T17:14:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![walucas](https://avatars.discourse-cdn.com/v4/letter/w/f07891/32.png) [@walucas](https://forum.opencv.org/u/walucas)\
**Post date:** [February 11, 2022, 5:14pm UTC](https://forum.opencv.org/t/number-of-columns-detector/7531/1 "2022-02-11T17:14:25Z")

</div>

I am trying to develop an algorithm that will count the number of columns blocks a specific image has.  
I tried a few algorithms, will paste them here, but they are not always accurate.

I can see the contours and merge them, but not sure how to get a column itself.

Algorithm 1: [mser-contours.py · GitHub](https://gist.github.com/lorenzob/f2d79320b4767f434c5c86c985b6de15)  
Algorithm 2:

```
def textBlocks(img):

thresholded = cv2.adaptiveThreshold(
            img, 255,
            cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV,
            25,
            15
        )

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 13))
closing = cv2.morphologyEx(thresholded, cv2.MORPH_CLOSE, kernel) 

contours,im2 = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
new_countours = []
for contour in contours:
    x,y,w,h = cv2.boundingRect(contour)
    if h< 30:
        continue
    new_countours.append(contour)

for contour in new_countours: 
    convex_contour = cv2.convexHull(contour)
    area = cv2.contourArea(convex_contour)
    rect = cv2.minAreaRect(contour)
    x,y,w,h = cv2.boundingRect(contour) 
    cv2.drawContours(img, [convex_contour], -1, (33,33,33), 3)

# show_scaled("contours", img)
return img

```

`

This is the picture I have

 ![opencv_to_parse](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/0615098587b1c0decc0fa8c34de12ff8d7b75068.jpeg)
