Chain code is a lossless compression technique used for representing an object in images. The co-ordinates of any continuous boundary of an object can be represented as a string of numbers where each number represents a particular direction in which the next point on the connected line is present. One point is taken as the reference/starting point and on plotting the points generated from the chain, the original figure can be re-drawn.
This article describes how to generate a 8-neighbourhood chain code of a 2-D straight line. In a rectangular grid, a point can have at most 8 surrounding points as shown below. The next point on the line has to be one of these 8 surrounding points. Each direction is assigned a code. Using this code we can find out which of the surrounding point should be plotted next.
The chain codes could be generated by using conditional statements for each direction but it becomes very tedious to describe for systems having large number of directions(3-D grids can have up to 26 directions). Instead we use a hash function. The difference in X() and Y() co-ordinates of two successive points are calculated and hashed to generate the key for the chain code between the two points.
Chain code list:
The function does not generate the value 4 so a dummy value is stored there.
Input : (2, -3), (-4, 2) Output : Chain code for the straight line from (2, -3) to (-4, 2) is 333433 Input : (-7, -4), (9, 3) Output : Chain code for the straight line from (-7, -4) to (9, 3) is 0101010100101010
Chain code for the straight line from (-9, -3) to (10, 1) is 0010000100010000100
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Slope of the line parallel to the line with the given slope
- Read a file line by line in Python
- PyQtGraph - Clearing the Line in Line Graph
- PyQtGraph - Getting Name of Line in Line Graph
- PyQtGraph - Setting Symbol of Line in Line Graph
- PyQtGraph - Setting Shadow Pen of Line in Line Graph
- PyQtGraph - Setting Pen of Line in Line Graph
- PyQtGraph - Setting Alpha Value of Line in Line Graph
- PyQtGraph - Getting Data Bounds of the Line in Line Graph
- PyQtGraph - Getting Data of Line in Line Graph
- Convex Hull | Monotone chain algorithm
- Digital Image Processing Chain
- Chain of Responsibility - Python Design Patterns
- Python - Itertools.chain()
- Python - Itertools.chain.from_iterable()
- PyQt5 QSpinBox - Getting next widget in focus chain
- PyQt5 QSpinBox - Getting previous widget in focus chain
- Chain Multiple Decorators in Python
- PyQt5 QCalendarWidget - Getting next widget in focus chain
- PyQt5 QCalendarWidget - Getting previous widget in focus chain
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.