2

This is my Input Image

I want to draw three lines horizontally from any where of the image so that it don't overlap any back color pixel, like I've drawn in the image. I'm new to python open cv, can anyone please guide me how can I do that without training any ML or DL model. As per as I've found on the internet it gives me some line detection or paragraph detection code. Like This

import cv2
from imutils import contours
import imutils

def captch_ex(file_name):
    img = cv2.imread(file_name)

    img_final = cv2.imread(file_name)
    img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY)
    image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask)
    ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY_INV)  # for black text , cv.THRESH_BINARY_INV
    '''
            line  8 to 12  : Remove noisy portion 
    '''
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,
                                                         3))  # to manipulate the orientation of dilution , large x means horizonatally dilating  more, large y means vertically dilating more
    dilated = cv2.dilate(new_img, kernel, iterations=9)  # dilate , more the iteration more the dilation


    contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # findContours returns 3 variables for getting contours



for contour in contours:
    # get rectangle bounding contour
    x, y, w, h = cv2.boundingRect(contour)

    # Don't plot small false positives that aren't text
    if w < 35 and h < 35:
        continue



    # draw rectangle around contour on original image
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 1)

    '''
    #you can crop image and send to OCR  , false detected will return no text :)
    cropped = img_final[y :y +  h , x : x + w]

    s = file_name + '/crop_' + str(index) + '.jpg' 
    cv2.imwrite(s , cropped)
    index = index + 1

    '''
# write original image with added contours to disk
cv2.imshow('captcha_result', img)
cv2.waitKey()


file_name = '2.png'
captch_ex(file_name)

Can anyone help me to achieve this image kind of result:

2

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • 1
    You could threshold so that the text is black and everything else is white. Then get the count of non-zero pixels for each row (see np.count_nonzero) and draw your lines where the count is the largest (full width of the image). – fmw42 Jan 28 '23 at 05:20
  • Can you please a provide me an example ? – Animesh Mondal Jan 28 '23 at 05:51
  • 1
    Example of what? Threshold, then use np.count_nonzero() on the rows. And you have not even provided your input image! – fmw42 Jan 28 '23 at 06:00
  • 1
    See https://stackoverflow.com/questions/68523822/draw-line-on-a-gridless-image-python-opencv/68524590#68524590 – fmw42 Jan 28 '23 at 06:07
  • Just added the input image, can you please show me an example how can I achieve my desired result. – Animesh Mondal Jan 28 '23 at 06:50
  • Just read this, https://stackoverflow.com/questions/68523822/draw-line-on-a-gridless-image-python-opencv/68524590#68524590 But how can I do this horizontally, this is exactly what I want, but I want it to be horizontally . Can you do this horizontally ? – Animesh Mondal Jan 28 '23 at 06:54
  • 1
    Threshold and reduce to a column averaging or accumulating the pixels of every row and look for values below a threshold. Those should approximately give you the vertical coordinate of the least concentration of (black) pixels. – stateMachine Jan 28 '23 at 07:28
  • Actually I'm new to Open CV in Python, Can you show me the full code example, if you don't mind. @stateMachine – Animesh Mondal Jan 28 '23 at 07:59
  • 1
    no, unfortunately we are unable to show you the full code example. it was already given by fmw42. you only need to look up each used function's documentation and understand what it was used for. please take the [tour] and review [ask] to learn how to use this site most effectively. alternatively, ChatGPT is very compliant in such matters (asking for full code example). it is on you to trust its output or verify it. – Christoph Rackwitz Jan 28 '23 at 16:13
  • Read the documentation for np.count_nonzero. Just change the axis to horizontal. Replace `np.count_nonzero(thresh, axis=0)` with `np.count_nonzero(thresh, axis=1)` – fmw42 Jan 28 '23 at 17:01

0 Answers0