bitmap - Vb.Net Check If Image Existing In Another Image -
i tried check if part of image existing in image
explanation:
full image:
http://imageshack.us/photo/my-images/526/part1g.png/
second part of image want check if existing in full image:
http://imageshack.us/photo/my-images/706/part2p.png/
if second part exist function return true
there function can check if exist?
(if single pixel verey easy, want check if part of image existing in image)
there code works , check if single pixel exist in image:
dim bmp bitmap = picturebox1.image x integer = 0 bmp.width - 1 y integer = 0 bmp.height - 1 if bmp.getpixel(x, y) = color.fromargb(48, 48, 48) msgbox("pixel exist in image!!!") end if next next
i wrote extension find image within image. has couple of limitations though: 1) images must saved without color-space (or same ones anyways) , 2) not work lossy-compressed images (ie. jpeg, need averaging , tolerance implementation).
i have optimized pixel matching routine. it's not debugged, seem work expected. ignores alpha-channel (on purpose, extend needed) , need try-catch on caller (ie. out-of-memory exception).
usage:
dim p point = yourbitmap.contains(bmpyoulookfor) if p <> nothing '... end if the code: if don't want have extension (which requires .net 3.5+) remove extension attribute , call ordinary function source bitmap argument instead.
copy , paste following module (license: cc-attribution):
'******************************************************************************* '* '* epistemex '* '* bitmap extension: .contains(bmp) '* kf '* '* 2012-09-26 initial version '* 2012-09-26 minor optimization, exit for's impl. '* '******************************************************************************* imports system.drawing imports system.runtime.compilerservices imports system.drawing.imaging imports system.runtime.interopservices module bitmapextension <extension()> public function contains(src bitmap, byref bmp bitmap) point ' '-- logic pre-checks ' if src nothing orelse bmp nothing return nothing if src.width = bmp.width andalso src.height = bmp.height if src.getpixel(0, 0) = bmp.getpixel(0, 0) return new point(0, 0) else return nothing end if elseif src.width < bmp.width orelse src.height < bmp.height return nothing end if ' '-- prepare optimizations ' dim sr new rectangle(0, 0, src.width, src.height) dim br new rectangle(0, 0, bmp.width, bmp.height) dim srclock bitmapdata = src.lockbits(sr, imaging.imagelockmode.readonly, pixelformat.format24bpprgb) dim bmplock bitmapdata = bmp.lockbits(br, imaging.imagelockmode.readonly, pixelformat.format24bpprgb) dim sstride integer = srclock.stride dim bstride integer = bmplock.stride dim srcsz integer = sstride * src.height dim bmpsz integer = bstride * bmp.height dim srcbuff(srcsz) byte dim bmpbuff(bmpsz) byte marshal.copy(srclock.scan0, srcbuff, 0, srcsz) marshal.copy(bmplock.scan0, bmpbuff, 0, bmpsz) ' don't need lock image anymore have local copy bmp.unlockbits(bmplock) src.unlockbits(srclock) dim x, y, x2, y2, sx, sy, bx, by, sw, sh, bw, bh integer dim r, g, b byte dim p point = nothing bw = bmp.width bh = bmp.height sw = src.width - bw ' limit scan need. corner sh = src.height - bh ' point need taken care of in loop itself. bx = 0 : = 0 ' '-- scan source bitmap ' y = 0 sh sy = y * sstride x = 0 sw sx = sy + x * 3 ' '-- find start point/pixel ' r = srcbuff(sx + 2) g = srcbuff(sx + 1) b = srcbuff(sx) if r = bmpbuff(2) andalso g = bmpbuff(1) andalso b = bmpbuff(0) p = new point(x, y) ' '-- have pixel match, check region ' y2 = 0 bh - 1 = y2 * bstride x2 = 0 bw - 1 bx = + x2 * 3 sy = (y + y2) * sstride sx = sy + (x + x2) * 3 r = srcbuff(sx + 2) g = srcbuff(sx + 1) b = srcbuff(sx) if not (r = bmpbuff(bx + 2) andalso g = bmpbuff(bx + 1) andalso b = bmpbuff(bx)) ' '-- not matching, continue checking ' p = nothing sy = y * sstride exit end if next if p = nothing exit next end if 'end of region check if p <> nothing exit next if p <> nothing exit next bmpbuff = nothing srcbuff = nothing return p end function end module
Comments
Post a Comment