Checking whether an image is part of another

The name of the pictureThe name of the pictureThe name of the pictureClash 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:



  1. The code within the two while loops is almost identical.


  2. 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






share|improve this question



























    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:



    1. The code within the two while loops is almost identical.


    2. 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






    share|improve this question























      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:



      1. The code within the two while loops is almost identical.


      2. 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






      share|improve this question













      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:



      1. The code within the two while loops is almost identical.


      2. 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








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 3 at 5:01









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Apr 3 at 4:37









      martin

      1162




      1162




















          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.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer













                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.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Apr 3 at 7:25









                Imus

                3,328223




                3,328223






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?