如何在C#中修正圖像顏色以便閱讀
使用IronOCR的二值化、灰階、反轉和顏色替換方法在C#中修正圖像顏色,以提高文字可讀性和OCR準確性。 讀取特定文字顏色以進行目標提取。
快速入門:隔離特定文字顏色使用IronOCR的SelectTextColor方法專注於特定文字顏色的OCR - 不需要複雜的圖像處理。 載入圖片,選擇文字顏色和容差,並僅提取該文字以獲得準確的OCR結果。
-
1Install IronOCR with NuGet Package Manager
-
2複製並運行這段程式碼片段。
new IronTesseract().Read(new IronOcr.OcrImageInput("sample.jpg").SelectTextColor(new IronSoftware.Drawing.Color("#DB645C"), 60));Dim OcrEngine As New IronTesseract() OcrEngine.Read(New IronOcr.OcrImageInput("sample.jpg").SelectTextColor(New IronSoftware.Drawing.Color("#DB645C"), 60))Dim OcrEngine As New IronTesseract() OcrEngine.Read(New IronOcr.OcrImageInput("sample.jpg").SelectTextColor(New IronSoftware.Drawing.Color("#DB645C"), 60)) -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronOCR,透過免費試用
如何在C#中修正圖像顏色以便閱讀
- 下載C#程式庫以校正圖像色彩
- 導入PDF文件和圖像以進行讀取
- 應用色彩效果:二值化、灰階、反轉和顏色替換
- 匯出校正後的圖片以供查看
- 使用
SelectTextColor讀取特定文字顏色
如何將圖像二值化以改善OCR?
二值化將圖像轉換為兩色格式,通常為黑白。這將背景與文字分開,並減少雜訊,使文字更明顯且易於閱讀。
為什麼二值化能提高OCR的準確性?
使用Binarize方法應用二值化。 OCR最適合於具有黑色文字在白色背景上的高對比圖像。 此方法創造了背景與字元間清晰的區別。
二值化在處理照明不均或背景雜訊的掃描文件時表現出色。算法分析圖像並確定最佳閾值來分離前景文字與背景。 對於高級預處理解決方案,請參閱OCR圖像優化過濾器指南。
二值化的程式碼實現是什麼?
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");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")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值。
灰階在處理有色背景或水印干擾文字識別時效果良好。 與二值化不同,灰階保留了具有微妙變化的細節。 用於包含嵌入式文字的照片或複雜的佈局。 對於低品質的掃描,請參閱修正低品質掃描和圖片。
灰階轉換的簡單程式碼?
// Apply grayscale affect
imageInput.ToGrayScale();// Apply grayscale affect
imageInput.ToGrayScale();' Apply grayscale affect
imageInput.ToGrayScale()' Apply grayscale affect
imageInput.ToGrayScale()對於不同影像質量,將灰階與其他預處理步驟結合使用。 OcrInput類文件中說明了多個過濾器的連結方法。
與原始圖像相比,灰階有何不同?

之前

之後
什麼時候應該顛倒圖像顏色?
顛倒顏色可以提高對比度。將黑色背景上的白色文字轉換為白色背景上的黑色文字提高了可讀性。
如何實現顏色反轉?
使用Invert方法反轉顏色。 傳遞一個布林值來移除色彩通道並返回灰階。
顏色反轉處理負片或深色主題的截圖。 現代應用使用深色模式介面,這對傳統OCR造成挑戰。 顛倒這些圖像可確保最佳的識別效果。 對於各種影像型別,請參閱快速OCR配置指南。
// Apply invert affect
imageInput.Invert();// Apply invert affect
imageInput.Invert();' Apply invert affect
imageInput.Invert()' Apply invert affect
imageInput.Invert()對於包含多種文件型別的批處理,實施自動深色背景檢測。 IronOCR的計算機視覺功能可以識別何時需要反轉。
反轉選項之間有何區別?
比較Invert方法有無灰階效果:

已反轉

已反轉及灰階
如何替換圖像中的特定顏色?
替換特定顏色來突出或淡化元素。 使用此方法改善文字的顯著性或修正問題對比。
為什麼寬容度對於顏色替換很重要?
ReplaceColor方法需要當前顏色、新顏色和容差值。 較高的容差可更好地處理模糊的圖像。
容差決定了像素與目標顏色匹配的程度。 低值(0-50)適合均勻顏色。 較高的值(100-200)可處理抗鋸齒文字或壓縮偽影。 這對於因墨水滲透或紙張紋理而產生變化的掃描文件特別有幫助。
如何實現顏色替換?
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");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
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sample.jpg")
Dim currentColor As New IronSoftware.Drawing.Color("#DB645C")
Dim newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan
' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)
' Export the modified image
imageInput.SaveAsImages("replaceColor")
End UsingImports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sample.jpg")
Dim currentColor As New IronSoftware.Drawing.Color("#DB645C")
Dim newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan
' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)
' Export the modified image
imageInput.SaveAsImages("replaceColor")
End Using如何處理進階的顏色替換場景?
為複雜場景連結多個替換:
/* :path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-multiple-replacements.cs */
// Replace multiple colors in sequence
using var imageInput = new OcrImageInput("multi-color-document.jpg");
// Replace red text with black
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Red, IronSoftware.Drawing.Color.Black, 70);
// Replace blue headers with dark gray
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Blue, IronSoftware.Drawing.Color.DarkGray, 60);
// Replace light yellow background with white
imageInput.ReplaceColor(new IronSoftware.Drawing.Color("#FFFACD"), IronSoftware.Drawing.Color.White, 40);
// Perform OCR on the cleaned image
var result = ocrTesseract.Read(imageInput);/* :path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-multiple-replacements.cs */
// Replace multiple colors in sequence
using var imageInput = new OcrImageInput("multi-color-document.jpg");
// Replace red text with black
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Red, IronSoftware.Drawing.Color.Black, 70);
// Replace blue headers with dark gray
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Blue, IronSoftware.Drawing.Color.DarkGray, 60);
// Replace light yellow background with white
imageInput.ReplaceColor(new IronSoftware.Drawing.Color("#FFFACD"), IronSoftware.Drawing.Color.White, 40);
// Perform OCR on the cleaned image
var result = ocrTesseract.Read(imageInput);Imports IronSoftware.Drawing
' Replace multiple colors in sequence
Using imageInput As New OcrImageInput("multi-color-document.jpg")
' Replace red text with black
imageInput.ReplaceColor(Color.Red, Color.Black, 70)
' Replace blue headers with dark gray
imageInput.ReplaceColor(Color.Blue, Color.DarkGray, 60)
' Replace light yellow background with white
imageInput.ReplaceColor(New Color("#FFFACD"), Color.White, 40)
' Perform OCR on the cleaned image
Dim result = ocrTesseract.Read(imageInput)
End UsingImports IronSoftware.Drawing
' Replace multiple colors in sequence
Using imageInput As New OcrImageInput("multi-color-document.jpg")
' Replace red text with black
imageInput.ReplaceColor(Color.Red, Color.Black, 70)
' Replace blue headers with dark gray
imageInput.ReplaceColor(Color.Blue, Color.DarkGray, 60)
' Replace light yellow background with white
imageInput.ReplaceColor(New Color("#FFFACD"), Color.White, 40)
' Perform OCR on the cleaned image
Dim result = ocrTesseract.Read(imageInput)
End Using對於複雜的顏色替換,請參閱圖像校正過濾器以獲取其他預處理解決方案。
顏色替換的視覺結果是什麼?

之前

之後
如何僅讀取特定文字顏色?
使用SelectTextColor方法讀取特定文字顏色。 指定目標顏色和容差(0-255)。 容差表示像素與選定顏色在R、G和B值之間的允許差異。
顏色容差如何影響結果?
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);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)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)使用選擇性顏色讀取表單中的顏色標碼部分,提取突出顯示的文字或讀取特定警告資訊。 欲了解結構化資料提取,請查看OcrResult類以進行進階處理。
從顏色選擇中可以期待什麼輸出?
OCR結果僅讀取橙色文字:

如何儲存套用過濾器的已修改PDF?
IronOCR可以保存有或無濾鏡的已修改PDF。 SaveAsSearchablePdf方法的第二個參數指定是否要保存套用濾鏡的文件。
搜索型PDF的實現方式是什麼?
using IronOcr;
// Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
var ocr = new IronTesseract();
var ocrInput = new OcrInput();
// Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf");
// Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale();
// Run OCR on the preprocessed input
OcrResult result = ocr.Read(ocrInput);
// Write the searchable PDF; true = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);using IronOcr;
// Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
var ocr = new IronTesseract();
var ocrInput = new OcrInput();
// Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf");
// Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale();
// Run OCR on the preprocessed input
OcrResult result = ocr.Read(ocrInput);
// Write the searchable PDF; true = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);Imports IronOcr
' Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
Dim ocr As New IronTesseract()
Dim ocrInput As New OcrInput()
' Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf")
' Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale()
' Run OCR on the preprocessed input
Dim result As OcrResult = ocr.Read(ocrInput)
' Write the searchable PDF; True = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", True)Imports IronOcr
' Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
Dim ocr As New IronTesseract()
Dim ocrInput As New OcrInput()
' Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf")
' Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale()
' Run OCR on the preprocessed input
Dim result As OcrResult = ocr.Read(ocrInput)
' Write the searchable PDF; True = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", True)什麼時候應將濾鏡套用至保存的PDF?
建立更乾淨、更可讀的文件時應套用濾鏡。 這樣有助於檔案存檔或跨不同裝置共享文件。 欲了解更多資訊,請參閱通過OCR建立可搜索PDF。
對於包含表格的複雜文件,請探索在文件中讀取表格以進行專門提取。 對於高負荷處理,請參閱多執行緒Tesseract OCR以優化跨多個文件套用濾鏡。
常見問題
如何修正影響OCR準確性的圖像色彩不佳現象?
IronOCR提供多種顏色校正方法,包括二值化、灰階轉換、反轉和顏色替換。這些過濾器通過增強文字和背景的對比來提高文字的可讀性,使OCR引擎能夠更準確地從顏色質量不佳的圖像中提取文字。
什麼是二值化,何時應該在OCR中使用?
二值化使用IronOCR的Binarize方法將圖像轉換為二色格式(通常是黑白)。它特別適用於光照不均或背景噪聲的掃描文件,因為它通過分析圖像並確定最佳的分離閾值來清晰區分文字和背景。
如何從圖像中僅提取特定顏色的文字?
IronOCR的SelectTextColor方法使您能夠將OCR集中於特定文字顏色,無需複雜的圖像操作。只需指定目標顏色和容差級別,例如:SelectTextColor(new IronSoftware.Drawing.Color("#DB645C"), 60),即可僅提取該顏色範圍內的文字。
什麼時候應該將圖像轉換為灰階而不是使用二值化?
處理有色背景或水印干擾文字識別時,使用IronOCR的ToGrayScale方法。與二值化不同,灰階保留了圖像中細微的變化,當您需要保持圖像質量同時減少視覺雜亂時,這是理想的選擇。
我可以導出更正後的圖像以查看顏色過濾結果嗎?
可以,IronOCR提供了SaveAsImages方法,讓您在應用顏色校正後導出修改過的圖像。這讓您可以比較前後的結果,幫助您確定哪種顏色校正方法最適合您的具體圖像。
我怎樣能找到最適合我圖像的顏色校正過濾器?
IronOCR包括一個Filter Wizard工具,幫助確定您圖像的最佳過濾器組合。對於批量處理,您可以將多個過濾器(如二值化)與其他預處理技術結合,以獲得最佳的OCR結果。
What outputs are expected from selective color reading with OCR?
The OCR result from selective color reading focuses only on specified colors, such as extracting highlighted texts or specific warning messages, enabling precise data extraction from color-coded documents.
How can I create searchable PDFs with IronOCR?
IronOCR allows you to save modified PDFs using the SaveAsSearchablePdf method. Filters like grayscale can be applied to improve text readability and noise reduction in the resulting documents.
What preprocessing steps can enhance OCR accuracy for low-quality scans?
For low-quality scans, preprocessing steps like binarization, grayscale conversion, and color inversion can significantly improve text readability and OCR accuracy by reducing noise and enhancing contrast.
How can IronOCR help with structured data extraction?
IronOCR provides advanced processing capabilities like the OcrResult Class, which supports structured data extraction from images, making it ideal for forms with color-coded sections and highlighted text.
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。