Commit 43b9a096 authored by Davis King's avatar Davis King

Added encode_8_pixel_neighbors() and find_line_endpoints().

parent b2f6071a
......@@ -837,6 +837,90 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
template <
typename image_type
>
unsigned char encode_8_pixel_neighbors (
const const_image_view<image_type>& img,
const point& p
)
{
unsigned char ch = 0;
const rectangle area = get_rect(img);
auto check = [&](long r, long c)
{
ch <<= 1;
if (area.contains(c,r) && img[r][c])
ch |= 1;
};
long r = p.y();
long c = p.x();
check(r-1,c-1);
check(r-1,c);
check(r-1,c+1);
check(r,c+1);
check(r+1,c+1);
check(r+1,c);
check(r+1,c-1);
check(r,c-1);
return ch;
}
// ----------------------------------------------------------------------------------------
template <
typename image_type
>
std::vector<point> find_line_endpoints (
const image_type& img_
)
{
const_image_view<image_type> img(img_);
std::array<bool,256> line_ending_patterns;
line_ending_patterns.fill(false);
line_ending_patterns[0b00000001] = true;
line_ending_patterns[0b00000010] = true;
line_ending_patterns[0b00000100] = true;
line_ending_patterns[0b00001000] = true;
line_ending_patterns[0b00010000] = true;
line_ending_patterns[0b00100000] = true;
line_ending_patterns[0b01000000] = true;
line_ending_patterns[0b10000000] = true;
line_ending_patterns[0b00000011] = true;
line_ending_patterns[0b00000110] = true;
line_ending_patterns[0b00001100] = true;
line_ending_patterns[0b00011000] = true;
line_ending_patterns[0b00110000] = true;
line_ending_patterns[0b01100000] = true;
line_ending_patterns[0b11000000] = true;
line_ending_patterns[0b10000001] = true;
std::vector<point> results;
for (long r = 0; r < img.nr(); ++r)
{
for (long c = 0; c < img.nc(); ++c)
{
if (img[r][c] && line_ending_patterns[encode_8_pixel_neighbors(img,point(c,r))])
{
results.emplace_back(c,r);
}
}
}
return results;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
......
......@@ -307,6 +307,60 @@ namespace dlib
- #img.nr() == img.nr()
!*/
// ----------------------------------------------------------------------------------------
template <
typename image_type
>
unsigned char encode_8_pixel_neighbors (
const const_image_view<image_type>& img,
const point& p
);
/*!
requires
- image_type is an object that implement the interface defined in
dlib/image_processing/generic_image.h
- img must contain a grayscale pixel type.
- all pixels in img are set to either on_pixel or off_pixel.
(i.e. it must be a binary image)
- get_rect(img).contains(p) == true
ensures
- This routine looks at the 8 pixels immediately surrounding the pixel
img[p.y()][p.x()] and encodes their on/off pattern into the bits of an
unsigned char and returns it. To be specific, the neighbors are read
clockwise starting from the upper left and written to the unsigned char
starting with the high order bits. Therefore, the mapping between
neighboring pixels to bits is:
7 6 5
0 4
1 2 3
Where 0 refers to the lowest order bit in the unsigned char and 7 to the
highest order bit. Finally, a bit in the unsigned char is 1 if and only if
the corresponding pixel is on_pixel.
!*/
// ----------------------------------------------------------------------------------------
template <
typename image_type
>
std::vector<point> find_line_endpoints (
const image_type& img
);
/*!
requires
- image_type is an object that implement the interface defined in
dlib/image_processing/generic_image.h
- img must contain a grayscale pixel type.
- all pixels in img are set to either on_pixel or off_pixel.
(i.e. it must be a binary image)
ensures
- This routine finds endpoints of lines in a thinned binary image. For
example, if the image was produced by skeleton() or something like a canny
edge detector then you can use find_line_endpoints() to find the pixels
sitting on the ends of lines.
!*/
// ----------------------------------------------------------------------------------------
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment