OpenCV findContours() just returning one external contour

2024/9/8 10:34:10

I'm trying to isolate letters in a captcha, I managed to filter a captcha and that result in this black and white image:

enter image description here

But when I tried to separate the letters with findContours method of OpenCV it just found a external contour that wraps my entire image, resulting in this image (black contour outside image).

enter image description here

I'm using this code with Python 3 and OpenCV 3.4.2.17:

img = threshold_image(img)
cv2.imwrite("images/threshold.png", img)image, contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)for i, contour in enumerate(contours):area = cv2.contourArea(contour)cv2.drawContours(img, contours, i, (0, 0, 0), 3)cv2.imwrite('images/output3.png', img)

I just want my final result is 5 contours outside each character.

Answer

The contour to be extracted should be in white, background being black. I have modified your code a bit, eliminated the lines which were not adding any value.

import cv2
img = cv2.imread('image_to_be_read',0)
backup = img.copy()   #taking backup of the input image
backup = 255-backup    #colour inversion

I am using RETR_TREE as the contour retrieval mode, which retrieves all the contours and creates full family hierarchy list. Please find the documentation for the same here

_, contours, _ = cv2.findContours(backup, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

In opencv4, the finContours method has been changed. Please use:

contours, _ = cv2.findContours(backup, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

Then iterate through contours and draw the rectangle around the contours

for i, contour in enumerate(contours):x, y, w, h = cv2.boundingRect(contour)cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)

Save the image

cv2.imwrite('output3.png', img)

I got result which looks like this -

enter image description here

https://en.xdnf.cn/q/72806.html

Related Q&A

Selenium/ChromeDriver Unknown policy Errors

I am currently using Python (v3.5.1), Selenium (v3.7), and Chromedriver (v2.33).When I run the following command:from selenium import webdriver driver = webdriver.Chrome(C:\Program Files\ChromeWebdrive…

Mako escaping issue within Pyramid

I need to put javascript function to mako template. The first argument of this function is string, so I write in my *.mako file (dict(field_name=geom)):init_map(${field_name} );But when I see my html p…

All arguments should have the same length plotly

I try to do a bar graph using plotly.express but I find this problemAll arguments should have the same length. The length of argument y is 51, whereas the length of previously-processed arguments [x] …

Python curves intersection with fsolve() and function arguments using numpy

I am trying to use fsolve as quoted here : http://glowingpython.blogspot.gr/2011/05/hot-to-find-intersection-of-two.html,On order to find the intersection between two curves. Both curves basically are …

What is the Python freeze process?

The Python Doc states:Frozen modules are modules written in Python whose compiled byte-codeobject is incorporated into a custom-built Python interpreter byPython’s freeze utility. See Tools/freeze/ fo…

Is there a way to get the top k values per row of a numpy array (Python)?

Given a numpy array of the form below:x = [[4.,3.,2.,1.,8.],[1.2,3.1,0.,9.2,5.5],[0.2,7.0,4.4,0.2,1.3]]is there a way to retain the top-3 values in each row and set others to zero in python (without an…

Python with tcpdump in a subprocess: how to close subprocess properly?

I have a Python script to capture network traffic with tcpdump in a subprocess:p = subprocess.Popen([tcpdump, -I, -i, en1,-w, cap.pcap], stdout=subprocess.PIPE) time.sleep(10) p.kill()When this script …

How to install GDB with Python support on Windows 7

I need to debug cython code. Official documentation says, I need to install "gdb 7.2 or higher, built with Python support". Unfortunately I didnt find any step-by-step guide how to install it…

Pip3 is unable to install requirements.txt during docker build

I am using docker tutorial (https://docs.docker.com/language/python/build-images/) to build a simple python app. Using freeze command I made requirements.txt file which consists a lot of packages. When…

__del__ at program end

Suppose there is a program with a couple of objects living in it at runtime.Is the __del__ method of each object called when the programs ends?If yes I could for example do something like this:class C…