Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find QImage inside another QImage.
Forum Updated to NodeBB v4.3 + New Features

Find QImage inside another QImage.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Synny
    wrote on last edited by Synny
    #1

    I have two QImage objects. I want to find the x,y of the smaller Qimage in the larger QImage.
    Update: The smaller image is a perfect crop of the larger image. Just to find a sub matrix in a matrix of rgb.
    How can I do it?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums
      So image 2 is also draw somewhere on image 1 ?

      It depends on what is around the image 2 when its inside image 1
      if a solid color or not very noise you might be able to search for a bit pattern and
      get no false positives.
      If you need something more advanced, then something like
      https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Synny
        wrote on last edited by
        #3

        The smaller image is a perfect crop of the larger image. No need to draw a thing. No need for complicated match algorithms. Just to find a sub matrix in a matrix of rgb.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          Then you'll have to implement your own function that will find the first pixel, then ensure that the second is the right etc.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @Synny maybe template matching using OpenCV can help you. Although some of the examples you can find are Python-based, I guess those could be a good start point to implement your C++ code.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Synny
              wrote on last edited by
              #6

              I wrote a function. Works great :)

              QPoint FindImageInImage(QImage &ImgLarge , QImage &ImgSmall )
              {
                  if ( ImgLarge.format() != QImage::Format_RGB32 )
                      ImgLarge.convertToFormat(QImage::Format_RGB32);
              
                  if ( ImgSmall.format() != QImage::Format_RGB32 )
                      ImgSmall.convertToFormat(QImage::Format_RGB32);
              
                  const uchar* bitsImgLarge = ImgLarge.constBits();
                  int nHeightImgLarge = ImgLarge.height();
                  int nWidthImgLarge = ImgLarge.width();
              
                  const uchar* bitsImgSmall = ImgSmall.constBits();
                  int nHeightImgSmall = ImgSmall.height();
                  int nWidthImgSmall = ImgSmall.width();
              
                  QPoint ptFound = QPoint(-1,-1);
              
                  for ( int yInImgLarge = 0 ; yInImgLarge < nHeightImgLarge-nHeightImgSmall ; yInImgLarge++  )
                      for ( int xInImgLarge = 0 ; xInImgLarge < nWidthImgLarge-nWidthImgSmall ; xInImgLarge++  )
                      {
                          bool bFound = true;
              
                          for ( int yInImgSmall = 0 ; yInImgSmall < nHeightImgSmall ; yInImgSmall++  )
                              for ( int xInImgSmall = 0 ; xInImgSmall < nWidthImgSmall ; xInImgSmall++  )
                              {
                                  int nXInImgLarge_ = xInImgLarge+xInImgSmall;
                                  int nYInImgLarge_ = yInImgLarge+yInImgSmall;
              
                                  int nXInImgSmall_ = xInImgSmall;
                                  int nYInImgSmall_ = yInImgSmall;
              
                                  if ( nYInImgLarge_ >= nHeightImgLarge )
                                  {
                                      bFound = false;
                                      goto endInnerLoop;
                                  }
              
                                  if ( nXInImgLarge_ >= nWidthImgLarge )
                                  {
                                      bFound = false;
                                      goto endInnerLoop;
                                  }
              
                                  const uchar* pImgLargeCurr = bitsImgLarge + 4*((nYInImgLarge_)*nWidthImgLarge+nXInImgLarge_);
                                  const uchar* pImgSmallCurr = bitsImgSmall + 4*(nYInImgSmall_*nWidthImgSmall+nXInImgSmall_);
              
                                  if ( memcmp( (void*)pImgLargeCurr, (void*)pImgSmallCurr, 3 ) )
                                  {
                                      bFound = false;
                                      goto endInnerLoop;
                                  }
                              }
              
                          endInnerLoop:
              
                          if ( bFound )
                          {
                              ptFound = QPoint( xInImgLarge , yInImgLarge );
                              goto endOuterLoop;
                          }
                      }
              
                  endOuterLoop:
              
                  return ptFound;
              }
              
              1 Reply Last reply
              1
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                Thank you for sharing.
                Why the goto if i may ask, and not just return ptFound;
                in case of clean up or ?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved