什么是“deskew”(以及为什么它重要)
Skew发生在文档在轻角度扫描或拍摄时(通常是±0~5°)。结果:文本线不是水平的,垂直边缘被粘贴,图像具有微妙的旋转。 检测滑角并旋转图像 因此,线路再次变成垂直/水平。
如何折磨你的管道
- **OCR准确度下降:**粘贴的基因阻碍分区、线路发现和字符分类;小角可以显著减少精度。
- ** 条形码无法解码:** 许多线性符号(例如,代码128/39)对旋转敏感;过度滑动会减少成功的阅读。
- Cropping & layout detection break: 页面边缘检测和桌面线探测往往采用近正方形地质。
ASPOSE.Imaging 如何修复 - 准确
Aspose.Imaging 在拉斯特图像上展示了一个 ** 一个呼叫的插图**:
RasterImage.NormalizeAngle()
自动检测滑角(内部使用)GetSkewAngle
)并在位置旋转图像。- 重量:
NormalizeAngle(bool resizeProportionally, Color backgroundColor)
选择是否要扩展管道,以保留所有内容,以及哪种背景颜色填补由旋转创建的角落。
有云和UI对手(REST和在线工具),如果您正在构建服务或原型,则会显示相同的操作。
完整例子(复印件)
此示例显示安全的预处理和坚固的折叠与Aspose.Imaging:
- 加载扫描仪(JPG/PNG/TIFF)。
- 可选转换为灰色和正常化对比,以获得更好的角度检测。
- Calls
NormalizeAngle(resizeProportionally: true, background: White)
. - 拯救清晰的图像。
- ** 奖金:** 显示如何在多页 TIFF 中分解每个页面。
- 要求 *
- .NET 8(或 6+)
- 诺基亚:
Aspose.Imaging
using System;
using System.IO;
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.ImageOptions;
class Program
{
static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: dotnet run -- <inputImageOrTiff> <outputImageOrTiff>");
return 1;
}
string inputPath = args[0];
string outputPath = args[1];
try
{
using (var image = Image.Load(inputPath))
{
// Multi-page TIFF? Deskew frame-by-frame.
if (image is TiffImage tiff)
{
foreach (var frame in tiff.Frames)
{
// --- Optional: lightweight preprocessing for better angle detection ---
// Convert to grayscale-like statistics to reduce chroma noise.
// Many real scans already are gray/bilevel; if not, Normalize() helps.
TryNormalizeForDeskew(frame);
// --- Deskew ---
// true = expand canvas to avoid cropping
// White = fill color for the new corners created by rotation
frame.NormalizeAngle(true, Aspose.Imaging.Color.White);
}
tiff.Save(outputPath); // encoder inferred from extension
}
else
{
// Single-page raster image
var raster = image as RasterImage
?? throw new InvalidOperationException("Input is not a raster image.");
TryNormalizeForDeskew(raster);
raster.NormalizeAngle(true, Aspose.Imaging.Color.White);
// Choose encoder explicitly (e.g., PNG/JPEG/TIFF). Here we mirror input extension.
image.Save(outputPath);
}
}
Console.WriteLine($"✅ Deskew complete: {Path.GetFullPath(outputPath)}");
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine("❌ " + ex.Message);
return 2;
}
}
/// <summary>
/// Minimal, safe preprocessing to stabilize skew detection.
/// Avoid heavy blurs that can smear thin text.
/// </summary>
private static void TryNormalizeForDeskew(RasterImage raster)
{
// Ensure pixels are accessible (performance hint for subsequent operations).
raster.CacheData();
// If the image has wildly varying brightness (camera shots), a light contrast
// normalization can help align text lines for skew detection. The exact set
// of helpers varies by version; keep it simple and non-destructive.
//
// Tip: If your version exposes BinarizeOtsu/AdaptiveBinarize, try them
// *after* deskew for OCR workflows to preserve thin strokes.
// Example: If available in your build, uncomment one of these:
// raster.AdjustBrightnessContrast(brightness: 0, contrast: 10); // gentle contrast pop
// raster.Grayscale(); // reduce chroma noise if present
// Leave as-is if your scans are already clean (e.g., 300 dpi monochrome).
}
}
Why NormalizeAngle
工作好
- 它在一个通话中检测到典型的扫描文本(使用基线/列表统计)和 旋转的滑角。
- 是的
resizeProportionally
选项阻止 ** 角落粘贴**,以及backgroundColor
参数控制了新曝光区域的 填色。
多页 TIFF deskew (什么观看)
- Run
NormalizeAngle
* 按框架*;TiffFrame
这是一个拉斯特页面,所以相同的API适用。 - 最后保存一次;考虑一个 无损压缩(例如,LZW/Deflate for RGB,CCITT Group 4 for bilevel)。
- 如果您计划到 OCR 以后,请保持页面在 300 dpi (或更高) 保存小格利夫。
常见的滑板裂缝 - 以及如何避免它们
- 旋转后切割*如果你旋转而不扩展管道,角落会被切断。
NormalizeAngle(true, Color.White)
以“比例”为回应。
- 旋转后切割*如果你旋转而不扩展管道,角落会被切断。
** 污背景欺骗角度探测器重噪音或格拉迪安可以破坏角度估计 做 ** 光正常化 (对比调或灰层) 放松之前,但避免强烈的泡沫,消除薄发作。
超级二元化板硬边界可以创建的基线;先分开,然后在需要时为OCR二元化。
- 按 Steep Angles 的 Barcode 扫描*如果线条代码在分解后仍然失败,请确认角度没有饱和;非常尖锐的射击可能需要初始旋转/通过金属数据(EXIF)之前。
NormalizeAngle
.
- 按 Steep Angles 的 Barcode 扫描*如果线条代码在分解后仍然失败,请确认角度没有饱和;非常尖锐的射击可能需要初始旋转/通过金属数据(EXIF)之前。
FAQ
Q:Deskiew 会改变图像大小吗?* A: * 如果你通过 resizeProportionally: true
,子长得足以保持所有内容 - 没有挖掘 - 新角落与您所选择的颜色。
问:我可以先检测角度吗?A: Deskew 通常是一次射击 NormalizeAngle
但是,如果你需要分析的角度,你可以使用相关的API测量(例如,OCR产品曝光角度计算)。
Q:什么是云/REST?A: Aspose.Imaging Cloud 显示一个 deskew
如果您正在构建一个服务而不是使用 .NET 图书馆。
Takeaways
- **Skew 伤害 OCR、条码阅读和布局分析。
- Aspose.Imaging 的 RasterImage.NormalizeAngle 提供了一个快速可靠的解决方案,一个通话,以及保护内容界限的选项。
- 将 gentle preprocessing (可选) 与 per-page deskew 为多页 TIFF 结合,以最大限度地提高准确性。
通过这些做法,您的 .NET 应用程序将产生更清晰、更可读的扫描 - 您的下流 OCR 和条形码步骤将感谢您。