/// <summary> /// Set the mouse position over the image. /// It also set the color intensity of the pixel on the image where is mouse is at /// </summary> /// <param name="location">The location of the mouse on the image</param> public void SetMousePositionOnImage(Point location) { IImage img = _imageBox.DisplayedImage; Size size = img.Size; location.X = Math.Max(Math.Min(location.X, size.Width - 1), 0); location.Y = Math.Max(Math.Min(location.Y, size.Height - 1), 0); mousePositionTextbox.Text = location.ToString(); if (_imageType == typeof(CvArray <>)) { MCvScalar scalar = CvInvoke.cvGet2D(img.Ptr, location.Y, location.X); colorIntensityTextbox.Text = BufferToString(scalar.ToArray(), img.NumberOfChannels); } else if (_imageType == typeof(Mat)) { Mat mat = img as Mat; RenderIntensityForMat(mat, location); } else if (_imageType == typeof(UMat)) { UMat umat = img as UMat; using (Mat mat = umat.GetMat(AccessType.Read)) { RenderIntensityForMat(mat, location); } } else { colorIntensityTextbox.Text = String.Empty; } }
/// <summary> /// Tenengrad梯度方法利用Sobel算子分别计算水平和垂直方向的梯度,同一场景下梯度值越高,图像越清晰。 /// 以下是具体实现,这里衡量的指标是经过Sobel算子处理后的图像的平均灰度值,值越大,代表图像越清晰。 /// </summary> /// <param name="fileName">包含文件路径的文件名</param> /// <returns></returns> public static double Tenengrad(string fileName) { Mat imageSource = CvInvoke.Imread(fileName, Emgu.CV.CvEnum.LoadImageType.Color); Mat imageGrey = new Mat(); CvInvoke.CvtColor(imageSource, imageGrey, Emgu.CV.CvEnum.ColorConversion.Rgb2Gray); Mat imageSobel = new Mat(); CvInvoke.Sobel(imageGrey, imageSobel, Emgu.CV.CvEnum.DepthType.Cv16U, 1, 1); //图像的平均灰度 MCvScalar scalar = CvInvoke.Mean(imageSobel); return(scalar.ToArray()[0]); }
/// <summary> /// Set the mouse position over the image. /// It also set the color intensity of the pixel on the image where is mouse is at /// </summary> /// <param name="location">The location of the mouse on the image</param> public void SetMousePositionOnImage(Point location) { IImage img = _imageBox.DisplayedImage; Size size = img.Size; location.X = Math.Max(Math.Min(location.X, size.Width - 1), 0); location.Y = Math.Max(Math.Min(location.Y, size.Height - 1), 0); mousePositionTextbox.Text = location.ToString(); if (_imageType == typeof(CvArray <>)) { MCvScalar scalar = CvInvoke.cvGet2D(img.Ptr, location.Y, location.X); colorIntensityTextbox.Text = BufferToString(scalar.ToArray(), img.NumberOfChannels); } else if (_imageType == typeof(Mat)) { Mat mat = img as Mat; byte[] raw = mat.GetData(location.Y, location.X); if (mat.Depth == DepthType.Cv8U) { colorIntensityTextbox.Text = BufferToString(raw, img.NumberOfChannels); } else if (mat.Depth == DepthType.Cv8S) { sbyte[] data = new sbyte[img.NumberOfChannels]; GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); Marshal.Copy(raw, 0, handle.AddrOfPinnedObject(), mat.ElementSize); handle.Free(); colorIntensityTextbox.Text = BufferToString(data, data.Length); } else if (mat.Depth == DepthType.Cv16S) { GCHandle handle = GCHandle.Alloc(raw, GCHandleType.Pinned); short[] data = new short[img.NumberOfChannels]; Marshal.Copy(handle.AddrOfPinnedObject(), data, 0, data.Length); handle.Free(); colorIntensityTextbox.Text = BufferToString(data, data.Length); } else if (mat.Depth == DepthType.Cv16U) { UInt16[] data = new UInt16[img.NumberOfChannels]; GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); Marshal.Copy(raw, 0, handle.AddrOfPinnedObject(), mat.ElementSize); handle.Free(); colorIntensityTextbox.Text = BufferToString(data, data.Length); } else if (mat.Depth == DepthType.Cv32F) { GCHandle handle = GCHandle.Alloc(raw, GCHandleType.Pinned); float[] floatData = new float[img.NumberOfChannels]; Marshal.Copy(handle.AddrOfPinnedObject(), floatData, 0, floatData.Length); handle.Free(); colorIntensityTextbox.Text = BufferToString(floatData, floatData.Length); } else if (mat.Depth == DepthType.Cv32S) { int[] data = new int[img.NumberOfChannels]; GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); Marshal.Copy(raw, 0, handle.AddrOfPinnedObject(), mat.ElementSize); handle.Free(); colorIntensityTextbox.Text = BufferToString(data, data.Length); } else if (mat.Depth == DepthType.Cv64F) { GCHandle handle = GCHandle.Alloc(raw, GCHandleType.Pinned); double[] doubleData = new double[img.NumberOfChannels]; Marshal.Copy(handle.AddrOfPinnedObject(), doubleData, 0, doubleData.Length); handle.Free(); colorIntensityTextbox.Text = BufferToString(doubleData, doubleData.Length); } else { colorIntensityTextbox.Text = String.Empty; } } else { colorIntensityTextbox.Text = String.Empty; } }