检测黑白或二值图像中字符或对象的矩形边界

发布于 2024-11-28 05:15:51 字数 157 浏览 0 评论 0原文

我正在开发一个手写识别项目。该项目的要求之一是获取图像输入,该图像仅在随机位置包含一些字符对象,首先我必须提取该字符以在下一步中进行处理。

现在我很困惑这样一个难题:如何从黑白(二进制)图像中提取一个字符,或者如何在黑白(二进制)图像中绘制字符的边界矩形?

非常感谢!

I'm developing a handwriting recognition project. one of the requirements of this project is getting an image input, this image only contains some character object in a random location, and firstly I must extract this characters to process in next step.

Now I'm confusing a hard problem like that: how to extract one character from black/white (binary)image or how to draw a bound rectangle of a character in black - white (binary) image?

Thanks very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染柒℉ 2024-12-05 05:15:51

如果您正在使用 MATLAB(我希望您是这样,因为它对于此类任务来说非常棒),我建议您查看内置函数 bwlabel() 和 Regionprops()。这些应该足以分割出所有字符并获取它们的边界框信息。

下面给出了一些示例代码:

%Read image
Im = imread('im1.jpg');

%Make binary
Im(Im < 128) = 1;
Im(Im >= 128) = 0;

%Segment out all connected regions
ImL = bwlabel(Im); 

%Get labels for all distinct regions
labels = unique(ImL);

%Remove label 0, corresponding to background
labels(labels==0) = [];

%Get bounding box for each segmentation
Character = struct('BoundingBox',zeros(1,4));
nrValidDetections = 0;
for i=1:length(labels)
    D = regionprops(ImL==labels(i));
    if D.Area > 10
        nrValidDetections = nrValidDetections + 1;
        Character(nrValidDetections).BoundingBox = D.BoundingBox;
    end
end


%Visualize results
figure(1);
imagesc(ImL);
xlim([0 200]);
for i=1:nrValidDetections
    rectangle('Position',[Character(i).BoundingBox(1) ...
                          Character(i).BoundingBox(2) ...
                          Character(i).BoundingBox(3) ...
                          Character(i).BoundingBox(4)]);

end

我在这里读取的图像是从 0-255,所以我必须对其进行阈值处理以使其成为二进制。由于 i 和 j 上方的点可能是一个问题,因此我还对构成不同区域的像素数量进行了阈值处理。

结果可以在这里看到:
https://www.sugarsync.com/pf/D775999_6750989_128710

If you are using MATLAB (which I hope you are, since it it awesome for tasks like these), I suggest you look into the built in function bwlabel() and regionprops(). These should be enough to segment out all the characters and get their bounding box information.

Some sample code is given below:

%Read image
Im = imread('im1.jpg');

%Make binary
Im(Im < 128) = 1;
Im(Im >= 128) = 0;

%Segment out all connected regions
ImL = bwlabel(Im); 

%Get labels for all distinct regions
labels = unique(ImL);

%Remove label 0, corresponding to background
labels(labels==0) = [];

%Get bounding box for each segmentation
Character = struct('BoundingBox',zeros(1,4));
nrValidDetections = 0;
for i=1:length(labels)
    D = regionprops(ImL==labels(i));
    if D.Area > 10
        nrValidDetections = nrValidDetections + 1;
        Character(nrValidDetections).BoundingBox = D.BoundingBox;
    end
end


%Visualize results
figure(1);
imagesc(ImL);
xlim([0 200]);
for i=1:nrValidDetections
    rectangle('Position',[Character(i).BoundingBox(1) ...
                          Character(i).BoundingBox(2) ...
                          Character(i).BoundingBox(3) ...
                          Character(i).BoundingBox(4)]);

end

The image I read in here are from 0-255, so I have to threshold it to make it binary. As dots above i and j can be a problem, I also threshold on the number of pixels which make up the distinct region.

The result can be seen here:
https://www.sugarsync.com/pf/D775999_6750989_128710

べ繥欢鉨o。 2024-12-05 05:15:51

在我的例子中,提取字符的更好方法是直方图的分割,我只能与您分享一些论文。

http://cut.by/j7LE8

http://cut.by/PWJf1

这也许可以帮助你

The better way to extract the character in my case was the segmentation for histogram i only can share with you some papers.

http://cut.by/j7LE8

http://cut.by/PWJf1

may be this can help you

〆凄凉。 2024-12-05 05:15:51

一个简单的选择是使用详尽的搜索,例如(假设文本为黑色,背景为白色):

  1. 从最左边的列开始,逐步遍历所有行,检查黑色像素。
  2. 当遇到第一个黑色像素时,将当前列索引保存为 left
  3. 继续遍历列,直到遇到其中没有黑色像素的列,将此列索引保存为right
  4. 现在以类似的方式遍历行,从最上面的行开始,逐步遍历该行中的每一列。
  5. 当遇到第一个黑色像素时,将当前行索引保存为top
  6. 继续遍历各行,直到找到其中没有黑色像素的行,并将这一行保存为“bottom”。

您的角色将包含在由 (left - 1, top - 1) 定义为左上角、(right, Bottom) 定义为右下角的框中角落。

One simple option is to use an exhaustive search, like (assuming text is black and background is white):

  1. Starting from the leftmost column, step through all the rows checking for a black pixel.
  2. When you encounter your first black pixel, save your current column index as left.
  3. Continue traversing the columns until you encounter a column with no black pixels in it, save this column index as right.
  4. Now traverse the rows in a similar fashion, starting from the topmost row and stepping through each column in that row.
  5. When you encounter your first black pixel, save your current row index as top.
  6. Continue traversing the rows until you find one with no black pixels in it, and save this row as `bottom.

You character will be contained within the box defined by (left - 1, top - 1) as the top-left corner and (right, bottom) as the bottom-right corner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文