Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:06:17+00:00 2026-06-12T14:06:17+00:00

I have recently been interested in template matching, I know librarys like OpenCV exist

  • 0

I have recently been interested in template matching, I know librarys like OpenCV exist but i wanted to make a simple function to find all the points where a template can be found in a search BufferedImage without the need for any extra librarys or installing software etc.

I found a article on wiki about it which contained some code:

http://en.wikipedia.org/wiki/Template_matching

It seems to do exactly what i need so i have ported this to Java but instead rather than greyscale changed it slightly to use colors. Testing with a solid white Template image (30×30) and a Search image (508×307) which is also solid white but with red rectangles in some areas the code seems to output correctly to the console for a while however i am then getting errors:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
    at sun.awt.image.ByteInterleavedRaster.getDataElements(Unknown Source)
    at java.awt.image.BufferedImage.getRGB(Unknown Source)
    

I am not sure if this is due to the fact it’s not greyscale or if java requires additional adjustments to the loop amounts but any help getting this to work correctly would be appreciated.

public Point[] TemplateMatchBI(BufferedImage Search, BufferedImage Template){

// Point list
ArrayList<Point> temp = new ArrayList<Point>(); 

//Matched Colors Boolean
boolean m = false; 

//Current Index, Matched Colors Count, Search Color (Int), Template Color (Int)
int index = 0, mcc = 0, sc = 0, tc = 0; 

// Quick Strings
String s01 = "Index = ", s02 = ", Search color = ", s03 = ", Template color = ", 
s04 = ", Matched? = ", s05 = ", Matched? = ", s06 = ", Template scan = ";

// Loop through the search image
for ( int x = 0; x <= Search.getHeight() - Template.getHeight(); x++ ) {
    for ( int y = 0; y <= Search.getWidth() - Template.getWidth(); y++ ) {

        // Loop through the template image
        for (int i = 0; i < Template.getHeight(); i++)
            for (int j = 0; j < Template.getWidth(); j++) {

                sc = Search.getRGB(x+i, y+j); // Set Search Color
                tc = Template.getRGB(i, j); // Set Template Color
                
                index++; // Increase index int
                m = false; // Reset m to false
                
                if ( sc == tc ){ // If the Search and Template Colors match then
                    mcc++; // Increase match count
                    m = true; // Colors match
                    //temp.add(new Point(i, j)); // Template scan point
                }

                Log(s01 + index + s02 + sc + s03 + tc + s04 + m + s05 + mcc + s06 + new Point(x, y));
                //Log("i = " + i + ", j = " + j + ", point = " + new Point(x+i, y+j));
            }
        }
    }
Point[] tempReturn = new Point[temp.size()];
temp.toArray(tempReturn);
return tempReturn;
}

The images i test with:

Template.png (It’s solid white so it looks invisible on the page)

Template.png

Search.png

Search.png

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:06:19+00:00Added an answer on June 12, 2026 at 2:06 pm

    The seems to be that wiki gives the wrong code and the X and Y are in reverse, when swapped around it appears to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been interested in hardware programming recently, but I have not started yet. I
I have recently been using WxPython to create a GUI Network simulator like Cisco
Recently I have been interested in FTP servers and I was wondering how to
I have recently been reading up on the D programming language, and am interested
Professionally I am a back-end Java developer, but recently I've been interested in writing
Our team has been very interested in continuous deployment recently, but we've run into
I have developed a few app store applications but recently have been asked by
I have recently been asked if I could make a macro in Excel VBA
recently been interested in using windows phone to upload photo. I have upgraded my
I have just recently got interested in the Google Analytics plugin but found out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.