Realization and evaluation of image similarity algorithm in C #

Source: Internet
Author: User

Recently visited the blog by chance found a picture similarity of the Python algorithm implementation. Think it is very interesting to move to C # come up, to see you.

Gossip

Caishuxueqian, only the calculation of the image similarity of a basic algorithm of the basic implementation of the way to list out, so that in the end of their own assessment when also big feeling, this algorithm is a little bit unreliable. Anyway, this algorithm is sometimes useful, so it's still listed to share with the big guys ~ ~

PS: Image processing This piece of profound, personal occasionally found something to share. Say bad place, write too bad place, you are ready to throw bricks still look calm, calm ~ ~

Introduction to basic knowledge

Color histogram

Color histogram is a color feature widely used in many image retrieval systems, it describes the proportion of different colors in the whole image, and does not care about the spatial location of each color, that is, the object or objects in the image cannot be described. The color histogram is especially useful for describing images that are difficult to auto-segment.

Grayscale histogram

The grayscale histogram is a grayscale function that represents the number of pixels in the image that have each grayscale level, reflecting the frequency at which each grayscale appears in the image. Gray scale histogram is the horizontal axis, the vertical axis is the frequency of this gray level, is the most basic statistical characteristics of the image.

In this article is the use of gray histogram to calculate the image similarity, about the algorithm that piece is not to be discussed, after all, graphic graphics, histogram I am not understand, I am not ready to jewelery, just want to achieve a basic algorithm, and then from the most intuitive perspective to see the effectiveness of this algorithm, only that.

Algorithm implementation

You crossing the author swallowed, a shallow learning attitude. After all, it is because of interest, in this respect there is no basis (of course, in addition to know what RGB is a thing-this is also thanks to the computer graphics teacher is a super beauty, so more on the sake of a few lessons), not to mention the slightest accomplishment, crossing Mo Strange, and to endure anger, is to leave, The niche does not have the slightest block ~ ~

The approximate steps are as follows:

1, convert the image to the same size, in favor of calculating a similar histogram to

2, calculate the gray histogram after conversion

3, using the XX formula, get the quantitative measurement of the histogram similarity degree

4, output the similarity result data that does not know useful

Code implementation

Step 1, convert the image to the same size and we'll turn it into a X 256.

Public Bitmap Resize (string imagefile, String newimagefile)

{

img = Image.FromFile (imagefile);

Bitmap imgoutput = new Bitmap (IMG, 256, 256);

Imgoutput.save (Newimagefile, System.Drawing.Imaging.ImageFormat.Jpeg);

Imgoutput.dispose ();

Return (BITMAP) image.fromfile (newimagefile);

}

This part of the code is very understood, imagefile as the full path of the original picture, Newimagefile for the strong-to-size X 256 picture of the path, in order to "race" can see the image we converted after the long, so I saved it to the local, So that there is a slightly ugly code above.

Step 2, calculate the histogram of the image

Public int[] 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 paragraph is to frighten the world of ghosts and spirits of gray histogram calculation method, the inside of the curved around or left you to mix.

Step 3, calculate the histogram similarity measure

The magic of this step is this:

Sim (g,s) = where g,s is a histogram and N is a color space sample point

For everyone to knock down two lines of words, also give a bunch of messy code:

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 float GetResult (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;

}

}

Step 4, output

This one...... You love how to output it. Direct console or, colorful command line output, or save to a text file to stay as a souvenir, you "take good" ~ ~

Algorithm evaluation

Really sorry everyone, forget to tell everyone, I am not a professional algorithm evaluation staff, but as a uncompleted testers inevitably hand itch want to see the algorithm in the end how much ability, take out a few pictures out inspection bar.

The following is the algorithm evaluation results. The following part of the content of the words with ridicule, no malice, open jokes, entertainment public ~ ~

Pedestrian Armor

Passers-by B

Image Similarity degree

Spoof reviews

100%

There's nothing in there!?

Congratulations, if you do not see this is two pictures of the white, then you are really small white, because you do not even recognize their own people ah ~ ~

100%

All Crows under, this algorithm at this point on the position is still firm, good performance ~

100%

Met Win7 also not tempted, will firmly give 100% of the correct answer.

This algorithm than I will be more determined, I am win7 just out of a 7000 installed, has been used to now, but really good ~ ~

88.84%

Obviously very different "I" and "you" in there, how the similarity degree so high??

"I", "you" are not recognized??

Oh, I forgot, the big background of these two pictures is the same, no wonder ...

16.08%

Is MS so incompatible with Apple?

"Use default Desktop ~ ~"

50.64%

I finally got to know the real reason Jack and Rose couldn't be together:

Not the love is not deep enough, nor the Titanic sank, in the mother's words "no husband and wife"

--or a mother who has been a veteran

99.21%

Wow, that's unbelievable.

This algorithm so "black" "white" does not divide??

I have to apologize to Jack and Rose's loyal fans, the words of the above is a slip of the tongue ~ ~ Wish them both a lifetime, do not do boat in the next life, ship also do not go to sea, sailing to the Arctic, ...

After my somewhat cynical assessment, to be honest, I was quite disappointed with the algorithm, especially in the last comparison, and the mood is low. This is not to say that the algorithm is useless, it should be me or some of the predecessors used the wrong place, the personal feel that the limitations of the algorithm is too large, or perhaps my expectations are too high.

Postscript

When I first saw this thing, I thought it was very simple, but there was no such thing as an easy one to think about, or google,ms these men did not have a decent thing to do for so long. Sure enough, in order to know a little more about the content, I have to Google a bit, I think those terms are completely unintelligible, I do not understand AH; Hey, really find out a bunch of things, more than Google looks much easier, but open the link to see, and found that is not my current ability to do. I didn't learn anything, but somehow I learned a little fur.

End of full text

If you feel that you are wasting your time when you think you are crossing, you should be right as a cold joke and listen to the nerves.

Reference Links:

Http://blog.csdn.net/lanphaday/archive/2008/04/24/2325027.aspx

http://www.cnblogs.com/wuchaodong/archive/2009/04/28/1444792.html turn

Realization and evaluation of image similarity algorithm in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.