Question Check if image exists in another image.

Cocer

New member
Joined
Dec 21, 2013
Messages
1
Programming Experience
Beginner
Hello,

I want to check if a image is in another image.
I found a piece of code, but if I want to find a circle (for example), and the circle has a transparent background, it doesn't find the circle, because in the other image the circle does have a colored background. Recoloring the background is not an option because the background can be too complex, how can I stop the program from checking if the transparent pixels are the same? Sorry for my bad english, I am not a native english speaker. I hope you can understand what I want.
Code:


VB.NET:
[COLOR=#00008B]Imports[/COLOR] System.Drawing
[COLOR=#00008B]Imports[/COLOR] System.Runtime.CompilerServices
[COLOR=#00008B]Imports[/COLOR] System.Drawing.Imaging
[COLOR=#00008B]Imports[/COLOR] System.Runtime.InteropServices

[COLOR=#00008B]Module[/COLOR] BitmapExtension

    <Extension()>
    [COLOR=#00008B]Public[/COLOR] [COLOR=#00008B]Function[/COLOR] Contains(src [COLOR=#00008B]As[/COLOR] Bitmap, [COLOR=#00008B]ByRef[/COLOR] bmp [COLOR=#00008B]As[/COLOR] Bitmap) [COLOR=#00008B]As[/COLOR] Point
        [COLOR=gray]'[/COLOR]
        [COLOR=gray]'-- Some logic pre-checks[/COLOR]
        [COLOR=gray]'[/COLOR]
        [COLOR=#00008B]If[/COLOR] src [COLOR=#00008B]Is[/COLOR] [COLOR=#800000]Nothing[/COLOR] [COLOR=#00008B]OrElse[/COLOR] bmp [COLOR=#00008B]Is[/COLOR] [COLOR=#800000]Nothing[/COLOR] [COLOR=#00008B]Then[/COLOR] [COLOR=#00008B]Return[/COLOR] [COLOR=#800000]Nothing[/COLOR]

        [COLOR=#00008B]If[/COLOR] src.Width = bmp.Width [COLOR=#00008B]AndAlso[/COLOR] src.Height = bmp.Height [COLOR=#00008B]Then[/COLOR]
            [COLOR=#00008B]If[/COLOR] src.GetPixel([COLOR=#800000]0[/COLOR], [COLOR=#800000]0[/COLOR]) = bmp.GetPixel([COLOR=#800000]0[/COLOR], [COLOR=#800000]0[/COLOR]) [COLOR=#00008B]Then[/COLOR]
                [COLOR=#00008B]Return[/COLOR] [COLOR=#00008B]New[/COLOR] Point([COLOR=#800000]0[/COLOR], [COLOR=#800000]0[/COLOR])
            [COLOR=#00008B]Else[/COLOR]
                [COLOR=#00008B]Return[/COLOR] [COLOR=#800000]Nothing[/COLOR]
            [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]If[/COLOR]

        [COLOR=#00008B]ElseIf[/COLOR] src.Width < bmp.Width [COLOR=#00008B]OrElse[/COLOR] src.Height < bmp.Height [COLOR=#00008B]Then[/COLOR]
            [COLOR=#00008B]Return[/COLOR] [COLOR=#800000]Nothing[/COLOR]

        [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]If[/COLOR]
        [COLOR=gray]'[/COLOR]
        [COLOR=gray]'-- Prepare optimizations[/COLOR]
        [COLOR=gray]'[/COLOR]
        [COLOR=#00008B]Dim[/COLOR] sr [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]New[/COLOR] Rectangle([COLOR=#800000]0[/COLOR], [COLOR=#800000]0[/COLOR], src.Width, src.Height)
        [COLOR=#00008B]Dim[/COLOR] br [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]New[/COLOR] Rectangle([COLOR=#800000]0[/COLOR], [COLOR=#800000]0[/COLOR], bmp.Width, bmp.Height)

        [COLOR=#00008B]Dim[/COLOR] srcLock [COLOR=#00008B]As[/COLOR] BitmapData = src.LockBits(sr, Imaging.ImageLockMode.[COLOR=#00008B]ReadOnly[/COLOR], PixelFormat.Format24bppRgb)
        [COLOR=#00008B]Dim[/COLOR] bmpLock [COLOR=#00008B]As[/COLOR] BitmapData = bmp.LockBits(br, Imaging.ImageLockMode.[COLOR=#00008B]ReadOnly[/COLOR], PixelFormat.Format24bppRgb)

        [COLOR=#00008B]Dim[/COLOR] sStride [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR] = srcLock.Stride
        [COLOR=#00008B]Dim[/COLOR] bStride [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR] = bmpLock.Stride

        [COLOR=#00008B]Dim[/COLOR] srcSz [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR] = sStride * src.Height
        [COLOR=#00008B]Dim[/COLOR] bmpSz [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR] = bStride * bmp.Height

        [COLOR=#00008B]Dim[/COLOR] srcBuff(srcSz) [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Byte[/COLOR]
        [COLOR=#00008B]Dim[/COLOR] bmpBuff(bmpSz) [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Byte[/COLOR]

        Marshal.Copy(srcLock.Scan0, srcBuff, [COLOR=#800000]0[/COLOR], srcSz)
        Marshal.Copy(bmpLock.Scan0, bmpBuff, [COLOR=#800000]0[/COLOR], bmpSz)

        [COLOR=gray]' we don't need to lock the image anymore as we have a local copy[/COLOR]
        bmp.UnlockBits(bmpLock)
        src.UnlockBits(srcLock)

        [COLOR=#00008B]Dim[/COLOR] x, y, x2, y2, sx, sy, bx, by, sw, sh, bw, bh [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR]
        [COLOR=#00008B]Dim[/COLOR] r, g, b [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Byte[/COLOR]

        [COLOR=#00008B]Dim[/COLOR] p [COLOR=#00008B]As[/COLOR] Point = [COLOR=#800000]Nothing[/COLOR]

        bw = bmp.Width
        bh = bmp.Height

        sw = src.Width - bw      [COLOR=gray]' limit scan to only what we need. the extra corner[/COLOR]
        sh = src.Height - bh     [COLOR=gray]' point we need is taken care of in the loop itself.[/COLOR]

        bx = [COLOR=#800000]0[/COLOR] : by = [COLOR=#800000]0[/COLOR]
        [COLOR=gray]'[/COLOR]
        [COLOR=gray]'-- Scan source for bitmap[/COLOR]
        [COLOR=gray]'[/COLOR]
        [COLOR=#00008B]For[/COLOR] y = [COLOR=#800000]0[/COLOR] [COLOR=#00008B]To[/COLOR] sh
            sy = y * sStride
            [COLOR=#00008B]For[/COLOR] x = [COLOR=#800000]0[/COLOR] [COLOR=#00008B]To[/COLOR] sw

                sx = sy + x * [COLOR=#800000]3[/COLOR]
                [COLOR=gray]'[/COLOR]
                [COLOR=gray]'-- Find start point/pixel[/COLOR]
                [COLOR=gray]'[/COLOR]
                r = srcBuff(sx + [COLOR=#800000]2[/COLOR])
                g = srcBuff(sx + [COLOR=#800000]1[/COLOR])
                b = srcBuff(sx)

                [COLOR=#00008B]If[/COLOR] r = bmpBuff([COLOR=#800000]2[/COLOR]) [COLOR=#00008B]AndAlso[/COLOR] g = bmpBuff([COLOR=#800000]1[/COLOR]) [COLOR=#00008B]AndAlso[/COLOR] b = bmpBuff([COLOR=#800000]0[/COLOR]) [COLOR=#00008B]Then[/COLOR]
                    p = [COLOR=#00008B]New[/COLOR] Point(x, y)
                    [COLOR=gray]'[/COLOR]
                    [COLOR=gray]'-- We have a pixel match, check the region[/COLOR]
                    [COLOR=gray]'[/COLOR]
                    [COLOR=#00008B]For[/COLOR] y2 = [COLOR=#800000]0[/COLOR] [COLOR=#00008B]To[/COLOR] bh - [COLOR=#800000]1[/COLOR]
                        by = y2 * bStride
                        [COLOR=#00008B]For[/COLOR] x2 = [COLOR=#800000]0[/COLOR] [COLOR=#00008B]To[/COLOR] bw - [COLOR=#800000]1[/COLOR]
                            bx = by + x2 * [COLOR=#800000]3[/COLOR]

                            sy = (y + y2) * sStride
                            sx = sy + (x + x2) * [COLOR=#800000]3[/COLOR]

                            r = srcBuff(sx + [COLOR=#800000]2[/COLOR])
                            g = srcBuff(sx + [COLOR=#800000]1[/COLOR])
                            b = srcBuff(sx)

                            [COLOR=#00008B]If[/COLOR] [COLOR=#00008B]Not[/COLOR] (r = bmpBuff(bx + [COLOR=#800000]2[/COLOR]) [COLOR=#00008B]AndAlso[/COLOR]
                                    g = bmpBuff(bx + [COLOR=#800000]1[/COLOR]) [COLOR=#00008B]AndAlso[/COLOR]
                                    b = bmpBuff(bx)) [COLOR=#00008B]Then[/COLOR]
                                [COLOR=gray]'[/COLOR]
                                [COLOR=gray]'-- Not matching, continue checking[/COLOR]
                                [COLOR=gray]'[/COLOR]
                                p = [COLOR=#800000]Nothing[/COLOR]
                                sy = y * sStride
                                [COLOR=#00008B]Exit[/COLOR] [COLOR=#00008B]For[/COLOR]
                            [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]If[/COLOR]

                        [COLOR=#00008B]Next[/COLOR]
                        [COLOR=#00008B]If[/COLOR] p = [COLOR=#800000]Nothing[/COLOR] [COLOR=#00008B]Then[/COLOR] [COLOR=#00008B]Exit[/COLOR] [COLOR=#00008B]For[/COLOR]
                    [COLOR=#00008B]Next[/COLOR]
                [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]If[/COLOR] [COLOR=gray]'end of region check[/COLOR]

                [COLOR=#00008B]If[/COLOR] p <> [COLOR=#800000]Nothing[/COLOR] [COLOR=#00008B]Then[/COLOR] [COLOR=#00008B]Exit[/COLOR] [COLOR=#00008B]For[/COLOR]
            [COLOR=#00008B]Next[/COLOR]
            [COLOR=#00008B]If[/COLOR] p <> [COLOR=#800000]Nothing[/COLOR] [COLOR=#00008B]Then[/COLOR] [COLOR=#00008B]Exit[/COLOR] [COLOR=#00008B]For[/COLOR]
        [COLOR=#00008B]Next[/COLOR]

        bmpBuff = [COLOR=#800000]Nothing[/COLOR]
        srcBuff = [COLOR=#800000]Nothing[/COLOR]

        [COLOR=#00008B]Return[/COLOR] p

    [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Function[/COLOR]

[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Module[/COLOR]
 
This question was resolved on another forum: see here. Cocer, please do not cross post different forums, because it can waste the time of people who try to help you. If necessary, try one first and then another if you do not get the help you need. VicJ (aka boops boops).
 

Latest posts

Back
Top