I was tasked with creating an image database, which has image objects of several types. Each image is consisted of a 2D array of pixels. I have been asked to create this method: MatchPattern (Image subimage, Image image): this function returns integer that represent how many times a subimage repeats into an image.
The code I have written is as follows:
public int MatchPattern(Image subimage, Image image) {
if(image.getClass().equals(subimage.getClass())){
int numOfMatches = 0;
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
int subimgWidth = subimage.getWidth();
int subimgHeight = subimage.getHeight();
if (imgWidth < subimgWidth || imgHeight < subimgHeight)
return 0;
for (int i = 0; i < imgHeight; i++) {
for (int j = 0; j < imgWidth; j++) {
int subHeightIndex = 0;
int subWidthIndex = 0;
Pixel imagePix = image.getImg()[i][j];
Pixel subimgPix = subimage.getImg()[subHeightIndex][subWidthIndex];
if( (imagePix.compareTo(subimgPix)==0) && ((imgWidth-j)>=subimgWidth) && ((imgHeight-i)>=subimgHeight) ){
boolean matchFlag = true;
for (int k = 0; k < subimgHeight; k++) {
if(matchFlag == false)
break;
for (int l = 0; l < subimgWidth; l++) {
matchFlag = (image.getImg()[i+k][j+l] == subimage.getImg()[k][l]);
if (matchFlag == false)
break;
}
}
if(matchFlag == true)
numOfMatches++;
}
}
}
return numOfMatches;
}
return 0;
However, whenever I run the method, it always returns a num of matches equals to 0. Can anybody point me to the right direction? Thank you in advance.