public static FilteredImage GetNormalizedGrayscaleFilteredImage(Bitmap bitmap) { using (Bitmap bmp = new Bitmap(bitmap)) { int height = bitmap.Height; FilteredImageChannel[] channels = new FilteredImageChannel[1]; double[,] channel = new double[height, height]; for (int i = 0; i < bitmap.Height; i++) { for (int j = 0; j < bitmap.Height; j++) { Color color = bitmap.GetPixel(j, i); //redChannel[i, j] = color.R / 255.0; //greenChannel[i, j] = color.G / 255.0; //blueChannel[i, j] = color.B / 255.0; channel[i, j] = (255 - ((color.R + color.G + color.B) / 3)) / 255.0 - 0.5; } } channels[0] = new FilteredImageChannel(height, channel); FilteredImage image = new FilteredImage(1, channels); return(image); } }
private void ProcessImage_Side2() { Image <Hsv, byte> HSVImage; Image <Gray, byte> HSVMaskedImage; Image <Gray, Byte> FilteredImage; Image <Gray, byte> CannyImage; Image <Bgr, byte> DrawBound; VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); RotatedRect BoundingBox; int Rectcount; float MaxArea = 0; RotatedRect MaxRect = new RotatedRect(); HSVImage = OriginalImage.Convert <Hsv, byte>(); Hsv lowerLimit = new Hsv(trackBar_H_Low_3.Value, trackBar_S_Low_3.Value, trackBar_V_Low_3.Value); //50 80 Hsv upperLimit = new Hsv(trackBar_H_Up_3.Value, trackBar_S_Up_3.Value, trackBar_V_Up_3.Value); //50 80 HSVMaskedImage = HSVImage.InRange(lowerLimit, upperLimit); imageBox_HSV_3.Image = HSVMaskedImage; FilteredImage = HSVMaskedImage.SmoothMedian(25); if (checkBox_Filter_3.Checked) { imageBox_HSV_3.Image = FilteredImage; } else { imageBox_HSV_3.Image = HSVMaskedImage; } CannyImage = FilteredImage.Clone(); CvInvoke.Canny(FilteredImage, CannyImage, 255, 255, 5, true); DrawBound = OriginalImage.Clone(); CvInvoke.FindContours(CannyImage, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple); Rectcount = contours.Size; for (int i = 0; i < Rectcount; i++) { using (VectorOfPoint contour = contours[i]) { // 使用 BoundingRectangle 取得框選矩形 BoundingBox = CvInvoke.MinAreaRect(contour); if ((BoundingBox.Size.Width * BoundingBox.Size.Height) > MaxArea) { MaxArea = BoundingBox.Size.Width * BoundingBox.Size.Height; MaxRect = BoundingBox; } } } CvInvoke.Polylines(DrawBound, Array.ConvertAll(MaxRect.GetVertices(), Point.Round), true, new Bgr(Color.Red).MCvScalar, 3); CvInvoke.Line(DrawBound, new Point(0, HeightThreshold), new Point(OriginalImage.Width, HeightThreshold), new Bgr(Color.DeepPink).MCvScalar, 5); imageBox_Result_3.Image = DrawBound; SideRect_2 = MaxRect; }
protected virtual void Dispose(bool disposing) { if (disposing) { if (OriginalImage != null) { OriginalImage.Dispose(); OriginalImage = null; } if (ThumbnailImage != null) { ThumbnailImage.Dispose(); ThumbnailImage = null; } if (FilteredImage != null) { FilteredImage.Dispose(); FilteredImage = null; } } }
protected virtual void Dispose(bool disposing) { if (disposing) { if (ImagemOriginal != null) { ImagemOriginal.Dispose(); ImagemOriginal = null; } if (ImagemMiniatura != null) { ImagemMiniatura.Dispose(); ImagemMiniatura = null; } if (FilteredImage != null) { FilteredImage.Dispose(); FilteredImage = null; } } }
//private static RGBPixel[,] GetRGBMatrix(Bitmap image) //{ // int width = image.Width, height = image.Height; // RGBPixel[,] result = new RGBPixel[height, width]; // for (int i = 0; i < height; i++) // { // for (int j = 0; j < width; j++) // { // Color color = image.GetPixel(j, i); // result[i, j] = new RGBPixel(color.R, color.G, color.B); // } // } // return result; //} //private static Bitmap ReconstructFromRGBMatrix(RGBPixel[,] imageMatrix) //{ // int width = imageMatrix.GetLength(1), height = imageMatrix.GetLength(0); // Bitmap image = new Bitmap(width, height); // using (Graphics graph = Graphics.FromImage(image)) // { // Rectangle rectangle = new Rectangle(0, 0, width, height); // graph.FillRectangle(Brushes.White, rectangle); // } // for (int i = 0; i < height; i++) // { // for (int j = 0; j < width; j++) // { // image.SetPixel(j, i, Color.FromArgb((int)imageMatrix[i, j].Red, (int)imageMatrix[i, j].Green, (int)imageMatrix[i, j].Blue)); // } // } // return image; //} //public static Bitmap ReconstructFromNormalizedRGBMatrix(RGBPixel[,] imageMatrix) //{ // return ReconstructFromRGBMatrix(DenormalizeRGBMatrix(imageMatrix)); //} //private static RGBPixel[,] DenormalizeRGBMatrix(RGBPixel[,] imageMatrix) //{ // for (int i = 0; i < imageMatrix.GetLength(0); i++) // { // for (int j = 0; j < imageMatrix.GetLength(1); j++) // { // imageMatrix[i, j] = DenormalizePixel(imageMatrix[i, j]); // } // } // return imageMatrix; //} //private static double DenormalizeValue(double value) //{ // return (int)(value * 255); //} //private static RGBPixel DenormalizePixel(RGBPixel pixel) //{ // return new RGBPixel(DenormalizeValue(pixel.Red), DenormalizeValue(pixel.Green), DenormalizeValue(pixel.Blue)); //} //private static double NormalizeValue(double value) //{ // return value / 255.0; //} //private static RGBPixel NormalizePixel(RGBPixel pixel) //{ // return new RGBPixel(NormalizeValue(pixel.Red), NormalizeValue(pixel.Green), NormalizeValue(pixel.Blue)); //} //private static RGBPixel[,] NormalizeRGBMatrix(RGBPixel[,] imageMatrix) //{ // for(int i = 0; i < imageMatrix.GetLength(0); i++) // { // for(int j = 0; j < imageMatrix.GetLength(1); j++) // { // imageMatrix[i, j] = NormalizePixel(imageMatrix[i,j]); // } // } // return imageMatrix; //} //public static RGBPixel[,] GetNormalizedRGBMatrix(string path) //{ // using (Bitmap bmp = new Bitmap(path)) // { // return NormalizeRGBMatrix(GetRGBMatrix(bmp)); // } //} //public static RGBPixel[,] GetNormalizedRGBMatrix(Bitmap bitmap) //{ // using (Bitmap bmp = new Bitmap(bitmap)) // { // return NormalizeRGBMatrix(GetRGBMatrix(bmp)); // } //} public static FilteredImage GetNormalizedFilteredImage(Bitmap bitmap) { using (Bitmap bmp = new Bitmap(bitmap)) { int height = bitmap.Height; FilteredImageChannel[] channels = new FilteredImageChannel[3]; double[,] redChannel = new double[height, height]; double[,] greenChannel = new double[height, height]; double[,] blueChannel = new double[height, height]; for (int i = 0; i < bitmap.Height; i++) { for (int j = 0; j < bitmap.Height; j++) { Color color = bitmap.GetPixel(j, i); redChannel[i, j] = (255 - color.R) / 255.0 - 0.5; greenChannel[i, j] = (255 - color.G) / 255.0 - 0.5; blueChannel[i, j] = (255 - color.B) / 255.0 - 0.5; //redChannel[i, j] = (255 - color.R) / 255.0; //greenChannel[i, j] = (255 - color.G) / 255.0; //blueChannel[i, j] = (255 - color.B) / 255.0; } } channels[0] = new FilteredImageChannel(height, redChannel); channels[1] = new FilteredImageChannel(height, greenChannel); channels[2] = new FilteredImageChannel(height, blueChannel); FilteredImage image = new FilteredImage(3, channels); return(image); } }
public void UpdatePreview() { var clone = (MagickImage)FilteredImage.Clone(); LayerPreview = clone.GetImagePreview(this, new Size(0, 60)); }