如何修正C#中閱讀的圖片顏色
在 C# 中使用 IronOCR 的二值化、灰階、反轉和色彩置換方法修復影像色彩,以改善文字的可讀性和 OCR 的準確度。 閱讀特定文字顏色,以進行目標擷取。
快速開始:分離特定文本顏色使用IronOCR的SelectTextColor方法來將OCR焦點集中在特定的文字顏色上 - 不需要複雜的圖像操作。 載入影像、選擇文字顏色和公差,並僅抽取該文字以獲得精確的 OCR 結果。
-
1使用 IronOCR 安裝 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以優化跨多個文件的濾鏡應用。
常見問題
C# OCR 中圖像二值化的目的是什么?
二值化將圖像轉換為兩色格式,通常是黑白,以便將文本與背景分離並減少噪點。這通過創建高對比度圖像來提高文本可讀性和 OCR 準確性。
SelectTextColor 方法如何改善 OCR 結果?
SelectTextColor 方法使您可以將 OCR 聚焦於特定的文本顏色,從而在不復雜圖像操作的情況下實現目標文本提取。這在處理含有多種文本顏色的文檔時提高了準確性。
何時應在圖像預處理中使用灰度轉換?
灰度轉換對於減少圖像中分散注意力的顏色所造成的視覺混亂很有作用。它在含有干擾文本識別的彩色背景或水印的圖像中效果很好,可以保留複雜佈局中的細節。
為什麼顏色反轉對 OCR 有利?
顏色反轉通過將黑色背景上的白色文本轉換為白色背景上的黑色文本來提高對比度,從而提高可讀性。這對於深色主題的截圖或負片圖像特別有用。
容差在 ReplaceColor 方法中起什麼作用?
ReplaceColor 方法中的容差定義了像素必須與目標顏色匹配的程度以進行替換。這對於處理模糊的圖像或墨水滲透的文檔非常重要,能提高文本的突顯度和對比度。
IronOCR 如何提高 OCR 處理速度?
IronOCR 支持多執行緒處理,使得在處理大量文檔時可加快 OCR 速度。這加快了跨多個圖像應用濾鏡的速度,提高了整體效率。
選擇性顏色閱讀的 OCR 預期輸出是什麼?
選擇性顏色閱讀的 OCR 結果僅聚焦於指定顏色,例如提取高亮文本或特定的警告信息,使得能夠從顏色編碼的文檔中精確提取數據。
如何使用 IronOCR 創建可搜尋的 PDF?
IronOCR 允許您使用 SaveAsSearchablePdf 方法保存修改的 PDF。可以應用類似灰度這樣的濾鏡來提高文本的可讀性和減少文件中的噪音。
什麼樣的預處理步驟可以提升低品質掃描件的 OCR 準確性?
對於低品質的掃描件,像二值化、灰度轉換和顏色反轉這樣的預處理步驟可以通過減少噪音和提高對比度顯著提高文本的可讀性和 OCR 準確性。
IronOCR 如何有助於結構化數據提取?
IronOCR 提供了如 OcrResult Class 這樣的高級處理能力,支持從圖像中提取結構化數據,非常適合處理含有顏色編碼部分和高亮文本的表單。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。