"Original" + "Reference" PPC-Based Image Comparison Program-use histogram Measurement

Source: Internet
Author: User

Preface:

Recently, when my younger brother was bored, he thought of making image comparison on PPC, searching for information from various parties, and finally completed this preliminary operation today.ProgramNow, I will share 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:

    1. Visual Studio 2008
    2. Windows Mobile 6 Professional SDK (wm6You can directly run the example provided by me, and wm5 can also be developed.)

Body:

Step 1: Create a C #Smart device project,I chose. NET Compact Framework 2.0And then selectWindows Mobile 6 Professional SDKPlatform.

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

One label2 is used to display the comparison result, one tabcontrol, one picturebox1 in tabpage1, one picturebox2 in tabpage2, And the sizemode attribute of two pictureboxes is setStretchimage.

After the interface is designed, we will addCodeHere, only key code is listed. For detailed code, go to the downloaded project.

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:

 

Code
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 ~

Calculate the histogram of the image
  ///   <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. 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;
}

 

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:

 

Final calculation result
///   <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!

 

Code
  ///   <Summary>
/// Image size scaling (square)
/// Author: Jack fan
///   </Summary>
///   <Param name = "sidesize"> 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 ();

ReturnBMP;
}

 

 

Code of the Compare button:

 

Comparison button
Picturebox1.refresh ();
Picturebox2.refresh ();
Label2.text=(Gethis. getresult (pic1t, pic2t)* 100). Tostring ()+ "%";//Calculation 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 that I thought from the blog of Aaron Wu, a friend in the garden, I wanted to include his signature in front of 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! (Original article name: image similarityAlgorithmIn C # implementation and evaluation, it seems that the 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 you are readingSource codeAfter learning about the relevant 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. If it is reproduced, please indicate the original article owner. Thank you.

Source code:

Click to download

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.