Calculate image similarity--one of Python can also

Source: Internet
Author: User

Disclaimer: This article was originally published in Rai Yonghao (Love flower butterflies) blog Http://blog.csdn.net/lanphaday

First, two pictures into a histogram, the similarity calculation of the image is converted to the distance of the histogram calculation, the paper according to the following formula for the quantitative measurement of histogram similarity:

Sim (g,s) =

Where G,s is the histogram and N is the color space sample point

Convert to the appropriate Python code as follows:

#!/usr/bin/env python#Coding=utf-8ImportImagedefMake_regalur_image (img,size= (256,256)):    returnImg.resize (size). CONVERT ('RGB')defSplit_image (img,part_size= (64,64)): W,h=img.size pw,ph=part_sizeassertW%PW = = h%ph==0return[Img.crop ((I,J,I+PW,J+PH)). Copy () forIinchXrange (0,W,PW) forJinchxrange (0,h,ph)]defhist_similar (LH,RH):assertLen (LH) = =Len (RH)returnSUM (-0ifL==rElseFloat (ABS (L-R))/max (l,r)) forL,rinchZip (LH,RH))/Len (LH)defcalc_similar (Li,ri):#return Hist_similar (Li.histogram (), Ri.histogram ())    returnSum (Hist_similar (L.histogram (), R.histogram ()) forL,rinchZip (Split_image (LI), Split_image (RI)))/16.0defCalc_similar_by_path (LF,RF): Li,ri=make_regalur_image (Image.open (LF)), Make_regalur_image (Image.open (RF) )returncalc_similar (Li,ri)defMake_doc_data (LF,RF): Li=make_regalur_image (Image.open (LF)) RI=Make_regalur_image (Image.open (RF)) Li.save (LF+'_regalur.png') Ri.save (RF+'_regalur.png') FD= Open ('Stat.csv','W') Fd.write ('\ n'. Join (L +','+r forL,rinchZip (Map (Str,li.histogram ()), Map (Str,ri.histogram ()))) Fd.close ()ImportImagedraw Li= Li.convert ('RGB') Draw=Imagedraw.draw (LI) forIinchXrange (0,256,64): Draw.line ((0,i,256,i), fill ='#F00') Draw.line (I,0,i,(fill=),'#F00') Li.save (LF+'_lines.png')if __name__=='__main__': Path= R'test/test%d/%d.jpg'     forIinchXrange (1,7):        Print 'test_case_%d:%.3f%%'% (I,calc_similar_by_path ('test/test%d/%d.jpg'% (i,1),'test/test%d/%d.jpg'% (i,2)) *100) Make_doc_data ('test/test4/1.jpg','test/test4/2.jpg')

Reference:

Using Python to do image processing

Calculate image Similarity--One of Python can also

Calculate image similarity--one of Python can also

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.