/// <summary> /// 缩放彩色图像(改进型) /// </summary> /// <param name="pDestImg"></param> /// <param name="pSrcImg"></param> /// <param name="newWidth"></param> /// <param name="newHeight"></param> /// <returns></returns> public static int Zoomup(out ColorImg pDestImg, ColorImg pSrcImg, int newWidth, int newHeight) { pDestImg = new ColorImg(); PicBase.ImgClone(out pDestImg, pSrcImg); if (pSrcImg.Width == newWidth && pSrcImg.Height == newHeight) return 1; float xscale = (float)newWidth / (float)pSrcImg.Width; float yscale = (float)newHeight / (float)pSrcImg.Height; float xscale2 = 1 - xscale; float yscale2 = 1 - yscale; pDestImg.Width = newWidth; pDestImg.Height = newHeight; int contLen = newWidth * newHeight; pDestImg.R = new byte[contLen]; pDestImg.G = new byte[contLen]; pDestImg.B = new byte[contLen]; /*插值存在黑色线条,插值用除法来做*/ for (int y = 0; y < newHeight; y++) { int Fy = (int)(y / yscale); for (int x = 0; x < newWidth; x++) { int Fx = (int)(x / xscale); //像素在原图像中位置 int Loc1 = Fy * pSrcImg.Width + Fx; int Loc12 = (Fy + 1) * pSrcImg.Width + Fx; //像素在目标图像中位置 int Loc2 = y * newWidth + x; pDestImg.R[Loc2] = (byte)((pSrcImg.R[Loc1] * xscale + pSrcImg.R[Loc1 + 1] * (1 - xscale2)) * yscale + (pSrcImg.R[Loc12] * xscale + pSrcImg.R[Loc12 + 1] * (1 - xscale2)) * yscale2); pDestImg.G[Loc2] = (byte)((pSrcImg.G[Loc1] * xscale + pSrcImg.G[Loc1 + 1] * (1 - xscale2)) * yscale + (pSrcImg.G[Loc12] * xscale + pSrcImg.G[Loc12 + 1] * (1 - xscale2)) * yscale2); pDestImg.B[Loc2] = (byte)((pSrcImg.B[Loc1] * xscale + pSrcImg.B[Loc1 + 1] * (1 - xscale2)) * yscale + (pSrcImg.B[Loc12] * xscale + pSrcImg.B[Loc12 + 1] * (1 - xscale2)) * yscale2); } } return 1; }
/// <summary> /// 缩放彩色图像 /// </summary> /// <param name="pDestImg"></param> /// <param name="pSrcImg"></param> /// <param name="newWidth"></param> /// <param name="newHeight"></param> /// <returns></returns> public static int Zoomup1(out ColorImg pDestImg, ColorImg pSrcImg, int newWidth, int newHeight) { pDestImg = new ColorImg(); PicBase.ImgClone(out pDestImg, pSrcImg); if (pSrcImg.Width == newWidth && pSrcImg.Height == newHeight) return 1; float xscale = (float)newWidth / (float)pSrcImg.Width; float yscale = (float)newHeight / (float)pSrcImg.Height; pDestImg.Width = newWidth; pDestImg.Height = newHeight; int contLen = newWidth * newHeight; pDestImg.R = new byte[contLen]; pDestImg.G = new byte[contLen]; pDestImg.B = new byte[contLen]; /*插值存在黑色线条,插值用除法来做*/ for (int y = 0; y < newHeight; y++) { int Fy = (int)(y / yscale); for (int x = 0; x < newWidth; x++) { int Fx = (int)(x / xscale); int Loc1 = Fy * pSrcImg.Width + Fx; int Loc2 = y * newWidth + x; pDestImg.R[Loc2] = pSrcImg.R[Loc1]; pDestImg.G[Loc2] = pSrcImg.G[Loc1]; pDestImg.B[Loc2] = pSrcImg.B[Loc1]; } } return 1; }
/// <summary> /// 预定义彩色图像矩阵转为Bitmap图像 /// </summary> /// <param name="out_Bmp">输出的Bitmap图像</param> /// <param name="in_corImg">输入彩色图</param> public static int ArrayToImg(out Bitmap out_Bmp, ColorImg in_corImg) { int width = in_corImg.Width; int height = in_corImg.Height; out_Bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb); int allPixel = width * height; BitmapData bmData = out_Bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); System.IntPtr PtrStart = bmData.Scan0; unsafe { byte* Ptr = (byte*)(void*)PtrStart; int nOffset = bmData.Stride - width * 3; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Ptr[2] = in_corImg.R[i * width + j]; Ptr[1] = in_corImg.G[i * width + j]; Ptr[0] = in_corImg.B[i * width + j]; Ptr += 3; } Ptr += nOffset; } //for (int i = 0; i < allPixel; i++) //{ // Ptr[2] = in_corImg.R[i]; // Ptr[1] = in_corImg.G[i]; // Ptr[0] = in_corImg.B[i]; // Ptr += 3; //} } out_Bmp.UnlockBits(bmData); return 1; }
/// <summary> /// 图像双向增强 /// </summary> /// <param name="PicMap"></param> /// <param name="Result"></param> public static void ImageEnhance(ColorImg PicMap, out ColorImg Result) { int Width, Height; //原图像相关数据 //传入图像信息 Width = PicMap.Width; Height = PicMap.Height; //返回图像大小 Result.Width = Width; Result.Height = Height; //开辟返回图像数据区 Result.B = new byte[Width * Height]; Result.G = new byte[Width * Height]; Result.R = new byte[Width * Height]; //3x3模板 double[,] h ={ { -1d, -1d, -1d}, { -1d, 8d,- -1d}, { -1d, -1d, -1d}, }; for (int j = 1; j < Height - 1; j++) { for (int i = 1; i < Width - 1; i++) { double[] pby_pt = { 0, 0, 0 }; pby_pt[0] = h[0, 0] * PicMap.R[(j - 1) * Width + i - 1] + h[0, 1] * PicMap.R[(j - 1) * Width + i] + h[0, 2] * PicMap.R[(j - 1) * Width + i + 1] + h[1, 0] * PicMap.R[j * Width + i - 1] + h[1, 1] * PicMap.R[j * Width + i] + h[1, 2] * PicMap.R[j * Width + i + 1] + h[2, 0] * PicMap.R[(j + 1) * Width + i - 1] + h[2, 1] * PicMap.R[(j + 1) * Width + i] + h[2, 2] * PicMap.R[(j + 1) * Width + i + 1]; pby_pt[1] = h[0, 0] * PicMap.G[(j - 1) * Width + i - 1] + h[0, 1] * PicMap.G[(j - 1) * Width + i] + h[0, 2] * PicMap.G[(j - 1) * Width + i + 1] + h[1, 0] * PicMap.G[j * Width + i - 1] + h[1, 1] * PicMap.G[j * Width + i] + h[1, 2] * PicMap.G[j * Width + i + 1] + h[2, 0] * PicMap.G[(j + 1) * Width + i - 1] + h[2, 1] * PicMap.G[(j + 1) * Width + i] + h[2, 2] * PicMap.G[(j + 1) * Width + i + 1]; pby_pt[2] = h[0, 0] * PicMap.B[(j - 1) * Width + i - 1] + h[0, 1] * PicMap.B[(j - 1) * Width + i] + h[0, 2] * PicMap.B[(j - 1) * Width + i + 1] + h[1, 0] * PicMap.B[j * Width + i - 1] + h[1, 1] * PicMap.B[j * Width + i] + h[1, 2] * PicMap.B[j * Width + i + 1] + h[2, 0] * PicMap.B[(j + 1) * Width + i - 1] + h[2, 1] * PicMap.B[(j + 1) * Width + i] + h[2, 2] * PicMap.B[(j + 1) * Width + i + 1]; pby_pt[0] /= 9.0d; pby_pt[1] /= 9.0d; pby_pt[2] /= 9.0d; if (pby_pt[0] > 20) Result.R[j * Width + i] = (byte)(Math.Abs(pby_pt[0]) + 100); else Result.R[j * Width + i] = (byte)(Math.Abs(pby_pt[0])); if (pby_pt[1] > 20) Result.G[j * Width + i] = (byte)(Math.Abs(pby_pt[1]) + 100); else Result.G[j * Width + i] = (byte)(Math.Abs(pby_pt[1])); if (pby_pt[2] > 20) Result.B[j * Width + i] = (byte)(Math.Abs(pby_pt[2]) + 100); else Result.B[j * Width + i] = (byte)(Math.Abs(pby_pt[2])); } } //p_temp.CopyTo(PicMap.Img, 0); }
/// <summary> /// 灰度图结构转彩色图结构 /// </summary> /// <param name="out_corImg">输出的彩色图像</param> /// <param name="in_gryImg">输入的灰度图像</param> public int GryToCor(out ColorImg out_corImg, GrayImg in_gryImg) { out_corImg = new ColorImg(); out_corImg.Width = in_gryImg.Width; out_corImg.Height = in_gryImg.Height; out_corImg.R = (byte[])in_gryImg.Img.Clone(); out_corImg.G = (byte[])in_gryImg.Img.Clone(); out_corImg.B = (byte[])in_gryImg.Img.Clone(); return 1; }
/// <summary> /// 彩色图结构转灰度图结构 /// </summary> /// <param name="out_gryImg">输出的灰度图像</param> /// <param name="in_corImg">输入的彩色图像</param> public int CorToGry(out GrayImg out_gryImg, ColorImg in_corImg) { int width = in_corImg.Width; int height = in_corImg.Height; int allPixel = width * height; int res = ImgMalloc(out out_gryImg, width, height); if (res == 0) return 0; out_gryImg.Width = width; out_gryImg.Height = height; for (int i = 0; i < allPixel; i++) { out_gryImg.Img[i] = (byte)(0.299f * in_corImg.R[i] + 0.587f * in_corImg.G[i] + 0.114f * in_corImg.B[i]); } return 1; }
/// <summary> /// 彩色图像转成定义类型 /// </summary> /// <param name="corImg">转换后的彩色图像</param> /// <param name="out_corImg">输出的彩色图像</param> /// <param name="in_Bmp">输入的图像</param> public static int ImgToArray(out ColorImg out_corImg, Bitmap in_Bmp) { int width = in_Bmp.Width; int height = in_Bmp.Height; int allPixel = width * height; int res = ImgMalloc(out out_corImg, in_Bmp.Width, in_Bmp.Height); if (res == 0) return 0; BitmapData bmData = in_Bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); System.IntPtr PtrStart = bmData.Scan0; unsafe { byte* Ptr = (byte*)(void*)PtrStart; int Height = in_Bmp.Height; int Width = in_Bmp.Width; int nOffset = bmData.Stride - Width * 3; for (int i = 0; i < Height; i++) { for (int j = 0; j < width; j++) { out_corImg.R[i * width + j] = Ptr[2]; out_corImg.G[i * width + j] = Ptr[1]; out_corImg.B[i * width + j] = Ptr[0]; Ptr += 3; } Ptr += nOffset; } } in_Bmp.UnlockBits(bmData); return 1; }
/// <summary> /// 给彩色图像预申请空间 /// </summary> /// <param name="out_corImg">输出申请过空间的彩色图像</param> /// <param name="width">申请图像的宽度</param> /// <param name="height">申请图像的高度</param> public static int ImgMalloc(out ColorImg out_corImg, int width, int height) { out_corImg = new ColorImg(); int allpixel = width * height; if (allpixel <= 0) return 0; out_corImg.R = new byte[allpixel]; out_corImg.G = new byte[allpixel]; out_corImg.B = new byte[allpixel]; out_corImg.Height = height; out_corImg.Width = width; return 1; }
/// <summary> /// 彩色图像复制 /// </summary> /// <param name="out_corImg">输出的彩色图像</param> /// <param name="in_corImg">输入的彩色图像</param> public static int ImgClone(out ColorImg out_corImg, ColorImg in_corImg) { int res = ImgMalloc(out out_corImg, in_corImg.Width, in_corImg.Height); if (res == 0) return 0; out_corImg.Width = in_corImg.Width; out_corImg.Height = in_corImg.Height; out_corImg.R = (byte[])in_corImg.R.Clone(); out_corImg.B = (byte[])in_corImg.B.Clone(); out_corImg.G = (byte[])in_corImg.G.Clone(); return 1; }