我有一个带有字母的图像,字母有黑色和蓝色两种颜色,我想读取图像中的蓝色字母。C#中的图像处理 - 一个智能解决方案?
任何人都可以建议我在C#中做到这一点的方法。荫学习GDI +,但仍然没有得到任何逻辑开发这个程序..
我试过OCRing,但与普通的同时进行文本识别的问题是,他们不认识的色差。
我只是想读的蓝字....
任何指导的高度赞赏。
我有一个带有字母的图像,字母有黑色和蓝色两种颜色,我想读取图像中的蓝色字母。C#中的图像处理 - 一个智能解决方案?
任何人都可以建议我在C#中做到这一点的方法。荫学习GDI +,但仍然没有得到任何逻辑开发这个程序..
我试过OCRing,但与普通的同时进行文本识别的问题是,他们不认识的色差。
我只是想读的蓝字....
任何指导的高度赞赏。
试试这个;),但是,这是不安全的代码。
void RedAndBlue()
{
OpenFileDialog ofd;
int imageHeight, imageWidth;
if (ofd.ShowDialog() == DialogResult.OK)
{
Image tmp = Image.FromFile(ofd.FileName);
imageHeight = tmp.Height;
imageWidth = tmp.Width;
}
else
{
// error
}
int[,] bluePixelArray = new int[imageWidth, imageHeight];
int[,] redPixelArray = new int[imageWidth, imageHeight];
Rectangle rect = new Rectangle(0, 0, tmp.Width, tmp.Height);
Bitmap temp = new Bitmap(tmp);
BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int j = 0; j < bmpData.Height; j++)
{
for (int i = 0; i < bmpData.Width; i++)
{
bluePixelArray[i, j] = ptr[0];
redPixelArray[i, j] = ptr[2];
ptr += 3;
}
ptr += remain;
}
}
temp.UnlockBits(bmpData);
temp.Dispose();
}
这是正确的方式做位图像素操作。在这种情况下,“不安全”的关键字是不幸的,因为如果你不擅长数学,它只是非常不安全。 “notaspathethicallyslowasgetpixelandsetpixel”将是一个更合适的关键字。 – MusiGenesis 2009-08-16 00:31:37
我必须警告他有关'不安全'的部分,因为他之前需要允许不安全的代码。你对“notaspathethicallyslowasgetpixelandsetpixel”:) – 2009-08-16 08:09:04
+1这个,但是你应该在最后的UnlockBits操作之后调用temp.Dispose()吗? – Andy 2009-08-16 08:44:25
你也许可以修改调色板只有黑白图像
上修改图像的颜色为灰色缩放,然后使用OCR
public Bitmap MakeGrayscale(Bitmap original)
{
//make an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
for (int i = 0; i < original.Width; i++)
{
for (int j = 0; j < original.Height; j++)
{
//get the pixel from the original image
Color originalColor = original.GetPixel(i, j);
//create the grayscale version of the pixel
int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
+ (originalColor.B * .11));
//create the color object
Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);
//set the new image's pixel to the grayscale version
newBitmap.SetPixel(i, j, newColor);
}
}
return newBitmap;
}
请问你的OCR技术工作?它只能检测白色背景上的黑色字符吗?或者还有其他一些问题? – 2009-08-15 23:19:50
为OCR荫使用Terrasact和开源OCR从谷歌,它的做工精细与大多数图像,但对于一些图像显示其拉丁语或希腊语字符,一些terrsact经验的人可能在这一领域的帮助.. – 2009-08-17 13:11:15