如何修正圖片顏色以便閱讀
修正圖像顏色包括多種技術,以提高圖像的清晰度和品質。 IronOCR 提供二值化、灰度、反轉和顏色替換方法,以提高影像中文字和內容的可讀性和美觀性,這在使用光學字元辨識 (OCR) 從影像中提取文字時尤為重要。 也可以只讀取所選文字的顏色。
開始使用IronOCR
立即在您的專案中使用IronOCR,並享受免費試用。
如何修正圖片顏色以便閱讀
- 下載 C# 程式庫來校正圖像顏色
- 導入 PDF 文件和圖像以進行閱讀
- 應用所需的顏色效果,如二值化、灰度、反轉和顏色替換
- 導出校正後的影像以供查看
- Read specific text colors using the
SelectTextColor
method
二值化圖像示例
此過程將圖像轉換為兩色格式,通常是黑色和白色。這有助於將文字與背景分離並減少噪音,使文字更加鮮明,更易於閱讀。
要對圖像應用二值化效果,請使用Binarize
方法。 由於光學字符識別(OCR)處理在對比度最高的圖像上效果最佳,特別是黑色文字在白色背景上,這種方法在使背景與字符截然不同方面具有重要意義。
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-binarize-image.cs
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Apply binarize affect
imageInput.Binarize();
// Export the modified image
imageInput.SaveAsImages("binarize.jpg");
Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Apply binarize affect
imageInput.Binarize()
' Export the modified image
imageInput.SaveAsImages("binarize.jpg")
為了方便,您可以使用SaveAsImages
方法導出修改後的圖片。 以下是二值化前後圖像的比較。

之前

後
灰階影像範例
將圖像轉換成各種灰階可以使其不那麼分散注意力,更便於閱讀。 这在原始图像中的颜色引起视觉混乱时尤其有帮助。
要將灰階效果應用於影像,請使用ToGrayScale
方法。 灰階處理涉及取 R、G 和 B 值的平均值。
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-grayscale-image.cs
// Apply grayscale affect
imageInput.ToGrayScale();
' Apply grayscale affect
imageInput.ToGrayScale()

之前

後
反轉影像範例
倒置顏色可以增強對比度。例如,將白色文字在黑色背景上轉變為黑色文字在白色背景上,可以提高可讀性。
使用Invert
方法來反轉圖片顏色。 此方法可選擇接受一個布林值,用於移除所有顏色通道並返回灰階圖像。
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-invert-image.cs
// Apply invert affect
imageInput.Invert();
' Apply invert affect
imageInput.Invert()
以下圖像展示了有和沒有灰階選項的反轉方法。

反轉

Inverted & Grayscaled
替換顏色示例
此技術允許您將圖像中的特定顏色替換為其他顏色,這可以幫助突出或淡化某些元素。 它常用於使文字更加突出或修正有問題的色彩對比。
要使用ReplaceColor
方法,請指定要替換的當前顏色以及新顏色。 方法的第三個參數對應於容差值,這也很重要。 模糊圖像中需要更高的容忍度才能達到預期的效果。
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-replace-color.cs
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
IronSoftware.Drawing.Color currentColor = new IronSoftware.Drawing.Color("#DB645C");
IronSoftware.Drawing.Color newColor = IronSoftware.Drawing.Color.DarkCyan;
// Replace color
imageInput.ReplaceColor(currentColor, newColor, 80);
// Export the modified image
imageInput.SaveAsImages("replaceColor");
Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
Private currentColor As New IronSoftware.Drawing.Color("#DB645C")
Private newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan
' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)
' Export the modified image
imageInput.SaveAsImages("replaceColor")

之前

後
讀取特定文字顏色範例
此功能旨在僅讀取指定的文字類色。 使用 SelectTextColor
方法來指定 IronOCR 要關注的顏色以及公差值。 容差值接受0-255的範圍,代表在色彩空間中每個R、G、B值與選定顏色之間允許的差異。
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-select-text-color.cs
using IronOcr;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Text color to focus on
IronSoftware.Drawing.Color focusColor = new IronSoftware.Drawing.Color("#DB645C");
// Specify which text color to read
imageInput.SelectTextColor(focusColor, 60);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output result to console
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Text color to focus on
Private focusColor As New IronSoftware.Drawing.Color("#DB645C")
' Specify which text color to read
imageInput.SelectTextColor(focusColor, 60)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output result to console
Console.WriteLine(ocrResult.Text)
以下是 OCR 的結果,僅針對橙色部分的文字進行讀取。
