public FocusImg(FocusImg img) { m_height = img.Height; m_width = img.Width; m_stride = img.m_stride; m_imgData = new byte[m_height*m_stride]; Array.Copy(img.ImgData, m_imgData, Height*m_stride); }
/// <summary> /// 打开单幅Focus格式的图片 /// </summary> /// <param name="path"></param> /// <returns></returns> private static FocusImg OpenFormatFOCUS(string path) { BinaryReader br = null; try { br = new BinaryReader(File.OpenRead(path)); int width = br.ReadInt32(); int height = br.ReadInt32(); FocusImg img = new FocusImg(width, height); img.ImgData = br.ReadBytes(img.Stride * height); return img; } catch (System.Exception) { return null; } finally { if (br != null) { br.Close(); } } }
/// <summary> /// 打开Focus格式的文件,包含双光源模式 /// </summary> /// <param name="path">文件路径</param> /// <param name="config">转换配置配置,本函数配置可以为null</param> /// <returns> /// config为All模式时候: FocusImg[0]为绿光; FocusImg[1]红外. FocusImg[2]为紫外。 /// config为Ir,Green或Uv模式时,FocusImg[0]返回当前指定的图像。 /// 出错时返回null。 /// </returns> public static FocusImg[] OpenFormatFOCUS(string path, OpenConfig config) { FocusImg img1 = OpenFormatFOCUS(path); FocusImg img2 = null; string pathgr = path.Replace("ir", "green"); int imgNum = 1; if (File.Exists(pathgr) && pathgr != path) { imgNum = 2; img2 = OpenFormatFOCUS(pathgr); } FocusImg[] imgs = null; if (imgNum == 1) { if (img1 != null) { imgs = new FocusImg[1]; imgs[0] = img1; return imgs; } else return imgs; } else { if (img1 != null && img2 != null) { imgs = new FocusImg[2]; imgs[0] = img1; imgs[1] = img2; return imgs; } else return imgs; } }
/// <summary> /// 打开DM642格式的文件, 多线程调用不安全 /// </summary> /// <param name="path">文件路径</param> /// <param name="config">转换配置配置</param> /// <returns> /// config为All模式时候: FocusImg[0]为绿光; FocusImg[1]红外. FocusImg[2]为紫外。 /// config为Ir,Green或Uv模式时,FocusImg[0]返回当前指定的图像。 /// 出错时返回null。 /// </returns> public static FocusImg[] OpenFormatDM642(string path, OpenConfig config) { string fileName = Path.GetFileName(path); string folder = Path.GetDirectoryName(path); string folderName = Path.GetFileName(folder); string[] inputpath = new string[3]; inputpath[0] = ""; inputpath[1] = ""; inputpath[2] = ""; short lightNum = 0; //单光源 if (config.lightNum == ConfigLightNum.Single) { inputpath[0] = path; lightNum = 1; if (!File.Exists(inputpath[0])) return null; } //多光源 : 三光源和双光源 if (config.lightNum == ConfigLightNum.Double || config.lightNum == ConfigLightNum.Triple) { //绿光路径 inputpath[0] = folder.Replace(folderName, "green") + @"\" + fileName; //红外路径 inputpath[1] = folder.Replace(folderName, "ir") + @"\" + fileName; lightNum = 2; if (!File.Exists(inputpath[0]) || !File.Exists(inputpath[1])) return null; } if (config.lightNum == ConfigLightNum.Triple) { //紫外路径 inputpath[2] = folder.Replace(folderName, "uv") + @"\" + fileName; lightNum = 3; if (!File.Exists(inputpath[2])) return null; } //设置校正方式 int isReflect = config.reviseMode != ConfigReviseMode.Reflect ? 0 : 1; //设置矫正图 int reviseIndex = 0; if (config.lightNum == ConfigLightNum.Single) reviseIndex = 0; if (config.basicImg == ConfigBasicImg.Ir) reviseIndex = 1; else if (config.basicImg == ConfigBasicImg.Green) reviseIndex = 0; //读取文件并且校正 try { FileStream[] fsArray = new FileStream[3]; byte[][] datArray = new byte[3][]; int[] lenArray = new int[3]; fsArray[0] = File.OpenRead(inputpath[0]); datArray[0] = new byte[fsArray[0].Length]; fsArray[0].Read(datArray[0], 0, (int)fsArray[0].Length); lenArray[0] = datArray[0].Length-4; fsArray[0].Close(); if (config.lightNum == ConfigLightNum.Double || config.lightNum == ConfigLightNum.Triple) { fsArray[1] = File.OpenRead(inputpath[1]); datArray[1] = new byte[fsArray[1].Length]; fsArray[1].Read(datArray[1], 0, (int)fsArray[1].Length); lenArray[1] = datArray[1].Length-4; fsArray[1].Close(); } if (config.lightNum == ConfigLightNum.Triple) { fsArray[2] = File.OpenRead(inputpath[2]); datArray[2] = new byte[fsArray[2].Length]; fsArray[2].Read(datArray[2], 0, (int)fsArray[2].Length); lenArray[2] = datArray[2].Length-4; fsArray[2].Close(); } IntPtr[] pDat = new IntPtr[3]; for (int i = 0; i < pDat.Length; i++ ) { unsafe { fixed (byte* p = datArray[i]) { pDat[i] = new IntPtr(p); } } } if (1 != DM642API.WinImageInitPlus(pDat, lenArray, lightNum, reviseIndex, isReflect)) return null; FocusImg[] imgs = new FocusImg[lightNum]; for (int picIndex = 0; picIndex < lightNum; picIndex++) { int width = DM642API.RMB_getEWidth(picIndex); int height = DM642API.RMB_getEHeight(picIndex); FocusImg img = new FocusImg(width, height); DM642API.RmbGetReviseImgByStride(img.ImgData, 0, 0, width, height, img.Stride, picIndex); bool isEqual = false; bool isNeighbor = false; if (lightNum == 1 || picIndex == 1) //单光源或者红外 { isEqual = config.irEqual; isNeighbor = config.irNeighbor; } else { isEqual = config.uvgrEqual; isNeighbor = config.uvgrNeighbor; } if (isEqual) { int[] hist = new int[256]; DM642API.GetHist(hist, picIndex); img.Equalization(hist); } if (isNeighbor) img.AverageFilter(); imgs[picIndex] = img; } return imgs; } catch (Exception ex) { Console.WriteLine(ex.Message); return null; } }
/// <summary> /// 打开C54XX格式的dat文件,暂时没有实现 /// </summary> /// <param name="path"></param> /// <param name="config"></param> /// <returns></returns> public static FocusImg[] OpenFormatC54XX(string path, OpenConfig config) { FileStream irfs = null; try { irfs = File.OpenRead(path); byte[] irDat = new byte[irfs.Length]; irfs.Read(irDat, 0, (int)irfs.Length); C54XXAPI.C5409_Init(irDat, (UInt32)irfs.Length); float EW = C54XXAPI.C5409_GetW(); float EH = C54XXAPI.C5409_GetH(); int width = (int)Math.Round(EW); int height = (int)Math.Round(EH); FocusImg[] img = new FocusImg[1]; img[0] = new FocusImg(width, height); if (config.irEqual == true) C54XXAPI.C5409_GethistequalImg(img[0].ImgData, 0, 0, width, height, img[0].Stride); else C54XXAPI.C5409_GetImg(img[0].ImgData, 0, 0, width, height, img[0].Stride); return img; } catch (System.Exception ex) { string m = ex.Message; return null; } finally { irfs.Close(); } }
public FocusImg ZoomIn(int scaleX,int scaleY) { if (scaleX <= 1&&scaleY<=1) { return this; } int width = m_width * scaleX; int height = m_height * scaleY; FocusImg dstimg = new FocusImg(width, height); int stride = dstimg.m_stride; for (int j=0; j<m_height; j++ ) { for (int i=0; i<m_width; i++ ) { byte gray = m_imgData[j*m_stride+i]; int index = j*scaleY*stride + i*scaleX; for (int y=0; y<scaleY; y++ ) { for (int x=0; x<scaleX; x++ ) { dstimg.ImgData[index + y*stride +x] = gray; } } } } return dstimg; }
public FocusImg GetImgByRect(Rectangle rect, RotateType rotateType = RotateType.FlipNone) { Rectangle srcRect = new Rectangle(0, 0, m_width, m_height); if (srcRect.Contains(rect)) { FocusImg dstImg = new FocusImg(rect.Width, rect.Height); int index = 0; for (int j=rect.Top; j<rect.Bottom; j++) { for (int i=rect.Left; i<rect.Right; i++) { dstImg.ImgData[index++] = m_imgData[j * m_stride + i]; //this[,] } index += dstImg.m_stride-dstImg.m_width; } return dstImg; } else { throw new Exception("FocusImg访问越界"); } }
public FocusImg Flip(RotateType type) { FocusImg rotateImg = new FocusImg(m_width, m_height); for (int y = 0; y < m_height; y++) { for (int x = 0; x < m_width; x++) { switch (type) { case RotateType.FlipX: rotateImg[x, y] = this[m_width-x-1, y]; break; case RotateType.FlipY: rotateImg[x, y] = this[x, m_height-y-1]; break; case RotateType.FlipXY: rotateImg[x, y] = this[m_width-x-1, m_height-y-1]; break; default: break; } } } return rotateImg; }
public void AverageFilter() { FocusImg copy = new FocusImg(this); for (int j = 1; j < Height - 1; j++) { for (int i = 1; i < Width - 1; i++) { int g = 0; g += copy.m_imgData[j * copy.m_stride + i]; g += copy.m_imgData[(j - 1) * copy.m_stride + i]; g += copy.m_imgData[(j + 1) * copy.m_stride + i]; g += copy.m_imgData[j * copy.m_stride + i + 1]; g += copy.m_imgData[j * copy.m_stride + i - 1]; g = (int)(g*0.2); m_imgData[j * m_stride + i] = (byte)g; } } }
public FocusImgOnDM642(FocusImg img) : base(img) { }