答案 0 :(得分:0)
我的方法是这样:
有些结构可以存储已经处理过的像素。
编写例程bool belongs_to_black_text(const PixelCoordinates& coords);
,该例程将应用于图像中的每个像素。相应地替换参数,以便您处理坐标。
由于步骤6也将白色分量反转,因此我们需要处理两个图像(原始图像和结果图像),我们需要原始图像来保留白色分量。
一种改进是以某种方式在文本周围标记以前的白色组件,这样我们就不必为每个新字母重新找到该组件。
编辑:将单个白色连接的组件更改为与黑色组件相邻的所有白色像素的连接组件的并集。否则,白色成分在某些字母中可能像眼睛一样。
注意:也许更好的方法是从白色像素开始,创建连接的组件,然后找出白色组件中的每个连接的组件并反转整个物体。
编辑:添加了另一个步骤,现在是步骤6。
伪代码:
给出:
Pixel: both coordinate and content
PixelList: stores a list of Pixel
Greyscale: a pixel can be evaluated to it's greyscale value within 0..1
is black: true iff Greyscale < 0.5
is white: not is black
Image: 2D-array of Pixel, accessed via image(pixel)
minimal_size: to be too big to be assumed to be a letter
支持的算法:
connected_component(pixel, condition): creates the connected component wrt the condition, using something like BFS,
example: "find me all black pixels connected to this pixel over other black pixels"
adjacent(pixel_list, condition): all pixels that are adjacent to any pixel in the given list that fulfill the condition
Frustrum(pixel_list): finds the minimal and maximal coordinates of the list, both in x- and y-direction
算法:
instantiate 2D-array of bool that is named handled, same size as image, default value false
instantiate a copy of image named result
for each pixel in image:
if handled(pixel) continue
if pixel is white continue
PixelList black_component = connected_component(pixel, is black)
PixelList white_seed = adjacent(black_component)
PixelList white_component
for each white_pixel in white seed:
merge connected_component(white_pixel, is white) into white_component
handled(white_component) = true
handled(black_component) = true
if Frustrum(white_component) < minimal_size continue
if Frustrum(black_component) is within Frustrum(white_component)
invert white_component and black_component in result