To build contours from a two-color (black and white) bitmap and represent each contour as a closed polygon defined by vectors, we can utilize a technique called "boundary tracing." In this implementation, we will scan the bitmap to find the boundaries of connected white pixels, and then represent these boundaries as a set of closed polygons.
Approach
- Input Bitmap: The input bitmap is represented as a 2D vector, where
0 denotes white pixels and 1 denotes black pixels.
- Contour Extraction: We will implement a simple algorithm that explores the boundaries of white regions using an 8-connectivity approach.
- Polygon Representation: Each contour found will be represented as a vector of points.
C++03 Implementation
Below is the complete implementation to achieve this functionality:
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm> // For std::find
typedef std::vector<std::vector<int> > Bitmap;
typedef std::pair<int, int> Point; // Represents a point in the polygon
typedef std::vector<Point> Polygon; // Represents a polygon
typedef std::vector<Polygon> Contours; // Collection of polygons (contours)
// Directions for 8-connectivity (N, NE, E, SE, S, SW, W, NW)
const int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
// Function to check if a position is within the bitmap and is white
bool isWhite(const Bitmap& bitmap, int x, int y) {
return (x >= 0 && x < bitmap.size() && y >= 0 && y < bitmap[0].size() && bitmap[x][y] == 0);
}
// Function to trace a contour starting from (start_x, start_y)
void traceContour(Bitmap& bitmap, int start_x, int start_y, Polygon& polygon) {
int x = start_x;
int y = start_y;
int direction = 0; // Start from the north direction
do {
polygon.push_back(Point(x, y)); // Add current point to the polygon
bitmap[x][y] = 1; // Mark the current point as visited
// Check the next pixel in the 8-connectivity (left-hand rule)
bool foundNext = false;
for (int i = 0; i < 8; i++) {
direction = (direction + 1) % 8; // Turn left
int next_x = x + dx[direction];
int next_y = y + dy[direction];
if (isWhite(bitmap, next_x, next_y)) {
x = next_x;
y = next_y;
foundNext = true;
break;
}
}
} while (foundNext && !(x == start_x && y == start_y)); // Until we return to the start point
}
// Function to find all contours in the bitmap
Contours findContours(Bitmap& bitmap) {
Contours contours;
for (int i = 0; i < bitmap.size(); i++) {
for (int j = 0; j < bitmap[i].size(); j++) {
if (isWhite(bitmap, i, j)) {
Polygon polygon;
traceContour(bitmap, i, j, polygon);
contours.push_back(polygon);
}
}
}
return contours;
}
// Function to print the output contours
void printContours(const Contours& contours) {
for (size_t i = 0; i < contours.size(); ++i) {
std::cout << "Contour " << i + 1 << ": ";
for (size_t j = 0; j < contours[i].size(); ++j) {
std::cout << "(" << contours[i][j].first << ", " << contours[i][j].second << ") ";
}
std::cout << std::endl;
}
}
// Main function for testing
int main() {
// Example bitmap (0 = WHITE, 1 = BLACK)
Bitmap bitmap = {
{1, 1, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 1},
{1, 1, 0, 1, 1, 1, 1}
};
Contours contours = findContours(bitmap);
printContours(contours);
return 0;
}
Explanation of the Code
-
Data Structures:
Bitmap: A typedef for a 2D vector representing the input bitmap where 0 indicates a white pixel and 1 indicates a black pixel.
Point: A typedef for a pair of integers representing coordinates (x, y).
Polygon: A vector of points representing a closed contour.
Contours: A vector of polygons, representing multiple contours found in the bitmap.
-
Direction Arrays:
- The arrays
dx and dy define the 8 possible movements in the neighborhood of a pixel, allowing us to explore surrounding pixels.
-
Functions:
isWhite: Checks if a given pixel is within bounds and is white.
traceContour: Uses a modified boundary tracing algorithm (following the left-hand rule) to find the contour starting from a given white pixel. It marks pixels as visited by changing their color to black (1).
findContours: Iterates through the bitmap, finds unvisited white pixels, and starts tracing contours from those points.
printContours: Outputs the resulting contours to the console.
Testing the Implementation
The provided example bitmap is defined in the main function. Running the code will find and print the contours formed by white pixels.
Output Example
For the given example bitmap, the output will display the coordinates of each contour identified.
Important Notes
- This implementation works with the assumption that contours are closed loops. If there are lines or areas that are supposed to be real polygons, they may need additional verification for connectivity and completeness.
- The bitmap structure, contour tracing, and output methods can be adjusted based on additional requirements or optimizations.