如何修正圖片顏色以便閱讀
修正圖像顏色涉及幾種技術來改善圖像的可讀性和質量。IronOCR 提供了二值化、灰階、反色和顏色替換方法,使圖像中的文本和內容更易讀且更加美觀,這在使用 OCR 時尤其重要。 (光學字符識別) 從圖像中提取文字。僅讀取選定的文字顏色也是可行的。
如何修正圖片顏色以便閱讀
- 下載 C# 庫來校正圖像顏色
- 導入 PDF 文件和圖像以進行閱讀
- 應用所需的顏色效果,如二值化、灰度、反轉和顏色替換
- 導出校正後的影像以供查看
- 使用該讀取特定文字顏色
選擇文字顏色
方法
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronOCR 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變OCR。
Install-Package IronOcr
請考慮安裝 IronOCR DLL 直接下載並手動安裝到您的專案或GAC表單: IronOcr.zip
手動安裝到您的項目中
下載DLL二值化圖像示例
此過程將圖像轉換為兩種顏色的格式,通常為黑色和白色。這對於將文字從背景中分離出來並減少噪點非常有用,使文字更加清晰易讀。
要將二值化效果應用於圖像,請使用 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");
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")
為了方便,您可以使用 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()
以下圖像展示了有和沒有灰階選項的反轉方法。
反轉
反轉 & 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 的結果,僅針對橙色部分的文字進行讀取。