Share via


C # Realization of Image Similarity Algorithm

I think this article of content you may find it from google every where, I'd just like to organize those let it looks easy to be read.

Introduction to basic knowledge

Color bar chart

       Color bar graphs are widely used in many image retrieval systems. They describe the proportions of different colors in the whole image, and do not care about the spatial position of each color, It is impossible to describe an object or an object in the image. The color bar chart is particularly useful for describing images that are difficult to segment automatically.

Gray scale bar graph

  The grayscale bar graph is a function of the gray scale, which represents the number of primitives in the image that have each gray level, reflecting the frequency of each grayscale in the image. The abscissa of the gray scale bar graph is the gray level, and the ordinate is the frequency of the gray level, which is the most basic statistical feature of the image.

  In this paper, the use of gray bars to calculate the image similarity, on the algorithm that is not a word, after all, graphic graphics, just want to achieve a basic algorithm, and then from the most intuitive point of view Look at the effectiveness of this algorithm, that's it. 

Algorithm implementation

The general steps are as follows:

1, The image is converted to the same size, in order to facilitate the calculation of similar to the long bar chart.

2, Calculate the converted gray scale bar graph.

3, Using XX formula, get the bar graph similarity quantitative measurement.

4, The output of these do not know useful useless similarity results data.

Program implementation

Step 1, the image is converted to the same size, we temporarily converted into 256 X 256 it. 

publicint[] BitmapResize(string imgFile,string newImageFile)
{
 
      img = Image.FromFile(imageFile);
 
      BitmapimgOutput = new Bitmap(img, 256, 256);
 
      imgOutput.Save(newImageFile, System.Drawing.Imaging.ImageFormat.Jpeg);
 
      imgOutput.Dispose();
 
      return (Bitmap)Image.FromFile(newImageFile);
 
  return histogram;
}

This part of the code is very easy to understand, imageFile for the original image of the full path, newImageFile for the size of the 256x256 after the strong transfer of the path, in order to see you after the conversion of the picture long how, so I saved it to Local, so that there are slightly ugly above the code.

Step 2, calculate the image of the bar chart:

publicint[] GetHisogram(Bitmap img)
{
 
   BitmapData data = img.LockBits(new System.Drawing.Rectangle(0, 0, img.Width, img.Height),
                                    ImageLockMode.ReadWrite,
                                    PixelFormat.Format24bppRgb);
 
   int[ ] histogram = new int[ 256 ];
 
   unsafe{
 
    byte* ptr = (byte* )data.Scan0;
 
    int remain = data.Stride - data.Width * 3;
 
    for(int i = 0 ; i < histogram.Length ; i ++ )
 
    histogram[ i ] = 0;
 
    for(int i = 0 ; i < data.Height ; i ++ ) {
 
       for(int j = 0 ; j < data.Width ; j ++ ) {
 
              int mean = ptr[ 0 ] + ptr[ 1 ] + ptr[ 2 ];
 
              mean /= 3;
 
              histogram[ mean ] ++;
 
              ptr += 3;
 
       }
 
       ptr += remain;
 
    }
 
    }
 
   img.UnlockBits( data ); 
 
   return histogram;
 
}

This is the shaking of the ghosts of the gray scale bar graph calculation method.

Step 3, calculate the bar graph similarity measure:

The formula for this step:

Sim(G,S)=

Where G, S is the long bar graph, and N is the number of color space samples.

The implementation code is as follows: 

// Calculate the absolute value after subtraction
 
Private float GetAbs(int firstNum,int secondNum)
{
 
   Float abs = Math.Abs((float)firstNum - (float)secondNum);
 
   Float result = Math.Max(firstNum, secondNum);
 
   if(result == 0)
 
   result = 1;
 
   return abs / result;
 
}
 
// Final calculation results
 
Public floatGetResult(int[] firstNum,int[] scondNum)
{
 
   if(firstNum.Length != scondNum.Length)
 
   {
 
     Return 0;
 
   }
 
   else
 
   {
 
     Float result = 0;
 
     Int j = firstNum.Length;
 
     for(int i = 0; i < j; i++)
 
     {
 
       result += 1 - GetAbs(firstNum[i], scondNum[i]);
 
       Console.WriteLine(i +"----"+ result);
 
     }
 
     Return result / j;
 
   }
 
}

you may download this article example below:

https://code.msdn.microsoft.com/C-Realization-of-Image-a253ad06