📄 convoperation.cs
字号:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Data;
namespace image
{
/// <summary>
/// Summary description for ConvOperation.
/// </summary>
public class ConvOperation
{
public ConvOperation()
{
//
// TODO: Add constructor logic here
//
}
/* public static Bitmap MeanFilter(Bitmap b, int nWidth)
{
CSharpMask m = new CSharpMask();
m.setAll(1);
m.Pixel = nWidth;
m.Factor = 9;
m.Offset = 0;
return simpleConv(b, m);
}
public static Bitmap GaussianSmoothing(Bitmap b, int nWidth)
{
CSharpMask m = new CSharpMask();
m.setAll(2);
m.Pixel = nWidth;
m.Factor = 16;
m.Offset = 0;
m.BottomLeft = 1;
m.BottomRight = 1;
m.TopLeft = 1;
m.TopRight = 1;
return simpleConv(b, m);
}
*/
public static Bitmap simpleConv(Bitmap b, CSharpMask m,int l)
{
if (m.Factor == 0)
return b;
Bitmap OutPutImage;
OutPutImage = (Bitmap)b.Clone();
BitmapData bData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
BitmapData copyData = b.LockBits(new Rectangle(0, 0, OutPutImage.Width, OutPutImage.Height ), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bData.Stride;
int stride2 = stride * 2;
int stride3 = stride * 3;
int stride4 = stride * 4;
int stride5 = stride * 5;
int stride6 = stride * 6;
int i=0;
System.IntPtr ptr = bData.Scan0;
System.IntPtr ptrOutPut = copyData.Scan0;
unsafe
{
byte* p = (byte*)(void*)ptr;
byte* pOutPut = (byte*)(void*)ptrOutPut;
int nOffset =stride - b.Width * 3;
int nWidth =0;
int nHeight =0;
int nPixel;
if (l==3)
{
nWidth = b.Width - 2;
nHeight =b.Height-2 ;
}
if (l==5)
{
nWidth =b.Width - 4;
nHeight =b.Height-4 ;
}
if (l==7)
{
nWidth =b.Width - 6;
nHeight =b.Height -6;
}
// pOutPut +=nOffset;
// p += nOffset;
for (int x = 0; x < nHeight; x++)
for (int y = 0; y < nWidth; ++y)
{
/*
*Now we will process the three pixels R,G and B
*in a single Pass
* */
/* nPixel = (((pOutPut[2] * m.TopLeft) + (pOutPut[5] * m.TopMiddle) + (pOutPut[8] * m.TopRight) +
(pOutPut[stride + 2] * m.MiddleLeft) + (pOutPut[stride + 5] * m.Pixel) + (pOutPut[stride + 8] * m.MiddleRight)
+ (pOutPut[stride2 + 2] * m.BottomLeft) + (pOutPut[stride2 + 5] * m.BottomMiddle) + (pOutPut[stride2 + 8] * m.BottomRight)) / m.Factor) + m.Offset;
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
pOutPut[5 + stride] = (byte)nPixel;
nPixel = ((((pOutPut[1] * m.TopLeft) + (pOutPut[4] * m.TopMiddle) +
(pOutPut[7] * m.TopRight) + (pOutPut[1 + stride] * m.MiddleLeft) +
(pOutPut[4 + stride] * m.Pixel) + (pOutPut[7 + stride] * m.MiddleRight) +
(pOutPut[1 + stride2] * m.BottomLeft) +
(pOutPut[4 + stride2] * m.BottomMiddle) +
(pOutPut[7 + stride2] * m.BottomRight))
/ m.Factor) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
pOutPut[4 + stride] = (byte)nPixel;
nPixel = ((((pOutPut[0] * m.TopLeft) + (pOutPut[3] * m.TopMiddle) +
(pOutPut[6] * m.TopRight) + (pOutPut[0 + stride] * m.MiddleLeft) +
(pOutPut[3 + stride] * m.Pixel) +
(pOutPut[6 + stride] * m.MiddleRight) +
(pOutPut[0 + stride2] * m.BottomLeft) +
(pOutPut[3 + stride2] * m.BottomMiddle) +
(pOutPut[6 + stride2] * m.BottomRight))
/ m.Factor) + m.Offset);*/
if (l==3)
{
nPixel = ((((pOutPut[0+i] * m.array3[0, 0]) + (pOutPut[3+i] * m.array3[0, 1]) +
(pOutPut[6+i] * m.array3[0, 2]) + (pOutPut[0+i + stride] * m.array3[1, 0]) +
(pOutPut[3+i + stride] * m.array3[1, 1]) +
(pOutPut[6+i + stride] * m.array3[1, 2]) +
(pOutPut[0+i + stride2] * m.array3[2, 0]) +
(pOutPut[3+i + stride2] * m.array3[2, 1]) +
(pOutPut[6+i+ stride2] * m.array3[2, 2]))
/ 9) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
pOutPut[3+i + stride] = (byte)nPixel;
i=i+1;
nPixel = ((((pOutPut[0+i] * m.array3[0, 0]) + (pOutPut[3+i] * m.array3[0, 1]) +
(pOutPut[6+i] * m.array3[0, 2]) + (pOutPut[0+i + stride] * m.array3[1, 0]) +
(pOutPut[3+i + stride] * m.array3[1, 1]) +
(pOutPut[6+i + stride] * m.array3[1, 2]) +
(pOutPut[0+i + stride2] * m.array3[2, 0]) +
(pOutPut[3+i + stride2] * m.array3[2, 1]) +
(pOutPut[6+i+ stride2] * m.array3[2, 2]))
/ 9) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
pOutPut[3+i + stride] = (byte)nPixel;
i=i+1;
nPixel = ((((pOutPut[0+i] * m.array3[0, 0]) + (pOutPut[3+i] * m.array3[0, 1]) +
(pOutPut[6+i] * m.array3[0, 2]) + (pOutPut[0+i + stride] * m.array3[1, 0]) +
(pOutPut[3+i + stride] * m.array3[1, 1]) +
(pOutPut[6+i + stride] * m.array3[1, 2]) +
(pOutPut[0+i + stride2] * m.array3[2, 0]) +
(pOutPut[3+i + stride2] * m.array3[2, 1]) +
(pOutPut[6+i+ stride2] * m.array3[2, 2]))
/ 9) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
pOutPut[3+i + stride] = (byte)nPixel;
i=0;
/* nPixel = ((((pOutPut[1] * m.array3[0, 0]) + (pOutPut[4] * m.array3[0, 1]) +
(pOutPut[7] * m.array3[0, 2]) + (pOutPut[1 + stride] * m.array3[1, 0]) +
(pOutPut[4 + stride] * m.array3[1, 1]) +
(pOutPut[7 + stride] * m.array3[1, 2]) +
(pOutPut[1 + stride2] * m.array3[2, 0]) +
(pOutPut[4 + stride2] * m.array3[2, 1]) +
(pOutPut[7 + stride2] * m.array3[2, 2]))
/ 9) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[4 + stride] = (byte)nPixel;
nPixel = ((((pOutPut[2] * m.array3[0, 0]) + (pOutPut[5] * m.array3[0, 1]) +
(pOutPut[8] * m.array3[0, 2]) + (pOutPut[2 + stride] * m.array3[1, 0]) +
(pOutPut[5 + stride] * m.array3[1, 1]) +
(pOutPut[8 + stride] * m.array3[1, 2]) +
(pOutPut[2 + stride2] * m.array3[2, 0]) +
(pOutPut[5 + stride2] * m.array3[2, 1]) +
(pOutPut[8 + stride2] * m.array3[2, 2]))
/ 9) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
pOutPut[5 + stride] = (byte)nPixel;*/
p += 3;
pOutPut += 3;
}
if (l==5)
{
nPixel = ((((pOutPut[0+i] * m.array5[0, 0]) + (pOutPut[3+i] * m.array5[0, 1]) +
(pOutPut[6+i] * m.array5[0, 2]) +(pOutPut[9+i] * m.array5[0, 3])+(pOutPut[12+i] * m.array5[0, 4])+ (pOutPut[0+i + stride] * m.array5[1, 0]) +
(pOutPut[3 +i+ stride] * m.array5[1, 1]) +
(pOutPut[6 +i+ stride] * m.array5[1, 2]) +
(pOutPut[9 +i+ stride] * m.array5[1, 3]) +
(pOutPut[12+i + stride] * m.array5[1, 4]) +
(pOutPut[0 +i+ stride2] * m.array5[2, 0]) +
(pOutPut[3 +i+ stride2] * m.array5[2, 1]) +
(pOutPut[6 +i+ stride2] * m.array5[2, 2]) +
(pOutPut[9 +i+ stride2] * m.array5[2, 3]) +
(pOutPut[12+i + stride2] * m.array5[2, 4]) +
(pOutPut[0 +i+ stride3] * m.array5[3, 0]) +
(pOutPut[3 +i+ stride3] * m.array5[3, 1]) +
(pOutPut[6 +i+ stride3] * m.array5[3, 2]) +
(pOutPut[9 +i+ stride3] * m.array5[3, 3]) +
(pOutPut[12 +i+ stride3] * m.array5[3, 4]) +
(pOutPut[0 +i+ stride4] * m.array5[4, 0]) +
(pOutPut[3 +i+ stride4] * m.array5[4, 1]) +
(pOutPut[6 +i+ stride4] * m.array5[4, 2]) +
(pOutPut[9 +i+ stride4] * m.array5[4, 3]) +
(pOutPut[12 +i+ stride4] * m.array5[4, 4]))
/ 25) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
pOutPut[9 +i+ stride] = (byte)nPixel;
i=i+1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -