Beispiel #1
0
        public unsafe int StretchContrast(int stretchVal)
        {
            BitmapData sourceData = sourceImage.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            BitmapData processData = processImage.LockBits(new Rectangle(0, 0, processImage.Width, processImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            // set pointers
            uint* sourceP = (uint*)sourceData.Scan0;
            uint* processP = (uint*)processData.Scan0;

            GetImageContrast contrast = new GetImageContrast();
            int[] contr = contrast.Process(sourceP, processP, sourceImage.Size);

            label6.Text = "DiffContrast: " + Convert.ToString(contr[0]);
            label5.Text = "MaxDiffContrast: " + Convert.ToString(contr[1]);

            // filter the image
            ContrastStretchingFilter stretch = new ContrastStretchingFilter();
            stretch.difference = contr[0];
            stretch.Process(sourceP, processP, size, stretchVal);

            sourceImage.UnlockBits(sourceData);
            processImage.UnlockBits(processData);

            originalImage = new Bitmap(processImage);

            return Convert.ToInt32(stretch.greyOverall);
        }
        /// <summary>
        /// Detects the labels in the Bitmap
        /// </summary>
        /// <param name="source">Source Bitmap where the labels should be found</param>
        /// <returns>List of found labels</returns>
        public unsafe List<Rectangle> GetLabels(Bitmap source)
        {
            List<Rectangle> results = new List<Rectangle>();
            Size size = new Size(source.Width, source.Height);  // size of the image

            sourceImage = new Bitmap(source);   // normal image
            processImage = new Bitmap(source);  // image for detection

            BitmapData sourceData = sourceImage.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            BitmapData processData = processImage.LockBits(new Rectangle(0, 0, processImage.Width, processImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            // set pointers
            uint* sourceP = (uint*)sourceData.Scan0;
            uint* processP = (uint*)processData.Scan0;

            // filter the image
            ContrastStretchingFilter stretch = new ContrastStretchingFilter();
            stretch.Process(sourceP, processP, size);
            processImage.Save(@"C:\Temp\labels_stretch.png");

            CopyImage(processP, sourceP, size);

            SobelFilter sobel = new SobelFilter();
            sobel.SobelValue = sobelValue;
            sobel.Process(sourceP, processP, size);

            PixelExtractionFilter pixelExtraction = new PixelExtractionFilter();
            pixelExtraction.Process(sourceP, processP, size);

            // save image for debugging
            processImage.Save(@"C:\Temp\labels_0.png");

            DrawBlackBorder(processP, size);

            // override source bitmap with filtered bitmap
            CopyImage(processP, sourceP, size);

            // detect the labels
            LabelDetection detection = new LabelDetection();
            detection.Ratio = legalRatio;
            results = detection.Process(sourceP, processP, size);

            sourceImage.UnlockBits(sourceData);
            processImage.UnlockBits(processData);

            // save image for debugging
            sourceImage.Save(@"C:\Temp\labels_1.png");

            return results;
        }