Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:49:22+00:00 2026-05-27T12:49:22+00:00

Just for educational purposes, I’m working on making a letter-and-symbol recognition program in Python,

  • 0

Just for educational purposes, I’m working on making a letter-and-symbol recognition program in Python, and I’ve run into some trouble with region separation. I made a working connected-component labeling function using the information here:

CCL – Wikipedia

But I need one with the accuracy of an 8-connectivity, which it mentions but doesn’t provide info for. It has a diagram on the right side that shows that to check for it, the Northwest and Northeast pixels need to be included, but I have no idea how and I can’t find any information on it. I’m not asking for code, but can anybody familiar with this method describe how to incorporate those?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:49:22+00:00Added an answer on May 27, 2026 at 12:49 pm

    8-connectivity isn’t more accurate, and in practice it’s suitable only for certain applications. It’s more common to use 4-connectivity, especially for “natural” images rather than images created in the lab for testing. An 8-connected region will include checkerboard patterns and zigzag noise. A 4-connected foreground yields an 8-connected background.

    You can dig into the source for the OpenCV function cvFindContours(). There are OpenCV bindings to Python.
    http://opencv.willowgarage.com/documentation/python/structural_analysis_and_shape_descriptors.html

    http://opencv.willowgarage.com/wiki/PythonInterface

    I would recommend first implementing a 4-connected algorithm. You can find pseudocode in books like the following:

    • Machine Vision: Theory, Algorithms, Practicalities by E. R. Davies
      In the 3rd edition, see section 6.3, “Object Labeling and Counting”
    • Digital Image Processing by Gonzalez and Woods
      See section 9.5.3 “Extraction of Connected Components”
      The presentation is less clear, but this is a standard all-in-one textbook for image processing. The section on thresholding for binarization is good. An international edition costs about $35.
    • Older textbooks may have simple, straightforward descriptions. Used copies of
      Computer Vision by Ballard and Brown are quite cheap. In that book, Algorithm 5.1 is called Blob Coloring.
    • My favorite quick description can be found in the section “Region Labeling Algorithm” of Handbook of Image and Video Processing edited by Al Bovik. Conveniently, pages 44 – 45 are available online in Google Books:
      http://books.google.com/books?id=UM_GCfJe88sC&q=region+labeling+algorithm#v=snippet&q=region%20labeling%20algorithm&f=false

    For OCR it’s common to look for dark connected regions (blobs) on a light background. Our binarized image will be a black foreground (0) on a white background (1) in a 1-bit image.

    For a 4-connected algorithm you’ll use structure elements like the ones shown below (which you’ll also see in the Bovik book). Once you’ve tinkered with 4-connectivity, the extension to 8-connectivity should be obvious.

    4-connected structure element

    We scan each row of pixels in the image from left to right, and all rows from top to bottom. For any pixel (x,y), its left neighbor (x – 1, y) and top neighbor (x, y – 1) have already been scanned, so we can check whether a region number has already been assigned to one or both of those neighbors. For example, if pixel (x, y-1) is labeled region 8, and if (x,y) is also a foreground pixel, then we assign region 8 to (x,y). If pixel (x,y) is a foreground pixel but the left and top neighbors are background pixels, we assign a new region number to (x,y).

    I recommend the Bovik reference, but here’s a quick overview of the algorithm.

    1. Initialize a region number contour (e.g. “region = 0”)
    2. Initialize a “region equivalency” data structure for later processing.
    3. Create a black and white image using a binarization threshold.
    4. Scan each pixel in the image from top to bottom, left to right.
    5. Assign region 0 to any white background (1) pixel.
    6. For any black foreground pixel (x,y) test the following conditions:
      • If top and left pixels are foreground, use the region number for (x-1, y) as the region number for (x,y), and track the equivalency of the left and top region numbers.
      • If only left neighbor (x – 1,y) is a foreground pixel, use its region number for (x,y)
      • If only top neighbor (x, y – 1) is a foreground pixel, use its region number for (x,y)
      • If left and top neighbors are background pixels, increment the region number and assign this new region number to (x,y).
    7. After completing this processing for the entire image, analyze the equivalency matrix and reduce each collection of equivalent regions to a single region.

    The reduction of equivalencies is the tricky part. In the image below, regions have been correctly labeled according to the algorithm. The image shows a different color for each region number. The three touching regions must be reduced to one connected region.

    Three adjacent regions that should be reduced to one region

    Your code should scan the equivalency data structure to reassign 2 (red) and 3 (dark blue) to the lowest-numbered region, which is 1 (yellow). Once the region number reassignment is complete, region labeling is complete.

    There are one-pass algorithms that avoid the need for an equivalency check altogether, though such algorithms are a bit harder to implement. I would recommend first implementing the traditional 4-connected algorithm, solving its problems, and then introducing an option to use 8-connectivity instead. (This option is common in image processing libraries.) Once you have 4-connected and 8-connected region labeling working you’ll have a good algorithm that will find many uses. In searching for academic papers on the subject, check for “region labeling,” “blobs,” “contours,” and “connectivity.”

    For grayscale algorithms that need to be binarized, your threshold algorithm will likely become a weak point in your chain of algorithms. For help with thresholding, get a copy of the Gonzalez and Woods book. For OCR, check out the book Character Recognition Systems by Cheriet, Karma, Liu, and Suen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple boost::any -like class for educational purposes, but I can't figure
For educational purposes I have set up a project layout like so (flat in
I'm trying to write my own C++ String class for educational and need purposes.
Background I'm working on a an educational JavaScript application/site (SPA) that will eventually have
In educational purposes I'm writing a HTTP server in C++. When receiving a request,
I'm interested in implementing a Forth system, just so I can get some experience
I'm working on authorization system for Kohana. I'm doing it just for education... This
I'm writing a lexer for the IMAP protocol for educational purposes and I'm stumped
For educational purposes, I'm writing a set of methods that cause runtime exceptions in
I am building a simple raytracer for educational purposes and want to add refraction

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.