Esempio n. 1
0
        private List <Bitmap> CLHSL(Bitmap bmp)
        {
            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown)
            {
                CLCalc.InitCL();
            }
            if (CLCalc.CLAcceleration != CLCalc.CLAccelerationType.UsingCL)
            {
                return(null);
            }

            InitKernels();

            if (CLBmpSaturation == null)
            {
                CLBmpSaturation = new CLCalc.Program.Image2D(bmp);
                CLBmpIntens     = new CLCalc.Program.Image2D(bmp);
            }


            if (CLbmp == null || CLbmp.Height != bmp.Height || CLbmp.Width != bmp.Width)
            {
                CLbmp           = new CLCalc.Program.Image2D(bmp);
                CLNewBmp        = new CLCalc.Program.Image2D(bmp);
                CLBmpSaturation = new CLCalc.Program.Image2D(bmp);
                CLBmpIntens     = new CLCalc.Program.Image2D(bmp);
            }
            else
            {
                CLbmp.WriteBitmap(bmp);
                CLN.WriteToDevice(new int[] { NLumIntens });
                CLWidth.WriteToDevice(new int[] { bmp.Width });
                CLHeight.WriteToDevice(new int[] { bmp.Height });
            }

            kernelComputeHue.Execute(new CLCalc.Program.MemoryObject[] { CLbmp, CLNewBmp, CLBmpSaturation, CLBmpIntens }, new int[] { bmp.Width, bmp.Height });

            return(new List <Bitmap>()
            {
                CLNewBmp.ReadBitmap(), CLBmpSaturation.ReadBitmap(), CLBmpIntens.ReadBitmap()
            });
        }
Esempio n. 2
0
        /// <summary>Equalizes image histogram using OpenCL</summary>
        private void CLEqualizeHistogram(ref Bitmap bmp)
        {
            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown)
            {
                CLCalc.InitCL();
            }
            if (CLCalc.CLAcceleration != CLCalc.CLAccelerationType.UsingCL)
            {
                return;
            }

            float[] PartialHistograms = new float[NLumIntens * bmp.Width];
            float[] histLuminance     = new float[NLumIntens];

            if (kernelComputeHistograms == null || CLN == null || CLHistogram == null)
            {
                CLHistogram         = new CLCalc.Program.Variable(histLuminance);
                CLPartialHistograms = new CLCalc.Program.Variable(PartialHistograms);
            }
            InitKernels();


            System.Diagnostics.Stopwatch swTotal               = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch swCopyBmp             = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch swRescaling           = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch swComputeHistPartial  = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch swComputeHistConsolid = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch swHistIntegral        = new System.Diagnostics.Stopwatch();

            swTotal.Start();

            swCopyBmp.Start();
            if (CLbmp == null || CLbmp.Height != bmp.Height || CLbmp.Width != bmp.Width)
            {
                CLbmp               = new CLCalc.Program.Image2D(bmp);
                CLNewBmp            = new CLCalc.Program.Image2D(bmp);
                CLPartialHistograms = new CLCalc.Program.Variable(PartialHistograms);
            }
            else
            {
                CLbmp.WriteBitmap(bmp);
                CLN.WriteToDevice(new int[] { NLumIntens });
                CLWidth.WriteToDevice(new int[] { bmp.Width });
                CLHeight.WriteToDevice(new int[] { bmp.Height });
            }
            swCopyBmp.Stop();

            swComputeHistPartial.Start();

            //Partial histograms
            CLCalc.Program.MemoryObject[] args = new CLCalc.Program.MemoryObject[] { CLbmp, CLPartialHistograms, CLHeight, CLN };

            kernelComputeHistograms.Execute(args, bmp.Width);
            CLCalc.Program.Sync();
            swComputeHistPartial.Stop();



            swComputeHistConsolid.Start();

            args = new CLCalc.Program.MemoryObject[] { CLPartialHistograms, CLHistogram, CLHeight, CLN };
            kernelConsolidateHist.Execute(args, NLumIntens);

            CLHistogram.ReadFromDeviceTo(histLuminance);

            swComputeHistConsolid.Stop();


            swHistIntegral.Start();
            //Perform histogram integration - better performance in CPU
            //Compute histogram integrals in-place
            for (int i = 1; i < NLumIntens; i++)
            {
                histLuminance[i] += histLuminance[i - 1];
            }

            float scale = 0.9f / histLuminance[NLumIntens - 1];

            //Scales histograms
            for (int i = 0; i < NLumIntens; i++)
            {
                histLuminance[i] *= scale;
            }

            //Writes histogram integral
            CLHistogram.WriteToDevice(histLuminance);
            swHistIntegral.Stop();

            swRescaling.Start();
            //Computes equalized image
            args = new CLCalc.Program.MemoryObject[] { CLbmp, CLNewBmp, CLHistogram, CLN };
            kernelPerformNormalization.Execute(args, new int [] { bmp.Width, bmp.Height });

            bmp = CLNewBmp.ReadBitmap();
            swRescaling.Stop();


            swTotal.Stop();
        }