getvalue from string of binary to compare the value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aznimah
    New Member
    • Nov 2009
    • 19

    getvalue from string of binary to compare the value

    hi, i'm work on image comparison. i'm using the similarity measurement which i need to:
    1) convert the image into the binary form since the algorithm that i've use works with binary data for the computation
    2) compare the string binary data to get the similarity or dissimilarity result.

    The problem is, i already done with the image (jpg) conversion to binary and also try the algorithm structure in C# language, but i having a problem to getvalue from the string of binary to pass with the comparison function where i declare as Bitmap since the comparison took the height and width of the image first. Then the comparison is between each rows of the two image.

    example:

    Image Binary Values (row operation)
    Image1 1 1 1 1
    Image2 0 1 0 0

    the calculation is to compare the strings of binary from the two image.
    p = positive both image ---> [1 1]
    q = positive image1 , negative image2 ---> [1 0]
    r = negative image1, positive image1 ---> [0 1]
    therefore,

    results from the image comparison above :
    p = 1
    q = 3
    r = 0

    i need to get the result example above so that i can calculate to measure the similarity between image.

    i really new to this language and i really appreciate with the help. i'm also attach the code that i already done and sample of interface from my program. thank you.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace ShapeRecognition
    {
        public partial class Form1 : Form
        {
            Bitmap fname1, fname2;
            Bitmap img1, img2;
            String textb = "";
            String texto = "";
            float p, q, r, jd;
            int sum1 = 0, sum2 = 0, sum3 = 0;
            bool flag = true;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                progressBar1.Visible = false;
            }
    
            private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                openFileDialog1.FileName = "";
                openFileDialog1.Title = "Images";
                openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png";//
                openFileDialog1.ShowDialog();
                if (openFileDialog1.FileName.ToString() != "")
                {
                    Box1.ImageLocation = openFileDialog1.FileName.ToString();
                    fname1 = new Bitmap(openFileDialog1.FileName.ToString());
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
             
    
                for (int i = 0; i < fname1.Height; i++)
                {
                    for (int j = 0; j < fname1.Width; j++)
                    {
                        if (fname1.GetPixel(j, i).A.ToString() == "255" && fname1.GetPixel(j, i).B.ToString() == "255" && fname1.GetPixel(j, i).G.ToString() == "255" && fname1.GetPixel(j, i).R.ToString() == "255")
                        {
                            texto = texto + "0";
    
                        }
                        else
                        {
                            texto = texto + "1";
    
    
                        }
    
                    }
                    texto = texto + "\r\n";
                }
    
                text1.Text = texto;
            }
    
            private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                openFileDialog2.FileName = "";
                openFileDialog2.Title = "Images";
                openFileDialog2.Filter = "All Images|*.jpg; *.bmp; *.png";//
                openFileDialog2.ShowDialog();
                if (openFileDialog1.FileName.ToString() != "")
                {
                    Box2.ImageLocation = openFileDialog2.FileName.ToString();
                    fname2 = new Bitmap(openFileDialog2.FileName.ToString());
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
               
                for (int i = 0; i < fname2.Height; i++)
                {
                    for (int j = 0; j < fname2.Width; j++)
                    {
                        if (fname2.GetPixel(j, i).A.ToString() == "255" && fname2.GetPixel(j, i).B.ToString() == "255" && fname2.GetPixel(j, i).G.ToString() == "255" && fname2.GetPixel(j, i).R.ToString() == "255")
                        {
                            textb = textb + "0";
    
                        }
                        else
                        {
                            textb = textb + "1";
    
    
                        }
    
                    }
                    textb = textb + "\r\n";
                }
    
                text2.Text = textb;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                progressBar1.Visible = true;
    
                string img1_ref, img2_ref;
             [B]   img1 = new Bitmap (texto);
                img2 = new Bitmap (textb);[/B]
    
            progressBar1.Maximum = img1.Width;
    
      if (img1.Width == img2.Width && img1.Height == img2.Height)
         {
          for (int i = 0; i < img1.Width; i++)
           {
             for (int j = 0; j < img1.Height; j++)
              {
              
               img1_ref = img1.GetPixel(i, j).ToString();
               img2_ref = img2.GetPixel(i, j).ToString();
                  
                          
                if (img1_ref == "1" && img2_ref == "1")
                   {
                    sum1++;
                   }
    
                  else if (img1_ref =="1" && img2_ref =="0")
                   {
                    sum2++;
                   }
    
                  else if (img1_ref =="0" && img2_ref =="1")
                   {
                    sum3++;
                   }
    
              } 
    
             
        p = sum1;
        q = sum2;
        r = sum3;
    
        jd = (q + r) / (p + q + r);
       
    
        if (jd < 0.75 )
          MessageBox.Show ("Images are dissimilar," +jd+ "ratio results");
    
        else if ( jd == 0.75 || jd <= 1)
          MessageBox.Show ("Images are similar," +jd+ "ratio results");
          }
          
        
         MessageBox.Show("can not compare this images");
    
         this.Dispose();
    
        }
    
    
            
            }
        }
    }
    Attached Files
    Last edited by tlhintoq; Nov 4 '09, 02:28 PM. Reason: [
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Bitmaps don't just create from a string.
      You either need a full stream of bytes that describe the bitmap, or you need to create a new bitmap and set every pixel based on the data string

      Comment

      • aznimah
        New Member
        • Nov 2009
        • 19

        #4
        getvalue from the string of binary to compare the value

        thanks Plater but i really need help more elaboration about your suggestion, could you explain more? i'm sorry and i really appreciate it.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Step 1: Open MSDN and actually LOOK at the bitmap object.
          Step 2: Step one should cover it

          EDIT: Ok, that's not really fair of me I suppose.

          The bitmap object can be created from a stream. If you have a byte[], you can turn it into a stream with the MemoryStream, which can be accepted by the Bitmap object.

          Otherwise, you create a bitmap of the correct size and then cycle through your data, decide what pixel the data is for, pick the color and the set the pixel in the bitmap.

          Comment

          • aznimah
            New Member
            • Nov 2009
            • 19

            #6
            getvalue from string of binary to compare the value

            hi Plater, thanks for the feedback.alread y search and do what you have been suggest for. i also have try another code. i manage to get value from the string of binary and run through the algorithm. but another problem occur where once the result appear, suppose i got the to total answer is 64 but the result shows 32.

            --> i have done the tracing to check where the error occur, the looping where comparison function produce incorrect result. the comparison of first row is correct but when it loop, the next row produce wrong result and so on.

            --> i really need help since i need to present this testing sooner by tomorrow. could you help me?

            --> sorry once again and i really wait for the reply. thank you so much.
            --> here i attach the new code.
            Code:
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Windows.Forms;
            using System.IO;
            
            namespace JaccardRecognition
            {
                public partial class shape : Form
                {
            
            
                    Bitmap Image1, Image2;
                    String textImage1 = "";
                    String textImage2 = "";
                    float a, b, p, q, r, jd;
                    int sum1 = 0, sum2 = 0, sum3 = 0;
                    
            
            
            
                    public shape()
                    {
                        InitializeComponent();
                    }
            
                    private void Form1_Load(object sender, EventArgs e)
                    {
            
                    }
            
                    /*Open and read the first image*/
            
                    private void OpenFirstImageBttn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
                    {
                        openFileDialog1.FileName = "";
                        openFileDialog1.Title = "Images";
                        openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png";//
                        openFileDialog1.ShowDialog();
                        if (openFileDialog1.FileName.ToString() != "")
                        {
                            Box1.ImageLocation = openFileDialog1.FileName.ToString();
                            Image1 = new Bitmap(openFileDialog1.FileName.ToString());
                            string h = Convert.ToString(Image1.Height);
                            string w = Convert.ToString(Image1.Width);
                            displayh.Text = h;
                            displayw.Text = w;
                        }
            
                    }
            
            
            
                    /*Covert first image into binary*/
            
                    private void convertImage1_Click(object sender, EventArgs e)
                    {
            
                        for (int i = 0; i < Image1.Height; i++)
                        {
                            for (int j = 0; j < Image1.Width; j++)
                            {
                                if (Image1.GetPixel(j, i).A.ToString() == "255" && Image1.GetPixel(j, i).B.ToString() == "255" && Image1.GetPixel(j, i).G.ToString() == "255" && Image1.GetPixel(j, i).R.ToString() == "255")
                                {
                                    textImage1 = textImage1 + "0";
            
                                }
                                else
                                {
                                    textImage1 = textImage1 + "1";
            
                                }
            
                            }
                            textImage1 = textImage1 + "\r\n";
                        }
            
                        text1.Text = textImage1;
                    }
            
                    /*Open and read the second image*/
            
                    private void OpenSecondImageBttn_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
                    {
                        openFileDialog2.FileName = "";
                        openFileDialog2.Title = "Images";
                        openFileDialog2.Filter = "All Images|*.jpg; *.bmp; *.png";//
                        openFileDialog2.ShowDialog();
                        if (openFileDialog1.FileName.ToString() != "")
                        {
                            Box2.ImageLocation = openFileDialog2.FileName.ToString();
                            Image2 = new Bitmap(openFileDialog2.FileName.ToString());
                        }
                    }
            
            
            
                    /*Covert second image into binary*/
            
                    private void convertImage2_Click(object sender, EventArgs e)
                    {
            
            
                        for (int i = 0; i < Image2.Height; i++)
                        {
                            for (int j = 0; j < Image2.Width; j++)
                            {
                                if (Image2.GetPixel(j, i).A.ToString() == "255" && Image2.GetPixel(j, i).B.ToString() == "255" && Image2.GetPixel(j, i).G.ToString() == "255" && Image2.GetPixel(j, i).R.ToString() == "255")
                                {
                                    textImage2 = textImage2 + "0";
            
                                }
                                else
                                {
                                    textImage2 = textImage2 + "1";
            
                                }
            
                            }
                            textImage2 = textImage2 + "\r\n";
                        }
            
                        text2.Text = textImage2;
                    }
            
                    /*This will compare both image from the binary data to check the similatiry measure */
                    /*Retrieve data in binary form to compute
                     --> The image size 16x16 pixels
                     --> looping the image 16 times where the comparison between in rows operation*/
            
                   [B] private void button3_Click(object sender, EventArgs e)
                    {
            
                        string output1 = "";
                        string output2 = "";
            
                        string img1_ref = "";
                        string img2_ref = "";
            
                        output1 = Convert.ToString(textImage1);
                        output2 = Convert.ToString(textImage2);
            
            
                        if (output1.Length == output2.Length)
                        {
            
                            for (int i = 0; i < 16; i++) // looping the height image
                            {
                                for (int j = 0; j < 16; j++) // looping the length image character
                                {
                                    img1_ref = output1[j].ToString();
                                    img2_ref = output2[j].ToString();
            
            
                                    if (img1_ref == "1" && img2_ref == "1")
                                    {
                                        sum1++;
                                    }
            
                                    else if (img1_ref == "1" && img2_ref == "0")
                                    {
                                        sum2++;
                                    }
            
                                    else if (img1_ref == "0" && img2_ref == "1")
                                    {
                                        sum3++;
                                    }
            
                                }
            
                            }
            
                            /* Jaccard measurement*/
                            p = sum1;
                            q = sum2;
                            r = sum3;
            
                            a = q + r;
                            b = p + q + r;
                            jd = a / b;
            
                            string valueP = Convert.ToString(p);
                            string valueQ = Convert.ToString(q);
                            string valueR = Convert.ToString(r);
                            string valueA = Convert.ToString(a);
                            string valueB = Convert.ToString(b);
                            text3.Text = valueP;
                            text4.Text = valueQ;
                            text5.Text = valueR;
                            textqr.Text = valueA;
                            textpqr.Text = valueB;
            
            
                            if (jd == 0 || jd < 0.15)
                            {
                                MessageBox.Show("Images are similar,ratio results:" + jd);
            
                            }
                            else if (jd == 0.15 || jd <= 1)
                            {
                                MessageBox.Show("Images are dissimilar,ratio results:" + jd);
            
                            }
            
                            else
                                MessageBox.Show("can not compare this images");
            
                        }
            [/B]
                        this.Dispose();
            
                    }
            
            
                }
            }

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Well I think going the byte[] route would be a better choice, bitmap data sometimes has padded bytes to each "row", there's a term for it but I forget it.

              Comment

              Working...