Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Find QImage inside another QImage.

    General and Desktop
    4
    7
    1348
    Loading More Posts
    • 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
      Synny last edited by Synny

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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 Reply Quote 2
        • S
          Synny last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 1
            • Pablo J. Rogina
              Pablo J. Rogina last edited by

              @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 Reply Quote 0
              • S
                Synny last edited by

                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 Reply Quote 1
                • mrjj
                  mrjj Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • First post
                    Last post