Find QImage inside another QImage.
-
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 -
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.
-
@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.
-
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; }
-
Hi
Thank you for sharing.
Why the goto if i may ask, and not just return ptFound;
in case of clean up or ?