Checking whether an image is part of another
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I have the following code (slightly modified for simplicity). It checks whether a given image A (the template) is part of another image B. To handle different sizes, image B is scaled as long as a match is found or some minimum/maximum image size is reached.
The naive implementation for this is quite straight forward but I'm not happy with it:
The code within the two while loops is almost identical.
Handling scaling down and scaling up in two different loops is inefficient in a lot of cases (e.g. when the first iteration of the second while loop would find a match but we still need to go through all iterations of the first one).
Can you offer some suggestions on how to improve my code?
double bestMatch = 0;
while (image.width() > MIN_IMAGE_WIDTH)
match = match(image, template); // for the sake of simplicity let this return a double
if (match > bestMatch)
bestMatch = match;
if (match.maxVal >= threshold)
foundMatch = true;
break;
image = scaleImage(image, 0.9);
image = originalImage;
while (image.width() < MAX_IMAGE_WIDTH)
match = match(image, template);
if (match > bestMatch)
bestMatch = match;
if (match.maxVal >= threshold)
foundMatch = true;
break;
image = scaleImage(image, 1.1);
// do something with bestMatch
java image lambda
add a comment |Â
up vote
3
down vote
favorite
I have the following code (slightly modified for simplicity). It checks whether a given image A (the template) is part of another image B. To handle different sizes, image B is scaled as long as a match is found or some minimum/maximum image size is reached.
The naive implementation for this is quite straight forward but I'm not happy with it:
The code within the two while loops is almost identical.
Handling scaling down and scaling up in two different loops is inefficient in a lot of cases (e.g. when the first iteration of the second while loop would find a match but we still need to go through all iterations of the first one).
Can you offer some suggestions on how to improve my code?
double bestMatch = 0;
while (image.width() > MIN_IMAGE_WIDTH)
match = match(image, template); // for the sake of simplicity let this return a double
if (match > bestMatch)
bestMatch = match;
if (match.maxVal >= threshold)
foundMatch = true;
break;
image = scaleImage(image, 0.9);
image = originalImage;
while (image.width() < MAX_IMAGE_WIDTH)
match = match(image, template);
if (match > bestMatch)
bestMatch = match;
if (match.maxVal >= threshold)
foundMatch = true;
break;
image = scaleImage(image, 1.1);
// do something with bestMatch
java image lambda
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have the following code (slightly modified for simplicity). It checks whether a given image A (the template) is part of another image B. To handle different sizes, image B is scaled as long as a match is found or some minimum/maximum image size is reached.
The naive implementation for this is quite straight forward but I'm not happy with it:
The code within the two while loops is almost identical.
Handling scaling down and scaling up in two different loops is inefficient in a lot of cases (e.g. when the first iteration of the second while loop would find a match but we still need to go through all iterations of the first one).
Can you offer some suggestions on how to improve my code?
double bestMatch = 0;
while (image.width() > MIN_IMAGE_WIDTH)
match = match(image, template); // for the sake of simplicity let this return a double
if (match > bestMatch)
bestMatch = match;
if (match.maxVal >= threshold)
foundMatch = true;
break;
image = scaleImage(image, 0.9);
image = originalImage;
while (image.width() < MAX_IMAGE_WIDTH)
match = match(image, template);
if (match > bestMatch)
bestMatch = match;
if (match.maxVal >= threshold)
foundMatch = true;
break;
image = scaleImage(image, 1.1);
// do something with bestMatch
java image lambda
I have the following code (slightly modified for simplicity). It checks whether a given image A (the template) is part of another image B. To handle different sizes, image B is scaled as long as a match is found or some minimum/maximum image size is reached.
The naive implementation for this is quite straight forward but I'm not happy with it:
The code within the two while loops is almost identical.
Handling scaling down and scaling up in two different loops is inefficient in a lot of cases (e.g. when the first iteration of the second while loop would find a match but we still need to go through all iterations of the first one).
Can you offer some suggestions on how to improve my code?
double bestMatch = 0;
while (image.width() > MIN_IMAGE_WIDTH)
match = match(image, template); // for the sake of simplicity let this return a double
if (match > bestMatch)
bestMatch = match;
if (match.maxVal >= threshold)
foundMatch = true;
break;
image = scaleImage(image, 0.9);
image = originalImage;
while (image.width() < MAX_IMAGE_WIDTH)
match = match(image, template);
if (match > bestMatch)
bestMatch = match;
if (match.maxVal >= threshold)
foundMatch = true;
break;
image = scaleImage(image, 1.1);
// do something with bestMatch
java image lambda
edited Apr 3 at 5:01
Jamalâ¦
30.1k11114225
30.1k11114225
asked Apr 3 at 4:37
martin
1162
1162
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
To answer your second point:
Why not include the foundMatch
in the condition of the while
?
while (!foundMatch && image.width() < MAX_IMAGE_WIDTH) {
That way you don't run the second loop at all if a match was found in the first.
Having a couple of duplicate lines isn't a bad thing. It's obvious what the lines do and what the difference is between the 2 loops (1 scaling up, the other scaling down). So at least in my opinion there is nothing wrong with having both loops like this.
I'm actually most surprised that you need to handle scaling at all though. One of the most popular image comparison algorithms these days uses SIFT (Scale invariant feature transform) which, as the name implies is scale invariant.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
To answer your second point:
Why not include the foundMatch
in the condition of the while
?
while (!foundMatch && image.width() < MAX_IMAGE_WIDTH) {
That way you don't run the second loop at all if a match was found in the first.
Having a couple of duplicate lines isn't a bad thing. It's obvious what the lines do and what the difference is between the 2 loops (1 scaling up, the other scaling down). So at least in my opinion there is nothing wrong with having both loops like this.
I'm actually most surprised that you need to handle scaling at all though. One of the most popular image comparison algorithms these days uses SIFT (Scale invariant feature transform) which, as the name implies is scale invariant.
add a comment |Â
up vote
1
down vote
To answer your second point:
Why not include the foundMatch
in the condition of the while
?
while (!foundMatch && image.width() < MAX_IMAGE_WIDTH) {
That way you don't run the second loop at all if a match was found in the first.
Having a couple of duplicate lines isn't a bad thing. It's obvious what the lines do and what the difference is between the 2 loops (1 scaling up, the other scaling down). So at least in my opinion there is nothing wrong with having both loops like this.
I'm actually most surprised that you need to handle scaling at all though. One of the most popular image comparison algorithms these days uses SIFT (Scale invariant feature transform) which, as the name implies is scale invariant.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
To answer your second point:
Why not include the foundMatch
in the condition of the while
?
while (!foundMatch && image.width() < MAX_IMAGE_WIDTH) {
That way you don't run the second loop at all if a match was found in the first.
Having a couple of duplicate lines isn't a bad thing. It's obvious what the lines do and what the difference is between the 2 loops (1 scaling up, the other scaling down). So at least in my opinion there is nothing wrong with having both loops like this.
I'm actually most surprised that you need to handle scaling at all though. One of the most popular image comparison algorithms these days uses SIFT (Scale invariant feature transform) which, as the name implies is scale invariant.
To answer your second point:
Why not include the foundMatch
in the condition of the while
?
while (!foundMatch && image.width() < MAX_IMAGE_WIDTH) {
That way you don't run the second loop at all if a match was found in the first.
Having a couple of duplicate lines isn't a bad thing. It's obvious what the lines do and what the difference is between the 2 loops (1 scaling up, the other scaling down). So at least in my opinion there is nothing wrong with having both loops like this.
I'm actually most surprised that you need to handle scaling at all though. One of the most popular image comparison algorithms these days uses SIFT (Scale invariant feature transform) which, as the name implies is scale invariant.
answered Apr 3 at 7:25
Imus
3,328223
3,328223
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191119%2fchecking-whether-an-image-is-part-of-another%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password