469,646 Members | 1,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

getvalue from string of binary to compare the value

19
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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace ShapeRecognition
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Bitmap fname1, fname2;
  16.         Bitmap img1, img2;
  17.         String textb = "";
  18.         String texto = "";
  19.         float p, q, r, jd;
  20.         int sum1 = 0, sum2 = 0, sum3 = 0;
  21.         bool flag = true;
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.             progressBar1.Visible = false;
  30.         }
  31.  
  32.         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  33.         {
  34.             openFileDialog1.FileName = "";
  35.             openFileDialog1.Title = "Images";
  36.             openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png";//
  37.             openFileDialog1.ShowDialog();
  38.             if (openFileDialog1.FileName.ToString() != "")
  39.             {
  40.                 Box1.ImageLocation = openFileDialog1.FileName.ToString();
  41.                 fname1 = new Bitmap(openFileDialog1.FileName.ToString());
  42.             }
  43.         }
  44.  
  45.         private void button1_Click(object sender, EventArgs e)
  46.         {
  47.  
  48.  
  49.             for (int i = 0; i < fname1.Height; i++)
  50.             {
  51.                 for (int j = 0; j < fname1.Width; j++)
  52.                 {
  53.                     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")
  54.                     {
  55.                         texto = texto + "0";
  56.  
  57.                     }
  58.                     else
  59.                     {
  60.                         texto = texto + "1";
  61.  
  62.  
  63.                     }
  64.  
  65.                 }
  66.                 texto = texto + "\r\n";
  67.             }
  68.  
  69.             text1.Text = texto;
  70.         }
  71.  
  72.         private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  73.         {
  74.             openFileDialog2.FileName = "";
  75.             openFileDialog2.Title = "Images";
  76.             openFileDialog2.Filter = "All Images|*.jpg; *.bmp; *.png";//
  77.             openFileDialog2.ShowDialog();
  78.             if (openFileDialog1.FileName.ToString() != "")
  79.             {
  80.                 Box2.ImageLocation = openFileDialog2.FileName.ToString();
  81.                 fname2 = new Bitmap(openFileDialog2.FileName.ToString());
  82.             }
  83.         }
  84.  
  85.         private void button2_Click(object sender, EventArgs e)
  86.         {
  87.  
  88.             for (int i = 0; i < fname2.Height; i++)
  89.             {
  90.                 for (int j = 0; j < fname2.Width; j++)
  91.                 {
  92.                     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")
  93.                     {
  94.                         textb = textb + "0";
  95.  
  96.                     }
  97.                     else
  98.                     {
  99.                         textb = textb + "1";
  100.  
  101.  
  102.                     }
  103.  
  104.                 }
  105.                 textb = textb + "\r\n";
  106.             }
  107.  
  108.             text2.Text = textb;
  109.         }
  110.  
  111.         private void button3_Click(object sender, EventArgs e)
  112.         {
  113.             progressBar1.Visible = true;
  114.  
  115.             string img1_ref, img2_ref;
  116.             img1 = new Bitmap (texto);
  117.             img2 = new Bitmap (textb);
  118.  
  119.         progressBar1.Maximum = img1.Width;
  120.  
  121.   if (img1.Width == img2.Width && img1.Height == img2.Height)
  122.      {
  123.       for (int i = 0; i < img1.Width; i++)
  124.        {
  125.          for (int j = 0; j < img1.Height; j++)
  126.           {
  127.  
  128.            img1_ref = img1.GetPixel(i, j).ToString();
  129.            img2_ref = img2.GetPixel(i, j).ToString();
  130.  
  131.  
  132.             if (img1_ref == "1" && img2_ref == "1")
  133.                {
  134.                 sum1++;
  135.                }
  136.  
  137.               else if (img1_ref =="1" && img2_ref =="0")
  138.                {
  139.                 sum2++;
  140.                }
  141.  
  142.               else if (img1_ref =="0" && img2_ref =="1")
  143.                {
  144.                 sum3++;
  145.                }
  146.  
  147.           } 
  148.  
  149.  
  150.     p = sum1;
  151.     q = sum2;
  152.     r = sum3;
  153.  
  154.     jd = (q + r) / (p + q + r);
  155.  
  156.  
  157.     if (jd < 0.75 )
  158.       MessageBox.Show ("Images are dissimilar," +jd+ "ratio results");
  159.  
  160.     else if ( jd == 0.75 || jd <= 1)
  161.       MessageBox.Show ("Images are similar," +jd+ "ratio results");
  162.       }
  163.  
  164.  
  165.      MessageBox.Show("can not compare this images");
  166.  
  167.      this.Dispose();
  168.  
  169.     }
  170.  
  171.  
  172.  
  173.         }
  174.     }
  175. }
Attached Images
File Type: jpg compare6&4.jpg (13.1 KB, 447 views)
Nov 4 '09 #1
6 4058
tlhintoq
3,525 Expert 2GB
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.
Nov 4 '09 #2
Plater
7,872 Expert 4TB
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
Nov 4 '09 #3
aznimah
19
thanks Plater but i really need help more elaboration about your suggestion, could you explain more? i'm sorry and i really appreciate it.
Nov 7 '09 #4
Plater
7,872 Expert 4TB
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.
Nov 9 '09 #5
aznimah
19
hi Plater, thanks for the feedback.already 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.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace JaccardRecognition
  12. {
  13.     public partial class shape : Form
  14.     {
  15.  
  16.  
  17.         Bitmap Image1, Image2;
  18.         String textImage1 = "";
  19.         String textImage2 = "";
  20.         float a, b, p, q, r, jd;
  21.         int sum1 = 0, sum2 = 0, sum3 = 0;
  22.  
  23.  
  24.  
  25.  
  26.         public shape()
  27.         {
  28.             InitializeComponent();
  29.         }
  30.  
  31.         private void Form1_Load(object sender, EventArgs e)
  32.         {
  33.  
  34.         }
  35.  
  36.         /*Open and read the first image*/
  37.  
  38.         private void OpenFirstImageBttn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  39.         {
  40.             openFileDialog1.FileName = "";
  41.             openFileDialog1.Title = "Images";
  42.             openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png";//
  43.             openFileDialog1.ShowDialog();
  44.             if (openFileDialog1.FileName.ToString() != "")
  45.             {
  46.                 Box1.ImageLocation = openFileDialog1.FileName.ToString();
  47.                 Image1 = new Bitmap(openFileDialog1.FileName.ToString());
  48.                 string h = Convert.ToString(Image1.Height);
  49.                 string w = Convert.ToString(Image1.Width);
  50.                 displayh.Text = h;
  51.                 displayw.Text = w;
  52.             }
  53.  
  54.         }
  55.  
  56.  
  57.  
  58.         /*Covert first image into binary*/
  59.  
  60.         private void convertImage1_Click(object sender, EventArgs e)
  61.         {
  62.  
  63.             for (int i = 0; i < Image1.Height; i++)
  64.             {
  65.                 for (int j = 0; j < Image1.Width; j++)
  66.                 {
  67.                     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")
  68.                     {
  69.                         textImage1 = textImage1 + "0";
  70.  
  71.                     }
  72.                     else
  73.                     {
  74.                         textImage1 = textImage1 + "1";
  75.  
  76.                     }
  77.  
  78.                 }
  79.                 textImage1 = textImage1 + "\r\n";
  80.             }
  81.  
  82.             text1.Text = textImage1;
  83.         }
  84.  
  85.         /*Open and read the second image*/
  86.  
  87.         private void OpenSecondImageBttn_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
  88.         {
  89.             openFileDialog2.FileName = "";
  90.             openFileDialog2.Title = "Images";
  91.             openFileDialog2.Filter = "All Images|*.jpg; *.bmp; *.png";//
  92.             openFileDialog2.ShowDialog();
  93.             if (openFileDialog1.FileName.ToString() != "")
  94.             {
  95.                 Box2.ImageLocation = openFileDialog2.FileName.ToString();
  96.                 Image2 = new Bitmap(openFileDialog2.FileName.ToString());
  97.             }
  98.         }
  99.  
  100.  
  101.  
  102.         /*Covert second image into binary*/
  103.  
  104.         private void convertImage2_Click(object sender, EventArgs e)
  105.         {
  106.  
  107.  
  108.             for (int i = 0; i < Image2.Height; i++)
  109.             {
  110.                 for (int j = 0; j < Image2.Width; j++)
  111.                 {
  112.                     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")
  113.                     {
  114.                         textImage2 = textImage2 + "0";
  115.  
  116.                     }
  117.                     else
  118.                     {
  119.                         textImage2 = textImage2 + "1";
  120.  
  121.                     }
  122.  
  123.                 }
  124.                 textImage2 = textImage2 + "\r\n";
  125.             }
  126.  
  127.             text2.Text = textImage2;
  128.         }
  129.  
  130.         /*This will compare both image from the binary data to check the similatiry measure */
  131.         /*Retrieve data in binary form to compute
  132.          --> The image size 16x16 pixels
  133.          --> looping the image 16 times where the comparison between in rows operation*/
  134.  
  135.         private void button3_Click(object sender, EventArgs e)
  136.         {
  137.  
  138.             string output1 = "";
  139.             string output2 = "";
  140.  
  141.             string img1_ref = "";
  142.             string img2_ref = "";
  143.  
  144.             output1 = Convert.ToString(textImage1);
  145.             output2 = Convert.ToString(textImage2);
  146.  
  147.  
  148.             if (output1.Length == output2.Length)
  149.             {
  150.  
  151.                 for (int i = 0; i < 16; i++) // looping the height image
  152.                 {
  153.                     for (int j = 0; j < 16; j++) // looping the length image character
  154.                     {
  155.                         img1_ref = output1[j].ToString();
  156.                         img2_ref = output2[j].ToString();
  157.  
  158.  
  159.                         if (img1_ref == "1" && img2_ref == "1")
  160.                         {
  161.                             sum1++;
  162.                         }
  163.  
  164.                         else if (img1_ref == "1" && img2_ref == "0")
  165.                         {
  166.                             sum2++;
  167.                         }
  168.  
  169.                         else if (img1_ref == "0" && img2_ref == "1")
  170.                         {
  171.                             sum3++;
  172.                         }
  173.  
  174.                     }
  175.  
  176.                 }
  177.  
  178.                 /* Jaccard measurement*/
  179.                 p = sum1;
  180.                 q = sum2;
  181.                 r = sum3;
  182.  
  183.                 a = q + r;
  184.                 b = p + q + r;
  185.                 jd = a / b;
  186.  
  187.                 string valueP = Convert.ToString(p);
  188.                 string valueQ = Convert.ToString(q);
  189.                 string valueR = Convert.ToString(r);
  190.                 string valueA = Convert.ToString(a);
  191.                 string valueB = Convert.ToString(b);
  192.                 text3.Text = valueP;
  193.                 text4.Text = valueQ;
  194.                 text5.Text = valueR;
  195.                 textqr.Text = valueA;
  196.                 textpqr.Text = valueB;
  197.  
  198.  
  199.                 if (jd == 0 || jd < 0.15)
  200.                 {
  201.                     MessageBox.Show("Images are similar,ratio results:" + jd);
  202.  
  203.                 }
  204.                 else if (jd == 0.15 || jd <= 1)
  205.                 {
  206.                     MessageBox.Show("Images are dissimilar,ratio results:" + jd);
  207.  
  208.                 }
  209.  
  210.                 else
  211.                     MessageBox.Show("can not compare this images");
  212.  
  213.             }
  214.             this.Dispose();
  215.  
  216.         }
  217.  
  218.  
  219.     }
  220. }
  221.  
Nov 11 '09 #6
Plater
7,872 Expert 4TB
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.
Nov 11 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Wiktor Zychla | last post: by
2 posts views Thread by http://www.visual-basic-data-mining.net/forum | last post: by
14 posts views Thread by Aman JIANG | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.