public HSVImage StretchContrast(HSVImage image, float min, float max)
        {
            var result = new HSVImage(image.Width, image.Height);

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    result.Hue[x, y]        = image.Hue[x, y];
                    result.Saturation[x, y] = image.Saturation[x, y];

                    result.Value[x, y] = (image.Value[x, y] - min) / (max - min);

                    if (result.Value[x, y] > 1)
                    {
                        result.Value[x, y] = 1;
                    }
                    else if (result.Value[x, y] < 0)
                    {
                        result.Value[x, y] = 0;
                    }
                }
            }

            return(result);
        }
Example #2
0
        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;
        }
        public HSVImage ChangeBrightness(HSVImage image, float brightnessDiffrence)
        {
            var hsvImage = new HSVImage(image.Width, image.Height);

            for (int x = 0; x < image.Width; ++x)
            {
                for (int y = 0; y < image.Height; ++y)
                {
                    var hsvColor = image.GetPixel(x, y);
                    hsvColor.Value += brightnessDiffrence;
                    hsvColor.Value  = hsvColor.Value > 1 ? 1 : hsvColor.Value < 0 ? 0 : hsvColor.Value;
                    hsvImage.SetPixel(x, y, hsvColor);
                }
            }

            return(hsvImage);
        }
Example #4
0
        public HSVImage ChangeSaturation(HSVImage image, float saturationGain)
        {
            var hsvImage = new HSVImage(image.Width, image.Height);

            for (int x = 0; x < image.Width; ++x)
            {
                for (int y = 0; y < image.Height; ++y)
                {
                    var pixel = image.GetPixel(x, y);
                    var sat   = pixel.Saturation + saturationGain;
                    sat = sat > 1 ? 1 : sat < 0 ? 0 : sat;
                    var hsvColor = new HSVColor(pixel.Hue, sat, pixel.Value);
                    hsvImage.SetPixel(x, y, hsvColor);
                }
            }

            return(hsvImage);
        }
        public HSVImage ChangeTint(HSVImage image, float tint)
        {
            var hsvImage = new HSVImage(image.Width, image.Height);

            for (int x = 0; x < image.Width; ++x)
            {
                for (int y = 0; y < image.Height; ++y)
                {
                    var pixel = image.GetPixel(x, y);
                    var h     = pixel.Hue + tint;
                    h = h > 360 ? h - 360 : h < 0 ? 360 + h : h;
                    var hsvColor = new HSVColor(h, pixel.Saturation, pixel.Value);
                    hsvImage.SetPixel(x, y, hsvColor);
                }
            }

            return(hsvImage);
        }
        public HSVImage BitmapToHSVImage(Bitmap image)
        {
            var hsvImage = new HSVImage(image.Width, image.Height);
            var bitmap   = image.Clone(new Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            System.Drawing.Imaging.BitmapData bitmapData =
                bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);

            IntPtr ptr    = bitmapData.Scan0;
            int    stride = bitmapData.Stride;

            unsafe
            {
                byte *p      = (byte *)(void *)ptr;
                int   offset = stride - bitmap.Width * 3;
                int   width  = bitmap.Width * 3;

                RGBColor rgbColor = new RGBColor();
                for (int y = 0; y < bitmap.Height; ++y)
                {
                    for (int x = 0; x < bitmap.Width; ++x)
                    {
                        rgbColor.Blue  = p[0];
                        rgbColor.Green = p[1];
                        rgbColor.Red   = p[2];

                        var hsvColor = ColorConverter.RGBToHSV(rgbColor);
                        hsvImage.SetPixel(x, y, hsvColor);
                        p += 3;
                    }
                    p += offset;
                }
            }

            bitmap.UnlockBits(bitmapData);

            return(hsvImage);
        }
        public Bitmap HSVImageToBitmap(HSVImage image)
        {
            var bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);

            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            System.Drawing.Imaging.BitmapData bitmapData =
                bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            IntPtr ptr    = bitmapData.Scan0;
            int    stride = bitmapData.Stride;

            unsafe
            {
                byte *p      = (byte *)(void *)ptr;
                int   offset = stride - image.Width * 3;
                int   width  = image.Width * 3;

                for (int y = 0; y < image.Height; ++y)
                {
                    for (int x = 0; x < image.Width; ++x)
                    {
                        var hsvColor = image.GetPixel(x, y);
                        var rgbColor = ColorConverter.HSVToRGB(hsvColor);

                        p[0] = rgbColor.Blue;
                        p[1] = rgbColor.Green;
                        p[2] = rgbColor.Red;

                        p += 3;
                    }
                    p += offset;
                }
            }

            bitmap.UnlockBits(bitmapData);

            return(bitmap);
        }