Atitit determines whether an image contains another small image,

Source: Internet
Author: User

Atitit determines whether an image contains another small image,

 

Atitit determines whether an image contains another small image

 

1. keyword1

2. template matching is one of the methods to find the target in the image (cutting + image similarity calculation) 1

3. Matching effect 2

4. image similarity algorithm (Perceptual hash algorithm) 2

5. performance result 2

6. How to improve performance 3

6.1. simplified algorithms can be used. The secondary matching method first determines Area 3

6.2. Set step 3 for the cut image.

7. References 3

8. ------ code3

 

1. Keyword

 

Image matching

Is the image part of another image?

If it is a standard image, it is good to match the template.

 

2. Template matching is one of the methods to find the target in the image (cutting + image similarity calculation)

. Come On, Boy. Let's take a look at the template matching.

Template Matching Method
The template matching method is basically the same as the reverse projection of the histogram. The general process is as follows: the actual image block and the input image are matched by moving the image block on the input image.
Suppose we have a 100x100 input image and a 10x10 template image. The search process is as follows:
(1) cut a temporary image (0, 0) to (10, 10) from the upper left corner of the input image (0, 0;
(2) Use a temporary image and a template image for comparison. The comparison result is recorded as c;
(3) compare result c, that is, the pixel value at the result image (0, 0;
(4) Cut the temporary image from () to (), compare it, and record it to the result image;
(5) duplicate (1 )~ (4) step until the bottom right corner of the input image.
As you can see, histogram reverse projection compares histograms, while template matching compares pixel values of images. template matching is faster than histogram reverse projection, but I personally think that the histogram reverse projection is more robust.

 

3. Matching Effect

The results are good. You can find the corresponding regional coordinates .. For a color image, you can use the color space information to exclude other incorrect matching images, and you can almost accurately determine an image ..

4. Image similarity algorithm (Perceptual hash algorithm)

Perceptual hash algorithm generates a "fingerprint" string for each image, and then compares the fingerprints of different images. The closer the result is, the more similar the image is.

The following is a simple implementation:

Step 1: Reduce the size.Reduce the image size to 8x8, with a total of 64 pixels. The purpose of this step is to remove the image details, retain only the basic information such as structure and brightness, and discard the image differences caused by different sizes and proportions.

Step 2: simplify the color.Convert the reduced image to 64-level gray scale. That is to say, all pixels have only 64 colors in total.

Step 3: calculate the average value.Calculate the average gray scale of all 64 pixels.

Step 4: Compare the gray scale of pixels.Compare the gray scale of each pixel with the average value. If the value is greater than or equal to the average value, it is recorded as 1; if the value is smaller than the average value, it is recorded.

Step 5: Calculate the hash value.The comparison result in the previous step is combined to form a 64-bit integer, which is the fingerprint of the image. The order of the combination is not important, as long as all images are in the same order.

After obtaining the fingerprint, you can compare different images to see how many digits are different in the 64-bit format. Theoretically, this is equivalent to calculating the "Hamming distance ". If the number of different data bits does not exceed 5, the two images are very similar. If the number is greater than 10, the two images are different.

 

 

5. Performance Results

On our machine, the java language is implemented in pure java. Matching and transforming dct algorithm size 8

Move step = 5 at a time .. The Parallel library is used ..

The speed is about 120 s .. I am too lazy to continue to adjust the performance. The main purpose is to test the performance. However, I have summarized the idea and used it for the time being. The ideal goal should be within 3 seconds ..

 

This is the reference article about 70 seconds .. The language is c # Call the opencv Interface

Efficiency of template matching and histogram reverse projection
In general, the efficiency of template matching and histogram reverse projection is not high. On my machine, it takes about 1136 milliseconds to match a 852*104-sized template image (all single-channel Grayscale Images) on an input image of 132*700; the histogram reverse projection takes about 75000 milliseconds (1.25 minutes ). It seems that you still need to continue learning to find a better solution.

 

6. How to improve performance

The main algorithm is used for matching small images ..

6.1. You can use a simplified algorithm. The secondary matching method first determines the region

. First, several suspected image coordinates can be determined significantly .. Then we can perform high-precision algorithm matching on the suspected images to match almost the only one .. If the image is a color image, the color information is used for secondary matching similarity .. And histogram and other algorithm supplements to match ..

6.2. Set a step for splitting an image.

For example, if the image size is 60*60, you can set a step less than 20% to skip the cut, which can greatly improve the performance, 60/0. 05 = 12 .. It can immediately increase the performance by 12 times ..

 

After selecting a region, you can use the exact moving step to cut the image.

 

7. References

 

How can I determine whether an image contains another small image? Image matching? -Csdntan -csdn.net-it technology community .html in China

Template Match (Match Template)-Wuya-bokyue.html

Algorithm-jpeg dct compression principle, who can easily understand and explain? -SegmentFault.html

Author: nickname: old wow's paw (full name: Attilax Akbar Al Rapanui Attila Akba Arla Panui)

Chinese name: atira (iron), EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://www.cnblogs.com/attilax/

 

8. ------ Code

 

Private VoidT ()ThrowsIOException {

String big = "c :\\ t.jpeg ";

String success = "c: \ lit.jpg ";

// Diff = 484

 

BufferedImage lit_img = ImageIO.Read(NewFile (bytes ));

 

IntW2 = lit_img.getWidth ();

IntH2 = lit_img.getHeight ();

 

Imgx =NewImgx ();

BufferedImage img = ImageIO.Read(NewFile (big ));

IntWidth = img. getWidth ();

IntH = img. getHeight ();

ImagePHash imagePHash =NewImagePHash (8, 8 );

ImageReader ImageReader1 = imgx. ImgReader (NewFile (big ));

 

String ext = filex.GetExtName(Big );

Imgx. save (lit_img, "c: \ 0tmpPic \" + filex.GetUUidName() + "." + Ext, ext );

ExecutorService es = Executors.NewFixedThreadPool(3 );

Lab:For(IntI = 0; I <width; I = I + 5 ){

For(IntJ = 0; j

// BufferedImage tmp =

// (BufferedImage) imgx. cutImage_retImg (big,

// I, j, w2, h2 );

N ++;

IntX = I;

IntY = j;

IntNowCount = n;

Runnable ra = ()-> {

 

Process (lit_img, w2, h2, imgx, imagePHash, ImageReader1, nowCount,

Ext, x, y );

};

Es.exe cute (ra );

 

}

}

Es. shutdown ();

}

 

Private IntProcess (BufferedImage lit_img,IntW2,IntH2, imgx,

ImagePHash imagePHash, ImageReader ImageReader1,IntN, String ext,

IntI,IntJ ){

Rectangle rect =NewJava. awt. Rectangle (I, j, w2, h2 );

BufferedImage tmp = imgx. cutImg (rect, ImageReader1 );

 

IntDis = imagePHash. distance (tmp, lit_img );

 

If(Dis <5 ){

Log. Info ("count:" + String.ValueOf(N) + "dis :"

+ String.ValueOf(Dis) + "rect:" + String.ValueOf(I) + "_"

+ String.ValueOf(J ));

// Break lab;

}

If(Dis <10 ){

String out = "c: \ 0tmpPic \" + filex.GetUUidName() + "." + Ext;

Imgx. save (tmp, out, ext );

Log. Info ("count:" + String.ValueOf(N) + "dis :"

+ String.ValueOf(Dis) + "rect:" + String.ValueOf(I) + "_"

+ String.ValueOf(J) + "file:" + out );

}

 

IntCnt = n;

If(Cnt % 100 = 0 ){

String count = String.ValueOf(Cnt );

 

Log. Info ("count:" + count + "dis:" + String.ValueOf(Dis)

+ "Rect:" + String.ValueOf(I) + "_" + String.ValueOf(J ));

}

ReturnN;

}

 

}

 

Atiend

 

 

 

Related Article

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.