Example #1
0
        private void PreProcessImage(String tId)
        {
            // preprocess image to make it ready for FR
            // windows bitmap to MAT for processing
            cv::Mat imgProc = BitmapConverter.ToMat((Bitmap)imgOriginal.Image);

            // load classifiers
            String haarLocation         = System.IO.Path.GetDirectoryName(Application.StartupPath) + "\\haar\\";
            cv::CascadeClassifier faceC = new cv::CascadeClassifier(haarLocation + "haarcascade_frontalface_alt.xml");

            cv::Rect[] faces;

            faces = faceC.DetectMultiScale(imgProc, 1.1, 2, cv::HaarDetectionType.DoCannyPruning);

            // find biggest detected face
            int faceIdx = 0;

            if (faces.Length >= 1)
            {
                // Largest Face Found
                long size = 0;          long max = 0;
                for (int i = 0; i < faces.Length; i++)
                {
                    size = faces[i].Height * faces[i].Width;
                    if (size > max)
                    {
                        max     = size;
                        faceIdx = i;
                    }
                }

                cv::Mat          imgResize = imgProc.Clone(faces[faceIdx]);
                OpenCvSharp.Size newSize = new OpenCvSharp.Size(1280, 1280);
                imgResize.Resize(newSize);
                imgProcessed.Image = imgResize.ToBitmap();
                imgProcessed.Update();
                //imgOriginal.Refresh();
            }
            else
            {
                Console.WriteLine(String.Format("No Face Found TomisID {0}", tId));
                //MessageBox.Show("No Face Found", "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Example #2
0
        private void BtnWebCamStart_Click(object sender, EventArgs e)
        {
            bool done = false;

            this.Enabled = false;
            this.Refresh();

            cv::VideoCapture capDev     = new cv::VideoCapture(0);
            cv::Window       vidPreview = new cv::Window("Video Preview");
            cv::Mat          imgRaw     = new cv::Mat();

            while (!done)
            {
                capDev.Read(imgRaw); // same as cvQueryFrame
                if (!imgRaw.Empty())
                {
                    vidPreview.ShowImage(imgRaw);
                    switch (cv::Cv2.WaitKey(100))
                    {
                    case 13:
                        imgOriginal.Image = imgRaw.ToBitmap();
                        done = true;
                        break;

                    case 27:
                        done = true;
                        break;
                    }
                }
            }
            vidPreview.Close();
            vidPreview.Dispose();
            imgRaw.Dispose();
            capDev.Dispose();
            this.Enabled = true;
            this.Refresh();
            PreProcessImage("WebCam");
        }
Example #3
0
        private void Image <Bgr, byte> PerformRandomSpraysRetinex(Image <Bgr, byte> source, int N, int n, double upperBound, int rowsStep, int colsStep)
        {
            int rows = source.Height;
            int cols = source.Width;

            int R = sqrt((double)(rows * rows + cols * cols)) + 0.5;

            int           spraysCount = 1000 * N;
            cv::Point2i **sprays      = CreateSprays(spraysCount, n, R);

            cv::Mat normalized;

            source.convertTo(normalized, CV_64FC3);

            int outputRows = rows / rowsStep;
            int outputCols = cols / colsStep;

            destination = cv::Mat(outputRows, outputCols, CV_64FC3);

            cv::Vec3d *input       = (cv::Vec3d *)normalized.data;
            cv::Vec3d *inputPoint  = input;
            cv::Vec3d *output      = (cv::Vec3d *)destination.data;
            cv::Vec3d *outputPoint = output;

            cv::RNG random;

            cv::Mat certainity = cv::Mat::zeros(rows, cols, CV_64FC1);

            for (int outputRow = 0; outputRow < outputRows; ++outputRow)
            {
                for (int outputCol = 0; outputCol < outputCols; ++outputCol)
                {
                    int row = outputRow * rowsStep;
                    int col = outputCol * colsStep;

                    inputPoint  = input + row * cols + col;
                    outputPoint = output + outputRow * outputCols + outputCol;

                    cv::Vec3d&currentPoint = *inputPoint;
                    cv::Vec3d&finalPoint   = *outputPoint;
                    finalPoint = cv::Vec3d(0, 0, 0);

                    for (int i = 0; i < N; ++i)
                    {
                        int       selectedSpray = random.uniform(0, spraysCount);
                        cv::Vec3d max           = cv::Vec3d(0, 0, 0);

                        for (int j = 0; j < n; ++j)
                        {
                            int newRow = row + sprays[selectedSpray][j].y;
                            int newCol = col + sprays[selectedSpray][j].x;

                            if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols)
                            {
                                cv::Vec3d&newPoint = input[newRow * cols + newCol];

                                for (int k = 0; k < 3; ++k)
                                {
                                    if (max[k] < newPoint[k])
                                    {
                                        max[k] = newPoint[k];
                                    }
                                }
                            }
                        }

                        for (int k = 0; k < 3; ++k)
                        {
                            finalPoint[k] += currentPoint[k] / max[k];
                        }
                    }

                    finalPoint /= N;

                    for (int i = 0; i < 3; ++i)
                    {
                        if (finalPoint[i] > 1)
                        {
                            finalPoint[i] = 1;
                        }
                    }
                }
            }

            double scaleFactor = upperBound;

            if (rowsStep > 1 || colsStep > 1)
            {
                resize(destination, destination, source.size());
            }

            destination = destination * scaleFactor - 1;

            destination.convertTo(destination, source.type());

            DeleteSprays(sprays, spraysCount);
        }
Example #4
0
        public cv.Mat Run(cv::Mat m)
        {
            bool   showIntermediate = false; // turn this on if further detail is needed.
            double d = MatDeterminant(m);

            if (Math.Abs(d) < 1.0e-5)
            {
                if (showIntermediate)
                {
                    Console.WriteLine("\nMatrix has no inverse");
                }
                else
                if (showIntermediate)
                {
                    Console.WriteLine("\nDet(m) = " + d.ToString("F4"));
                }
            }

            inverse = MatInverse(m);

            cv.Mat prod = MatProduct(m, inverse);
            if (showIntermediate)
            {
                Console.WriteLine("\nThe product of m * inv is ");
                MatShow(prod, 1, 6);
            }

            cv.Mat lum;
            int[]  perm;
            int    toggle = MatDecompose(m, out lum, out perm);

            if (showIntermediate)
            {
                Console.WriteLine("\nThe combined lower-upper decomposition of m is");
                MatShow(lum, 4, 8);
            }

            cv.Mat lower = ExtractLower(lum);
            cv.Mat upper = ExtractUpper(lum);

            if (showIntermediate)
            {
                solution = MatVecProd(inverse, bVector);  // (1, 0, 2, 1)
                Console.WriteLine("\nThe lower part of LUM is");
                MatShow(lower, 4, 8);

                Console.WriteLine("\nThe upper part of LUM is");
                MatShow(upper, 4, 8);

                Console.WriteLine("\nThe perm[] array is");
                VecShow(perm, 4);

                cv.Mat lowUp = MatProduct(lower, upper);
                Console.WriteLine("\nThe product of lower * upper is ");
                MatShow(lowUp, 4, 8);

                Console.WriteLine("\nVector b = ");
                VecShow(bVector, 1, 8);

                Console.WriteLine("\nSolving m*x = b");

                Console.WriteLine("\nSolution x = ");
                VecShow(solution, 1, 8);
            }
            return(inverse);
        }
Example #5
0
        public void Image <Bgr, byte> PerformLightRandomSpraysRetinex(Image <Bgr, byte> source, int N, int n, int inputKernelSize, double inputSigma, int intensityChangeKernelSize, double intensityChangeSigma, int rowsStep, int colsStep, bool normalizeIntensityChange, double upperBound)
        {
            Image <Bgr, byte> inputSource;
            Image <Bgr, byte> inputRetinex;
            Image <Bgr, byte> retinex;

            PerformRandomSpraysRetinex(source, retinex, N, n, upperBound, rowsStep, colsStep);

            source.convertTo(inputSource, CV_64FC3);
            retinex.convertTo(inputRetinex, CV_64FC3);

            if (normalizeIntensityChange)
            {
                Image <Bgr, byte> illuminant;

                cvdivide(inputSource, inputRetinex, illuminant);
                std::vector <cv::Mat> illuminantChannels;

                split(illuminant, illuminantChannels);
                cv::Mat illuminantAverage = (illuminantChannels[0] + illuminantChannels[1] + illuminantChannels[2]) / 3;
                for (int i = 0; i < 3; ++i)
                {
                    cv::divide(illuminantChannels[i], illuminantAverage, illuminantChannels[i]);
                }
                cv::merge(illuminantChannels, illuminant);

                inputSource = inputRetinex.mul(illuminant);
            }

            if (inputKernelSize > 1)
            {
                if (inputSigma == 0.0)
                {
                    cv::Mat averaging = cv::Mat::ones(inputKernelSize, inputKernelSize, CV_64FC1) / (double)(inputKernelSize * inputKernelSize);
                    Filter64F(inputSource, inputSource, inputKernelSize);
                    Filter64F(inputRetinex, inputRetinex, inputKernelSize);
                }
                else
                {
                    GaussianBlur(inputSource, inputSource, cv::Size(inputKernelSize, inputKernelSize), inputSigma);
                    GaussianBlur(inputRetinex, inputRetinex, cv::Size(inputKernelSize, inputKernelSize), inputSigma);
                }
            }

            cv::Mat illuminant;

            divide(inputSource, inputRetinex, illuminant);
            std::vector <cv::Mat> illuminantChannels;

            if (intensityChangeKernelSize > 1)
            {
                if (intensityChangeSigma == 0.0)
                {
                    cv::Mat averaging = cv::Mat::ones(intensityChangeKernelSize, intensityChangeKernelSize, CV_64FC1) / (double)(intensityChangeKernelSize * intensityChangeKernelSize);
                    Filter64F(illuminant, illuminant, intensityChangeKernelSize);
                }
                else
                {
                    GaussianBlur(illuminant, illuminant, cv::Size(intensityChangeKernelSize, intensityChangeKernelSize), intensityChangeSigma);
                }
            }

            std::vector <cv::Mat> destinationChannels;

            split(source, destinationChannels);
            split(illuminant, illuminantChannels);
            for (int i = 0; i < (int)destinationChannels.size(); ++i)
            {
                destinationChannels[i].convertTo(destinationChannels[i], CV_64FC1);
                cv::divide(destinationChannels[i], illuminantChannels[i], destinationChannels[i]);
            }

            cv::merge(destinationChannels, destination);

            double *check = (double *)destination.data;

            for (int i = 0; i < destination.rows * destination.cols * 3; ++i)
            {
                if (check[i] >= upperBound)
                {
                    check[i] = upperBound - 1;
                }
            }

            destination.convertTo(destination, source.type());
        }