Overview

I had an opportunity to obtain the coordinates within a larger image from multiple cropped sub-images. This article is a memo summarizing the method for doing this.

I introduce a method using OpenCV’s SIFT (Scale-Invariant Feature Transform) to perform feature point matching between template images and the original image, estimate the affine transformation, and obtain the coordinates.

Implementation

Required Libraries

pipinstallopencv-pythonnumpytqdm

Python Code

The following code matches template images (PNG images in templates_dir) against a specified large image (image_path) using SIFT, and obtains the coordinates within the original image.

iiffi#d#d#d#d#d#dmmrrmeeeeeeppoopLfEfMfAfDfMfoommooxafrarrraliirtertmmgrfesdMradhrtcrim#itd#sbk#if#cpttgttdomferxecaaoeisrs_ewr,ervenamesifpfovrlqagtatthttotntctataca2tiLgmtSf1IrS2icnodoidiucruiccduei__fumwwtn.upnop_It=,fda.nvubmsm_=mprtarnhh_rmppfra__sprr(a=liFeprttikigi#Mi#b#xbcvit2maigrncng_emntattintm=pfonoid_mT=cdnsreeefpfof_fe,avem(piigmciftfsartssncatolcmlpgveo1itmm2oAaDsDs2wfymmeavinie_dpetgaeeMhttsryteaioafc2snuppt,ddgffMrtiye.rr"ppg2stmafera=con_==,_ecemlrsgmat=ev.1fitrllecec_opcff_pca_s,_peiRaooie.(gtetothosaadhm=eiaseadha2Bes(n_modsomoroiiarowdpnustesrrn_iNfuaecumedffnn_fepdnni_g_sct.F=a"p=pne2nadinnnfinsl_atuesttgmo"rtceras_ofppfrdln_esnpeivuSMtNNaltstt_nteefntrta,mTl(ungrrnIeutsetmri..=ie__ppsfgam=2rIaeuootli2iicmtiitieyeetolpgtraeemsrossc=amnffncrs.t(otaa.eFtxrnhoinsnha(nt=n(nc=_xutlqaya:aer(htaellcetehfsirhnggiTcteefasu=uetfurefutf=ttody(dgs.de[ct_oovacalmm,delmd_hrs:eideNesc"eae"eadi=(pibmsp(e(dermhitaa2ntpo=ae_oreceaan_NeohInsiAnrlodumcapies.eortt.gaeagdttgbetrrcftioxn=enstsfgaecsstaatanmt1kfsna33elntce_eer(aee(toutmntessfiflwnv.t_glhtoae,non22seg3v,pmmatdcac_urqaer:muomNie_a2p_pee)htgcnries((tl22tppye(ttvfnedg:aaifraonmm.aia:,etdMfsf[[ioe(.[sll(mioe2edsmectsfmtneaebtmtsf,Aeamtokkmn([tnaaipmr(.a(_tciaeetohghaconst,lirppai[rpttmla)Ntftg_hNct_:tcnu.,,vvudd2cemm21tim0a.eeaag&Ouoerf_oiiarhense2neC,hnna([[ema,nisgteRrumaefneofaeadpbdd.dto((tkmmAagsn_lee_BMenpyaeennfndrilas:I:emmdigip..fge0ftdi_spF_sdl(ta:tis_nistMcpaenoo1tqfe,]o3isp_aML(_tutenfrtgte_{R{tutson,rui,r2rtadta2iiperufseoehRe_ioEpotc1mdaenMm(,tiht,mnameret_rceexnmuAareh,a_kire_[(thr)cgtpseaitmtctagtDt)(etmpnyPawnro))hc,thl(stmraart(m)p_h:irdca2IIaf,pauerhs_t(uaatne(oeuG}m,eht,ddrf.ntrose)pedrtnigcbs,tR"asecxxti0aspsi:ameeisolte._A)gr2shg]]in]rfussfttpsofneasp(pYea,eo..ae,roteCtahl1mno(ntaxaS,tisoppl,ar_th)r),,aredg_t,tCikf)dtt2[ympuegtmssldhhANo=_Dtw(eapcesdc(ttes.y}Lo_2m>mff(e,[dtktiehki_tb"Ent).=aoosmr_h=fsepmi)a-))eedtrrrphep)Fit2s1ams)simccl]ct:am),:,tge1tsihmm__,tslai,n0=tneps_)sgb{koa)0a_siith[p]eeftpnMm,.nm)nnsa0t,).)e2_e7ca:,p,s"m,fa(c,etgge]i)paftvcood)h)slgife2m<hoos:],C_olim.ieddt]lpoenpFnrs___)MoaddelO_ammp_st_:,_NmteaataehmpTailtts#fd}a{ta_toscc,f="ttetHc_ehhFiT)cemhEhteemonrhmp)ReeNsseueuepl)Ssso]]tr)esl.[H=tn))h[,)_s0E4e..oc0ph]Y)rrdo]caa_:ee=rotpSnsscnlheI.hhve#o})Mdaa2rr"Pipp.sT=)LseeRr(Et((Aoa0Xa--Nfn,,n11Ssc,,Atf01eCho,,]11,er,,m2(rre5022aed5,))nc)stc,0aao,cnotRgrh2eldi5peic5rnk)oan,jteTes2hss)r=e2s)hold=5.0)

Execution

#ifExe_#ITOmcnMEUauaPAMTitmaGPPnierELU(o_a_ATIn_mPT_MeAEPA=tTSAG=eH_TErDH_"=IP_sR=A_e"Tmt/="Hatx/,iix"xnnxxT_gxxE_sdx/M"exmP:f/aLa*tAu.cTlphEtn_S.gr_j"eDpsIguR"l,t.OjUpTgP"UT_PATH)

Summary

This article introduced a method for estimating where sub-images are located in the original image using SIFT-based feature point matching, and identifying positions through affine transformation.

  • SIFT is used for feature extraction (freely available since OpenCV 4.4)
  • BFMatcher is used for feature matching, and RANSAC is used for noise removal
  • Affine transformation is used to estimate coordinates and draw rectangles on the original image
  • Result images are saved for visualization of where each sub-image is located

This method can be applied to tasks such as locating partial images in historical maps, OCR region detection, and image comparison.

Future challenges:

  • Correction for rotated images
  • Consideration of faster algorithms than SIFT (ORB, AKAZE, etc.)
  • Processing speed optimization (feature point filtering)

There may be some incomplete points, but I hope this serves as a useful reference.