Exemplo n.º 1
0
        private void kśrednichToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageSeg = new ImageSegmentation();

            Bitmap image = ImageSeg.Compute((Bitmap)this.pctbImage.Image);

            ImageSeg = null;

            SetImageAndHist(image);
        }
Exemplo n.º 2
0
        ////~~~~~~~~~~~~~~~~~~~~~~~~~ Some crucial methods
        ///
        private void analyze()
        {
            //Do different image segmentations, depending on the choices
            if (comboBox1.SelectedIndex > -1)
            {
                String select = comboBox1.Text;

                if (select == "K Clustering")
                {
                    //do the K clustering method

                    segmentation = "K Cluster";
                    //System.Windows.Forms.MessageBox.Show("No image imported");
                }

                else if (select == "Edge Detection")
                {
                    //have this done in edge detection

                    segmentation = "Edge Detection";
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("No Segmentation Type Selected.");
                    return;
                }
            }
            //first, the base case: we analyze when the user didn't put in a picture

            if (pic == null)
            {
                System.Windows.Forms.MessageBox.Show("No image imported");
                return;
            }
            else
            {
                if (segmentation == "K Cluster")
                {
                    //before doing the segmentation, check if "temp.jpg" exists and "Segmented Image" exists



                    //using the method from KMC.cs
                    if (searched == 0)
                    {
                        Bitmap temp = (Bitmap)pic;
                        //set the name of the files created from the segmentation
                        temp.Save("t3mp.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                        //temp.Dispose();
                        searched = 1;
                    }
                    //string directoryName = Path.GetDirectoryName("temp.jpg");
                    //string sourcePath = Directory.GetCurrentDirectory();


                    string path = Directory.GetCurrentDirectory();
                    //Now the color segmented images are in a folder called "Segmented Image"
                    string directory = path + "\\S3gmented Image";
                    //if there has not been K-clustering, skip the process and go to the API
                    if (!(System.IO.Directory.Exists(directory)))
                    {
                        //Trying Amanda's method
                        ImageSegmentation.Compute("t3mp.jpg");
                    }


                    files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
                    int numFiles = files.GetLength(0);

                    //if the user enters another term after the images were already sent to API, we don't need to send to API again
                    if (API_sent == 0)
                    {
                        ImageCollections = new string[numFiles];

                        int tempCounter = 0;
                        //array that collects the accurate terms list
                        API_results = new List <string> [numFiles];
                        //Now we go inside the Segmented Image folder and loop
                        foreach (string file in files)
                        {
                            ImageCollections[tempCounter] = file;
                            Google.Cloud.Vision.V1.Image integer = Google.Cloud.Vision.V1.Image.FromFile(ImageCollections[tempCounter]);
                            //Keeps track of the terms used
                            API_results[tempCounter] = Vision.containsElement(integer);
                            tempCounter++;
                        }
                        //so we don't have to loop over again. We already have the information
                        API_sent = 1;
                    }

                    //our algorithm is first loop over each picture and if the user_query match, save the matching image and the rank in the array
                    // then continue over and if the user_query match on the next file and if the rank in the array is lower (more front), switch the saved image
                    int rank = 999;

                    for (int i = 1; i < numFiles; i++)
                    {
                        int count = API_results[i].Count;
                        for (int k = 0; k < count; k++)
                        {
                            if (String.Equals(user_query, API_results[i][k]))
                            {
                                //found a better and more accurate image. higher ranked
                                if (rank > k)
                                {
                                    //want to free up our files, so we can delete later
                                    if (resultBitmap != null)
                                    {
                                        resultBitmap.Dispose();
                                    }
                                    resultBitmap = new Bitmap(ImageCollections[i]);
                                    rank         = k;
                                }
                            }
                        }
                    }
                    // no API term found
                    if (rank == 999)
                    {
                        System.Windows.Forms.MessageBox.Show("No search term of \"" + user_query + "\" found");
                    }


                    /*
                     * List<String> output = API_results[1];
                     * String printing = output[1];
                     *
                     * System.Windows.Forms.MessageBox.Show(printing.ToString());
                     */
                    //string name = ImageCollections[1];
                    //testNum = Vision.containsElement(ImageCollections[1], theText1);


                    //for the test purpose, get the second image that has been cropped

                    //resultBitmap = new Bitmap(ImageCollections[2]);
                    //Google.Cloud.Vision.V1.Image i = Google.Cloud.Vision.V1.Image.FromFile(ImageCollections[2]);
                    //testNum = Vision.containsElement(i, theText1);

                    //System.Windows.Forms.MessageBox.Show(testNum.ToString());
                    //pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                    if (resultBitmap != null)
                    {
                        pictureBox1.Image = resultBitmap;
                    }
                }
                //System.Windows.Forms.MessageBox.Show(segmentation);

                if (segmentation == "Edge Detection")
                {
                    if (resultBitmap != null)
                    {
                        resultBitmap.Dispose();
                    }
                    //System.Windows.Forms.MessageBox.Show("Here");

                    resultBitmap = Filter.ApplyFilter((Bitmap)pic, EdgeFilter);

                    //Image finaloutput = (Image)resultBitmap;
                    //pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                    //resultBitmap.UnlockBits
                    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

                    pictureBox1.Image = resultBitmap;
                }
            }
        }