如何修复书页图像变暗 [英] How to repair dimmed image of book page
问题描述
我有 1000 张书页图片,它的文字像这幅画一样变暗
现在我试图修复它以更清晰地阅读,我使用此代码
private Bitmap repairImage(Bitmap bmp){颜色 cc=Color.FromArgb(255, 251, 251, 251);for (int x = 0; x 238){bmp.SetPixel(x, y, Color.White);}别的{bmp.SetPixel(x, y, Color.Black);}}}返回 bmp;}
由于图像尺寸为 1168 x 1807,修复花费了很多时间,正好循环了 2110576 个周期.
有没有其他方法可以解决这个问题?谢谢.
我能想到的最好方法是使用内置的
这是我使用的代码:
使用 System.Drawing.Imaging;..公共静态位图 ApplyGamma(位图 bmp0,浮点伽玛,浮点对比度){Bitmap bmp1 = new Bitmap(bmp0.Width, bmp0.Height);使用 (Graphics g = Graphics.FromImage(bmp1)){ColorMatrix colorMatrix = new ColorMatrix(new float[][]{新浮点[] {对比度,0, 0, 0, 0},新的浮点[] {0,contrast, 0, 0, 0},新浮点[] {0, 0, 对比度, 0, 0},新浮点[] {0, 0, 0, 1, 0},新浮点[] {0, 0, 0, 0, 1}});ImageAttributes 属性 = new ImageAttributes();attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default,ColorAdjustType.Bitmap);attributes.SetGamma(gamma, ColorAdjustType.Bitmap);g.DrawImage(bmp0, new Rectangle(0, 0, bmp0.Width, bmp0.Height),0, 0, bmp0.Width, bmp0.Height, GraphicsUnit.Pixel, 属性);}返回 bmp1;}
该函数使用两个变量和两个TrackBars
以及两个Labels
:
float gamma = 1f ;浮动对比度 = 1f;私有无效 trackBar1_Scroll(对象发送者,EventArgs e){伽玛 = 1f * trackBar1.Value/100f;label1.Text = gamma.ToString("#0.00");图片框1.Image = ApplyGamma(原始图像,伽马,对比度);}私有无效 trackBar2_Scroll(对象发送者,EventArgs e){对比度 = 1f * trackBar2.Value/1000f;label2.Text = contrast.ToString("#0.00");图片框1.Image = ApplyGamma(原始图像,伽马,对比度);}
请注意,我泄露了位图;它仅用于测试;-)
I have 1000 Image of book page, Its text is dimmed as this piece
Now I tried to repair it to be more clearly to read, I use this code
private Bitmap repairImage(Bitmap bmp)
{
Color cc=Color.FromArgb(255, 251, 251, 251);
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
if (bmp.GetPixel(x, y).R>238)
{
bmp.SetPixel(x, y, Color.White);
}
else
{
bmp.SetPixel(x, y, Color.Black);
}
}
}
return bmp;
}
Due to the image dimensions is 1168 x 1807 it took a lot of time to finish repair, it exactly loops 2110576 cycles.
Is there any another way to solve this problem? Thanks.
The best way I can think of it to use the built-in ColorMatrix
class to change the Gamma and the Contrast of the image.
Here is a result for a Gamma = 6.27
and a Contrast = +1.04
:
Here is the code I used:
using System.Drawing.Imaging;
..
public static Bitmap ApplyGamma(Bitmap bmp0, float gamma, float contrast)
{
Bitmap bmp1 = new Bitmap(bmp0.Width, bmp0.Height);
using (Graphics g = Graphics.FromImage(bmp1))
{
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {contrast, 0, 0, 0, 0},
new float[] {0,contrast, 0, 0, 0},
new float[] {0, 0, contrast, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
attributes.SetGamma(gamma, ColorAdjustType.Bitmap);
g.DrawImage(bmp0, new Rectangle(0, 0, bmp0.Width, bmp0.Height),
0, 0, bmp0.Width, bmp0.Height, GraphicsUnit.Pixel, attributes);
}
return bmp1;
}
The function uses two variables and two TrackBars
along with two Labels
:
float gamma = 1f ;
float contrast = 1f;
private void trackBar1_Scroll(object sender, EventArgs e)
{
gamma = 1f * trackBar1.Value / 100f;
label1.Text = gamma.ToString("#0.00");
pictureBox1.Image = ApplyGamma(originalImage, gamma, contrast);
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
contrast = 1f * trackBar2.Value / 1000f;
label2.Text = contrast.ToString("#0.00");
pictureBox1.Image = ApplyGamma(originalImage, gamma, contrast);
}
Note that I am leaking the Bitmaps; it is just for testing ;-)
这篇关于如何修复书页图像变暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!