/// <summary> /// 集合中删除元素(线程安全), 必须调用base,否则会造成无限递归 /// </summary> /// <param name="ts"></param> public void Remove(LayerImage li) { lock (this.SyncRoot) { base.Remove(li); } }
/// <summary> /// 集合中增加元素(线程安全), 必须调用base,否则会造成无限递归 /// </summary> /// <param name="li">LayerImage元素</param> public void Add(LayerImage li) { lock (this.SyncRoot) { base.Add(li); } }
/// <summary> /// 求得(x,y)处x的偏导数 /// </summary> /// <param name="l">层号</param> /// <param name="x">x坐标</param> /// <param name="y">y坐标</param> /// <returns>偏导数值</returns> private float Ix(int l, float x, float y) { LayerImage li = I[l]; return((li[x + 1, y] - li[x - 1, y]) / 2); }
/// <summary> /// 求得(x,y)处y的偏导数 /// </summary> /// <param name="l">层号</param> /// <param name="x">x坐标</param> /// <param name="y">y坐标</param> /// <returns>偏导数值</returns> private float Iy(int l, float x, float y) { LayerImage li = I[l]; return((li[x, y + 1] - li[x, y - 1]) / 2); }
/// <summary> /// 将位图转换为层信息 /// </summary> /// <param name="bmp">位图</param> /// <returns>返回LayerImageCollection</returns> public LayerImageCollection TransformBmpToLayerImg(Bitmap bmp) { if (bmp == null || bmp.PixelFormat != PixelFormat.Format8bppIndexed) { throw new Exception("原位图为空或者不是灰度图。"); } int width = bmp.Width; int height = bmp.Height; Rectangle rect = new Rectangle(0, 0, width, height); BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); // Stride为位图中每一行以4字节对齐的行宽 int strideSource = bmpdata.Stride; byte[] byteData = new byte[width * height]; LayerImage li = null; LayerImageCollection layerCollection = null; unsafe { byte *ptr = (byte *)bmpdata.Scan0.ToPointer(); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { // 位图的在内存中的排列是从左到右,从下到上的,但BitmapData貌似做了优化,把位图数据在内存中的排列 // 顺序改为与坐标系一致,即从左到右、从上到下 byteData[col + row * width] = *ptr; ptr++; } } bmp.UnlockBits(bmpdata); } layerCollection = new LayerImageCollection(); li = new LayerImage(byteData, width, height); layerCollection.Add(li); // 加入I的第0层数据 for (int l = 1; l < LN; l++) { int w, h; // 当前层图像的宽和高 // width和height为前一层图像的宽和高 w = (width + 1) / 2; h = (height + 1) / 2; byteData = new byte[w * h]; for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { int leftX = 2 * col - 1; int rightX = 2 * col + 1; int lowY = 2 * row - 1; int upY = 2 * row + 1; int centerX = 2 * col; int centerY = 2 * row; if (leftX < 0) { leftX = 0; } if (rightX >= width) { rightX = width - 1; } if (lowY < 0) { lowY = 0; } if (upY >= height) { upY = height - 1; } // 根据上一层的数据生成当前层的数据 byteData[col + row * w] = (byte)(0.25 * li.ImageData[centerX + centerY * width] + 0.125 * (li.ImageData[leftX + centerY * width] + li.ImageData[rightX + centerY * width] + li.ImageData[centerX + lowY * width] + li.ImageData[centerX + upY * width]) + 0.0625 * (li.ImageData[leftX + lowY * width] + li.ImageData[rightX + lowY * width] + li.ImageData[leftX + upY * width] + li.ImageData[rightX + upY * width])); } } li = new LayerImage(byteData, w, h); layerCollection.Add(li); width = w; height = h; } return(layerCollection); }