如何修正圖片顏色以便閱讀

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼思·賓

修正圖像顏色涉及幾種技術來改善圖像的可讀性和質量。IronOCR 提供了二值化、灰階、反色和顏色替換方法,使圖像中的文本和內容更易讀且更加美觀,這在使用 OCR 時尤其重要。 (光學字符識別) 從圖像中提取文字。僅讀取選定的文字顏色也是可行的。


C# NuGet 程式庫用于 OCR

安裝與 NuGet

Install-Package IronOcr
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 OCR

安裝與 NuGet

Install-Package IronOcr
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronOCRNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變OCR。

C# NuGet 程式庫用于 OCR nuget.org/packages/IronOcr/
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")
VB   C#

為了方便,您可以使用 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()
VB   C#
示例圖片
灰階圖像

反轉影像範例

反轉顏色可以增強對比。例如,將黑色背景上的白色文字轉換為白色背景上的黑色文字可以提高可讀性。

使用 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()
VB   C#

以下圖像展示了有和沒有灰階選項的反轉方法。

反轉 image
反轉 and grayscaled image

替換顏色範例

此技術允許您將影像中的特定顏色替換為其他顏色,這可以幫助突顯或減少某些元素的強調。它常用於使文字更加突出或修正有問題的顏色對比。

要使用 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")
VB   C#
示例圖片
替換彩色圖像

讀取特定文字顏色範例

此功能旨在僅讀取指定的文字顏色。使用 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)
VB   C#

以下是 OCR 的結果,僅針對橙色部分的文字進行讀取。

OCR結果

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。