Image similarity (reference)-"original" + "Reference" PPC-Based Image Comparison Program-use histogram Measurement

Source: Internet
Author: User

From: http://tech.it168.com/a2009/0811/622/000000622042.shtml

Preface:

Recently, When I was bored, I thought of Image Comparison on PPC. I finally completed this program today by searching for information from various parties. Now I will share my work with you. There are still many shortcomings in the program. Despite your comments and suggestions, I am very welcome!

Preface:

After reading this article, you will know:

How to scale and save images in the C # language under the. NET Compact Framework 2.0 Framework

How to calculate the histogram measurement of an image in C #

How to Use the selectpicturedialog dialog box

You need the following development environment:

Visual Studio 2008

Windows Mobile 6 Professional SDK (wm6 can directly run the examples provided by me, and wm5 can also be developed)

Body:

Step 1: first create a C # smart device project. I chose the. NET Compact Framework 2.0 Framework library and then select the Windows Mobile 6 Professional SDK platform.

In the default form1 form, we design it as follows:

One label2 is used to display the comparison result and one tabcontrol. One picturebox1 exists in tabpage1, and one picturebox2 is also put in tabpage2. The sizemode attribute of the two pictureboxes is set to stretchimage.

After the interface is designed, we will add code. Here we will only list the key code. For detailed code, please go to the downloaded project to find it.

Reference in program Header

Using Microsoft. windowsmobile. forms;

Using system. drawing;

Then, add the global variable:

Piccompare. gethisogram. gethis = new piccompare. gethisogram. gethis (); // the histogram measurement calculation class contains the image scaling method.

String pic1 = @ "storage card/test.bmp"; // specify the storage location and format of the scaled image.

String pic2 = @ "storage card/test2.bmp"; // specify the storage location and format of the scaled image.

Int [] pic1t; // histogram measurement container of image 1

Int [] pic2t; // histogram measurement container of Image 2

After defining the variable, double-click "Image 1" and add the following code to it:

Selectpicturedialog SPD = new selectpicturedialog ();

SPD. showdialog ();

Pic1t = gethis. gethisogram (gethis. resized (Spd. filename, pic1); // calculate the histogram measurement of image 1 and store it in an array variable of pic1t.

Bitmap BMP = new Bitmap (Spd. filename );

Picturebox1.image = BMP; // put the processed image in picturebox1 for preview.

The buttons in Image 2 are the same as those in the preceding figure. You only need to modify the parameters.

Then let's take a look at some code implementation of the histogram measurement calculation class:

The code for calculating the histogram of an image is copied from the network. This is everywhere and I am not very good at it. So I will not explain it for the moment, Khan ~

/// <Summary>

/// Calculate the histogram of the image

/// </Summary>

/// <Param name = "IMG"> image </param>

/// <Returns> return histogram measurement </returns>

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 [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;

}

The following code calculates the measurements of the two images and puts the two measurements here to calculate the results. The obtained result is a reference value of the image similarity. The Code is as follows:

/// <Summary>

/// Final calculation result

/// </Summary>

/// <Param name = "firstnum"> histogram measurement of image 1 </param>

/// <Param name = "scondnum"> histogram measurement of Image 2 </param>

/// <Returns> calculation result </returns>

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;

}

}

Here, another class is used to process the image size. It is also posted here. You should use it!

/// <Summary>

/// Resize the image (square)

/// By Jack fan

/// </Summary>

/// <Param name = "sidesize"> specify the size </param>

/// <Param name = "srcbmp"> original image </param>

/// <Returns> returns the scaled bitmap image. </returns>

Public bitmap resizebmp (INT sidesize, bitmap srcbmp)

{

Bitmap BMP = new Bitmap (sidesize, sidesize );

Rectangle srcrec = new rectangle (0, 0, srcbmp. Width, srcbmp. Height );

Rectangle destrec = new rectangle (0, 0, sidesize, sidesize );

Graphics G = graphics. fromimage (BMP );

G. drawimage (srcbmp, destrec, srcrec, graphicsunit. pixel );

G. Dispose ();

Return BMP;

}

Code of the Compare button:

Picturebox1.refresh ();

Picturebox2.refresh ();

Label2.text = (gethis. getresult (pic1t, pic2t) * 100). tostring () + "%"; // calculate the final result

Okay, the code is like this. Let's take a look at the actual effect:

1. After the program is running, two similar images are added (for differences, see the red circle of the third image ):

Then, click "Compare" to see the similarity:

End:

Here, I want to declare that the idea of this histogram measure to calculate the image similarity is from a friend's blog in the garden. I wanted to attach his signature to it, however, no matter what the problem is, I can't find his original article. Sorry, if you see it, let me know. After all, I still have many questions to ask! (The original name is "C # Implementation and evaluation of image similarity algorithms". It seems that cnblogs cannot be searched. Please click it !) Some details are not clearly described in this article. You are welcome to ask questions. In the next article, you can only answer questions. I believe that after reading the source code and learning about the histogram measurement, you will feel another way.

In addition, during the test, we found that if we used image segmentation, we would compare them one by one. Then, in the summary method, the results would be relatively high! However, PPC devices have limited hardware conditions and are not considered yet.

View Original address

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.