如何使用投影配置文件去除文档图像中的下划线?
How to remove underlines in document image using projection profile?
我正在为我名为阿姆哈拉语的本地语言开发 OCR 项目,我想检测并删除文档图像中的下划线....我有以下代码,但它只能用于单个单词图像.. ..所以谁能告诉我如何使用整个文本文档的投影配置文件来做到这一点?
class UnderLineRemoval
{
public static Bitmap removeUnderline(Bitmap bm)
{
//declarations
List<int> rowSum = new List<int>();
int count = 0;
int roi = bm.Height / 2;
Color pixelColor;
//perform horizontal projections for points below roi
for (int y = roi; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
pixelColor = bm.GetPixel(x, y);
if (pixelColor.R == 0 && pixelColor.G == 0 && pixelColor.B == 0)
count++;
}
rowSum.Add(count);
count = 0;
}
// check if the sums are greater than 70% of the image width
for (int i = 0; i < rowSum.Count; i++)
{
if (rowSum[i] > bm.Width * 0.7)
{
break;
}
roi++;
}
// return if no underline are detected
if (roi == bm.Height )
return bm;
// crop image if underline are detected
Rectangle cropRect = new Rectangle(0, 0, bm.Width, bm.Height - ((bm.Height )-(roi - 2)));
if (cropRect.Height ==0 && cropRect.Y == 0)
return bm;
bm = bm.Clone(cropRect, bm.PixelFormat);
return bm;
}
}
您可以尝试通过应用来检测文档中的水平线
Hough Line Transform
和 'removing' 通过使用文档的背景颜色(例如白色)重新绘制它们的每个像素找到的行。
使用OpenCV的HoughLines()
或HoughLinesP()
方法:http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html
Here 您可以找到一个说明如何查找水平线的示例(查看页面底部)。
编辑: 我忘了说 OpenCV 有一些 C# 包装器(例如 OpenCvSharp or Emgu CV)
你可以找到带有形态的水平线,然后将其屏蔽掉。
threshold(candidate, candidate, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
Mat horiz;
Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(candidate.cols/10, 1));
morphologyEx(candidate, horiz, cv::MORPH_OPEN, element);
bitwise_and(candidate, ~horiz, candidate);
我正在为我名为阿姆哈拉语的本地语言开发 OCR 项目,我想检测并删除文档图像中的下划线....我有以下代码,但它只能用于单个单词图像.. ..所以谁能告诉我如何使用整个文本文档的投影配置文件来做到这一点?
class UnderLineRemoval
{
public static Bitmap removeUnderline(Bitmap bm)
{
//declarations
List<int> rowSum = new List<int>();
int count = 0;
int roi = bm.Height / 2;
Color pixelColor;
//perform horizontal projections for points below roi
for (int y = roi; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
pixelColor = bm.GetPixel(x, y);
if (pixelColor.R == 0 && pixelColor.G == 0 && pixelColor.B == 0)
count++;
}
rowSum.Add(count);
count = 0;
}
// check if the sums are greater than 70% of the image width
for (int i = 0; i < rowSum.Count; i++)
{
if (rowSum[i] > bm.Width * 0.7)
{
break;
}
roi++;
}
// return if no underline are detected
if (roi == bm.Height )
return bm;
// crop image if underline are detected
Rectangle cropRect = new Rectangle(0, 0, bm.Width, bm.Height - ((bm.Height )-(roi - 2)));
if (cropRect.Height ==0 && cropRect.Y == 0)
return bm;
bm = bm.Clone(cropRect, bm.PixelFormat);
return bm;
}
}
您可以尝试通过应用来检测文档中的水平线
Hough Line Transform
和 'removing' 通过使用文档的背景颜色(例如白色)重新绘制它们的每个像素找到的行。
使用OpenCV的HoughLines()
或HoughLinesP()
方法:http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html
Here 您可以找到一个说明如何查找水平线的示例(查看页面底部)。
编辑: 我忘了说 OpenCV 有一些 C# 包装器(例如 OpenCvSharp or Emgu CV)
你可以找到带有形态的水平线,然后将其屏蔽掉。
threshold(candidate, candidate, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
Mat horiz;
Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(candidate.cols/10, 1));
morphologyEx(candidate, horiz, cv::MORPH_OPEN, element);
bitwise_and(candidate, ~horiz, candidate);