Exemplo n.º 1
0
        //Find limits to contrast stretch an image\\ Using for default BW contrast
        //IntensityProcent - intensity in % pixels saturated at low and high intensities of image
        private static double[] StretchlimsCount(int[,] grayColor, double intensityProcent)
        {
            double[] imLH = new double[2]; //contatin a pair of gray values, which represent image low & high limits to contrast stretch an image
            double[] tol  = new double[2]; //tol saturates equal fractions at low and high pixel values

            if (intensityProcent == 0.01)
            {
                tol[0] = .01; //default
                tol[1] = .99; //default
            }
            else
            {
                tol[0] = intensityProcent;
                tol[1] = 1 - intensityProcent;
            }

            if (tol[0] < tol[1])                                // tol[0] - low, tol[1] - high
            {
                var ImHist = ContrastProcess.ImHist(grayColor); //obtain img histohram

                int[] CumulativeSum = new int[256];
                //CumulativeSum[0] = ImHist[0];
                for (int i = 0; i < 256; i++)
                {
                    if (i == 0)
                    {
                        CumulativeSum[i] = ImHist[i];
                    }
                    else
                    {
                        CumulativeSum[i] = ImHist[i] + CumulativeSum[i - 1];
                    }
                }

                //cumulative distribution function
                var cdf   = CumulativeSum.VectorToDouble().VectorDivByConst(ImHist.Sum());
                var ilow  = Array.IndexOf(cdf, cdf.First(x => x > tol[0]));  //index first low
                var ihigh = Array.IndexOf(cdf, cdf.First(x => x >= tol[1])); //index first high

                if (ilow == ihigh)                                           //this could happen if img is flat
                {
                    //no implementation exception c(: ImLH[0, 0] = 1; ImLH[1, 0] = 256;
                }
                else
                {
                    imLH[0] = ((double)ilow - 1) / 255; imLH[1] = ((double)ihigh - 1) / 255; //convert to range [0 1]
                }
            }
            else
            {
                imLH[0] = 0; imLH[1] = 1;
            }

            return(imLH);
        }
Exemplo n.º 2
0
        //if want use for RGB image, input Rc, Gc, Bc args. Trying for def lims such as for balckwhite image
        private static double[] StretchlimsCount(int[,] grayColor, double low, double high)
        {
            double[] imLH = new double[2];
            double[] tol  = { low, high };

            if (tol[0] < tol[1])                                // tol[0] - low, tol[1] - high
            {
                var ImHist = ContrastProcess.ImHist(grayColor); //obtain img histohram

                int[] CumulativeSum = new int[256];
                //CumulativeSum[0] = ImHist[0];
                for (int i = 0; i < 256; i++)
                {
                    if (i == 0)
                    {
                        CumulativeSum[i] = ImHist[i];
                    }
                    else
                    {
                        CumulativeSum[i] = ImHist[i] + CumulativeSum[i - 1];
                    }
                }

                //cumulative distribution function
                var cdf   = CumulativeSum.VectorToDouble().VectorDivByConst(ImHist.Sum());
                var ilow  = Array.IndexOf(cdf, cdf.First(x => x > tol[0]));  //index first low
                var ihigh = Array.IndexOf(cdf, cdf.First(x => x >= tol[1])); //index first high

                if (ilow == ihigh)                                           //this could happen if img is flat
                {
                    //no implementation exception c(: ImLH[0, 0] = 1; ImLH[1, 0] = 256;
                }
                else
                {
                    imLH[0] = ((double)ilow - 1) / 255; imLH[1] = ((double)ihigh - 1) / 255;
                }
            }
            else
            {
                imLH[0] = 0; imLH[1] = 1;
            }

            return(imLH);
        }