Visual C# Developer Center > Visual C# Forums > Visual C# Language > getvalue from string of binary to compare the value
Ask a questionAsk a question
 

Answergetvalue from string of binary to compare the value

  • Wednesday, November 04, 2009 4:06 AMaznimah Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    <!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin-top:0in; margin-right:0in; margin-bottom:10.0pt; margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} .MsoPapDefault {mso-style-type:export-only; margin-bottom:10.0pt; line-height:115%;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

    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.

    i'm using visual c# language

    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;
    }
    // read and open first image
    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());
    }
    }
    // convert first image to binary data
    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;
    }

    // read and open second image
    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());
    }
    }

    // convert second image to binary data
    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;
    }

    // compare the images with algoritm that use binary data for computation.
    private void button3_Click(object sender, EventArgs e)
    {
    progressBar1.Visible = true;

    string img1_ref, img2_ref;
    img1 = new Bitmap (texto);
    img2 = new Bitmap (textb);


    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();

    }



    }
    }
    }

Answers

  • Wednesday, November 04, 2009 5:39 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    I'm guessing your problem is in the two red lines from your code. You don't need to convert it to a bitmap, you have both images as one long string of zeros and ones. Just compare those.

    Also your code to convert it to a string is going to be very memory intensive and slow. Use StringBuilder rather than adding strings, and unless there is some requirement to do so, leave the \r\n stuff out. If there is a requirement for that, remember to skip them in your comparison code.

    Comparison code:
    int p = 0;
    int q = 0;
    int r = 0;
    
    for (int i = 0; i < texto.Length; i++) {
        if (texto[i] == '1') {
            if (textb[i] == '1') {
                p++;
            } else {
                q++;
            }
        } else if (textb[i] == '1') {
            r++;
        }
    }
    
    double jd = ((double)q + r)/(p + q + r);
    

    Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit
  • Wednesday, November 04, 2009 11:12 AMLouis.fr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    img1_ref = img1.GetPixel(i, j).ToString();
    img2_ref = img2.GetPixel(i, j).ToString();
    
    if (img1_ref == "1" && img2_ref == "1")
    ...
    
    

    GetPixel returns a Color. Color.ToString() will not return "1" or "0"

All Replies

  • Wednesday, November 04, 2009 5:39 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    I'm guessing your problem is in the two red lines from your code. You don't need to convert it to a bitmap, you have both images as one long string of zeros and ones. Just compare those.

    Also your code to convert it to a string is going to be very memory intensive and slow. Use StringBuilder rather than adding strings, and unless there is some requirement to do so, leave the \r\n stuff out. If there is a requirement for that, remember to skip them in your comparison code.

    Comparison code:
    int p = 0;
    int q = 0;
    int r = 0;
    
    for (int i = 0; i < texto.Length; i++) {
        if (texto[i] == '1') {
            if (textb[i] == '1') {
                p++;
            } else {
                q++;
            }
        } else if (textb[i] == '1') {
            r++;
        }
    }
    
    double jd = ((double)q + r)/(p + q + r);
    

    Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit
  • Wednesday, November 04, 2009 11:12 AMLouis.fr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    img1_ref = img1.GetPixel(i, j).ToString();
    img2_ref = img2.GetPixel(i, j).ToString();
    
    if (img1_ref == "1" && img2_ref == "1")
    ...
    
    

    GetPixel returns a Color. Color.ToString() will not return "1" or "0"

  • Thursday, November 26, 2009 5:48 AMaznimah Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    thanks for the reply Ron Whittle, i really appreciate it ;D

    what about the comparison in 2dimensional array or byte[]? can you elaborate more?
  • Thursday, November 26, 2009 6:10 AMaznimah Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hi Ron whittle, your code is work. i'm already try your code in my program. to get a correct result, i have done manually where i calculate by myself and thanks for the code since i return the same result that i have done manually ;D