图像简单处理
时间:2022-07-24
本文章向大家介绍图像简单处理,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
public class ImageDistinguish
{
public string imgName { get; set;}
private Bitmap bitmap { get; set; }
/// <summary>
/// 构造
/// </summary>
/// <param name="img">包含二维码的发票</param>
public ImageDistinguish(Bitmap img)
{
this.bitmap = img;
}
/// <summary>
/// 构造图像识别
/// </summary>
/// <param name="img">包含二维码的发票</param>
public ImageDistinguish(string path)
{
this.bitmap = new Bitmap(path);
this.imgName = path;
}
/// <summary>
/// 截图,只截取(0,0)到(400,400)位置的图片,包含了二维码了
/// </summary>
/// <returns></returns>
public ImageDistinguish ScreenShot()
{
Bitmap destBitmap = new Bitmap(400, 400);//目标图
Rectangle destRect = new Rectangle(0, 0, 400, 400);//矩形容器
Rectangle srcRect = new Rectangle(0, 0, 400, 400);
Graphics gh = Graphics.FromImage(destBitmap);
gh.DrawImage(this.bitmap, destRect, srcRect, GraphicsUnit.Pixel);
this.bitmap = destBitmap;
return this;
}
/// <summary>
/// 二值化处理
/// </summary>
/// <returns></returns>
public ImageDistinguish TwoValued()
{
for (int i = 0; i < this.bitmap.Width; i++)
{
for (int j = 0; j < this.bitmap.Height; j++)
{
Color pixelColor = this.bitmap.GetPixel(i, j);
if (pixelColor.R < 127.5 || pixelColor.G < 127.5 && pixelColor.B < 127.5)
{
this.bitmap.SetPixel(i, j, Color.FromArgb(0, 0, 0));
}
else
{
this.bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
}
}
return this;
}
public ImageDistinguish TwoValued(int Level)
{
if (Level < 0)
{
Level = 0;
}
if (Level > 10)
{
Level = 10;
}
for (int i = 0; i < this.bitmap.Width; i++)
{
for (int j = 0; j < this.bitmap.Height; j++)
{
Color pixelColor = this.bitmap.GetPixel(i, j);
if (pixelColor.R < Level*20 || pixelColor.G < Level * 20 && pixelColor.B < Level * 20)
{
this.bitmap.SetPixel(i, j, Color.FromArgb(0, 0, 0));
}
else
{
this.bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
}
}
return this;
}
/// <summary>
/// 柔化处理
/// </summary>
/// <returns></returns>
public ImageDistinguish SoftenImage()
{
int height = this.bitmap.Height;
int width = this.bitmap.Width;
Bitmap newbmp = new Bitmap(width, height);
LockBitmap lbmp = new LockBitmap(this.bitmap);
LockBitmap newlbmp = new LockBitmap(newbmp);
lbmp.LockBits();
newlbmp.LockBits();
Color pixel;
//高斯模板
int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
for (int x = 1; x < width - 1; x++)
{
for (int y = 1; y < height - 1; y++)
{
int r = 0, g = 0, b = 0;
int Index = 0;
for (int col = -1; col <= 1; col++)
{
for (int row = -1; row <= 1; row++)
{
pixel = lbmp.GetPixel(x + row, y + col);
r += pixel.R * Gauss[Index];
g += pixel.G * Gauss[Index];
b += pixel.B * Gauss[Index];
Index++;
}
}
r /= 16;
g /= 16;
b /= 16;
//处理颜色值溢出
r = r > 255 ? 255 : r;
r = r < 0 ? 0 : r;
g = g > 255 ? 255 : g;
g = g < 0 ? 0 : g;
b = b > 255 ? 255 : b;
b = b < 0 ? 0 : b;
newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
}
}
lbmp.UnlockBits();
newlbmp.UnlockBits();
this.bitmap = newbmp;
return this;
}
/// <summary>
/// 图像锐化处理
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public ImageDistinguish SharpenImage()
{
int height = this.bitmap.Height;
int width = this.bitmap.Width;
Bitmap newbmp = new Bitmap(width, height);
LockBitmap lbmp = new LockBitmap(this.bitmap);
LockBitmap newlbmp = new LockBitmap(newbmp);
lbmp.LockBits();
newlbmp.LockBits();
Color pixel;
//拉普拉斯模板
int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
for (int x = 1; x < width - 1; x++)
{
for (int y = 1; y < height - 1; y++)
{
int r = 0, g = 0, b = 0;
int Index = 0;
for (int col = -1; col <= 1; col++)
{
for (int row = -1; row <= 1; row++)
{
pixel = lbmp.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
g += pixel.G * Laplacian[Index];
b += pixel.B * Laplacian[Index];
Index++;
}
}
//处理颜色值溢出
r = r > 255 ? 255 : r;
r = r < 0 ? 0 : r;
g = g > 255 ? 255 : g;
g = g < 0 ? 0 : g;
b = b > 255 ? 255 : b;
b = b < 0 ? 0 : b;
newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
}
}
lbmp.UnlockBits();
newlbmp.UnlockBits();
return this;
}
/// <summary>
/// 获取处理完成后的图片
/// </summary>
/// <returns></returns>
public Bitmap Result()
{
return this.bitmap;
}
/// <summary>
/// 识别二维码
/// </summary>
/// <returns></returns>
public string Distinguish()
{
BarcodeReader reader = new BarcodeReader();
//设置读取的格式(一般为UTF-8)
reader.Options.CharacterSet = "UTF-8";
Result result = reader.Decode(this.bitmap);
if (result != null)
{
return result.Text;
}
else
{
return "";
}
}
/// <summary>
/// 3×3中值滤波除杂,yuanbao,2007.10
/// </summary>
/// <param name="dgGrayValue"></param>
public ImageDistinguish ClearNoise()
{
int x, y;
byte[] p = new byte[9]; //最小处理窗口3*3
byte s;
int i, j;
for (y = 1; y < this.bitmap.Height - 1; y++) //--第一行和最后一行无法取窗口
{
for (x = 1; x < this.bitmap.Width - 1; x++)
{
//取9个点的值
p[0] = this.bitmap.GetPixel(x - 1, y - 1).R;
p[1] = this.bitmap.GetPixel(x, y - 1).R;
p[2] = this.bitmap.GetPixel(x + 1, y - 1).R;
p[3] = this.bitmap.GetPixel(x - 1, y).R;
p[4] = this.bitmap.GetPixel(x, y).R;
p[5] = this.bitmap.GetPixel(x + 1, y).R;
p[6] = this.bitmap.GetPixel(x - 1, y + 1).R;
p[7] = this.bitmap.GetPixel(x, y + 1).R;
p[8] = this.bitmap.GetPixel(x + 1, y + 1).R;
//计算中值
for (j = 0; j < 5; j++)
{
for (i = j + 1; i < 9; i++)
{
if (p[j] > p[i])
{
s = p[j];
p[j] = p[i];
p[i] = s;
}
}
}
this.bitmap.SetPixel(x, y, Color.FromArgb(p[4], p[4], p[4])); //给有效值付中值
}
}
return this;
}
}
public class LockBitmap
{
Bitmap source = null;
IntPtr Iptr = IntPtr.Zero;
BitmapData bitmapData = null;
public byte[] Pixels { get; set; }
public int Depth { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
public LockBitmap(Bitmap source)
{
this.source = source;
}
/// <summary>
/// Lock bitmap data
/// </summary>
public void LockBits()
{
try
{
// Get width and height of bitmap
Width = source.Width;
Height = source.Height;
// get total locked pixels count
int PixelCount = Width * Height;
// Create rectangle to lock
Rectangle rect = new Rectangle(0, 0, Width, Height);
// get source bitmap pixel format size
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
// Check if bpp (Bits Per Pixel) is 8, 24, or 32
if (Depth != 8 && Depth != 24 && Depth != 32)
{
throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
}
// Lock bitmap and return bitmap data
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
source.PixelFormat);
// create byte array to copy pixel values
int step = Depth / 8;
Pixels = new byte[PixelCount * step];
Iptr = bitmapData.Scan0;
// Copy data from pointer to array
Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Unlock bitmap data
/// </summary>
public void UnlockBits()
{
try
{
// Copy data from byte array to pointer
Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);
// Unlock bitmap data
source.UnlockBits(bitmapData);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Get the color of the specified pixel
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public Color GetPixel(int x, int y)
{
Color clr = Color.Empty;
// Get color components count
int cCount = Depth / 8;
// Get start index of the specified pixel
int i = ((y * Width) + x) * cCount;
if (i > Pixels.Length - cCount)
throw new IndexOutOfRangeException();
if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha
{
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
byte a = Pixels[i + 3]; // a
clr = Color.FromArgb(a, r, g, b);
}
if (Depth == 24) // For 24 bpp get Red, Green and Blue
{
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
clr = Color.FromArgb(r, g, b);
}
if (Depth == 8)
// For 8 bpp get color value (Red, Green and Blue values are the same)
{
byte c = Pixels[i];
clr = Color.FromArgb(c, c, c);
}
return clr;
}
/// <summary>
/// Set the color of the specified pixel
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="color"></param>
public void SetPixel(int x, int y, Color color)
{
// Get color components count
int cCount = Depth / 8;
// Get start index of the specified pixel
int i = ((y * Width) + x) * cCount;
if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha
{
Pixels[i] = color.B;
Pixels[i + 1] = color.G;
Pixels[i + 2] = color.R;
Pixels[i + 3] = color.A;
}
if (Depth == 24) // For 24 bpp set Red, Green and Blue
{
Pixels[i] = color.B;
Pixels[i + 1] = color.G;
Pixels[i + 2] = color.R;
}
if (Depth == 8)
// For 8 bpp set color value (Red, Green and Blue values are the same)
{
Pixels[i] = color.B;
}
}
}
- 15:21爆出的小程序功能升级,你还要对小程序观望吗?
- 如何解决分布式系统中的跨时区问题[原理篇]
- 什么是区块链:块的结构
- Spring读书笔记——bean创建(下)
- 当区块链遇上传统行业 我们的生活和工作会改变吗?
- 如何设计开发好一个 HTTP API?
- [WCF权限控制]基于Windows用户组的授权方式[下篇]
- Spring读书笔记——bean解析
- 10个大数据误区,看看你中了几个?
- 从数据到代码——通过代码生成机制实现强类型编程[上篇]
- Spring读书笔记——bean加载
- Java8-初识Lambda
- 我的WCF之旅(5):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的重载(Overloading)
- WCF技术剖析之三十:一个很有用的WCF调用编程技巧[下篇]
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- MySQL死锁系列-线上死锁问题排查思路
- # 3分钟短文:Laravel路由注册,你必须掌握的“动词”!
- 资深程序员带你解锁Android性能优化五大误区和两大疑点!(附333页性能优化PDF宝典)
- Android大三提前批的钉钉和抖音面筋,阿里挂了HR面,抖音通过收获Offer
- leetcode之键盘行
- 浅析centos 7 自带的 php 5.4升级为 5.6的方法
- Linux内核设备驱动地址映射笔记整理
- Linux中的who命令实例介绍
- Linux十个新手命令分享
- Linux 内核通用链表学习小结
- 从零开始针对 .NET 应用的 DevOps 运营实践 - 运行环境搭建
- 面试问我,创建多少个线程合适?我该怎么说
- C语言链表实例--玩转链表
- 帝国CMS怎么利用灵动标签循环调用多个栏目下的文章
- PHP 加密 Password Hashing API基础知识点