c# - How to detect black Bullets in Image? -
given following image, how can detect black bullets (90 bullet) in image using c#, emgucv or aforge?
i tried use getpixel(x,y)
method checks pixel pixel, slow , need detect bullets not pixels.
algorithm/idea 1 can divide image in squares shown in sample:
with logic have check every 20th pixel. know first dot is, know every other dot have in same horizontal line (in provided sample).
a sample algorithm similar (please note need further improvement):
bitmap mybitmap = new bitmap ("input.png"); int skipx = 12; int skipy = 12; int detecteddots = 0; (int x = 0; x < mybitmap.width; x += skipx) { (int y = 0; y < mybitmap.height; y += skipy) { color pixelcolor = mybitmap.getpixel (x, y); if (pixelcolor.r + pixelcolor.g + pixelcolor.b == 0) { mybitmap.setpixel (x, y, color.red); detecteddots++; } } } mybitmap.save ("output.png"); console.writeline (detecteddots + " dots detected");
i added output can check dots got detected (all containing red pixels).
further improvement find center of dot. after should know width (and height) , can start @ first upper left dot offset of dots width.
algorithm 2 second algorithm analyses each pixel , lot easier implement. there's black pixel checks if there black pixel in same vertical or horizontal line before , skips in case until there no black pixel in line.
further improvement store height of first dot , make condition in middle of snippet more beautiful.
stopwatch watch = new stopwatch(); watch.start(); bitmap mybitmap = new bitmap ("input.png"); int dotsdetected = 0; list<int> xfound = new list<int>(); (int x = 0; x < mybitmap.width; x++) { bool yfound = false; bool dotfound = false; (int y = 0; y < mybitmap.height; y++) { color pixelcolor = mybitmap.getpixel (x, y); if (pixelcolor.r + pixelcolor.g + pixelcolor.b == 0) { dotfound = true; if (yfound) continue; if (xfound.contains (y) || xfound.contains (y + 1) || xfound.contains (y + 2) || xfound.contains (y + 3) || xfound.contains (y + 4) || xfound.contains (y - 1) || xfound.contains (y - 2) || xfound.contains (y - 3) || xfound.contains (y - 4)) { yfound = true; continue; } xfound.add (y); //mybitmap.setpixel (x, y, color.red); dotsdetected++; yfound = true; } else yfound = false; } if(!dotfound) //no dot found in line xfound.clear(); } //mybitmap.save ("output.png"); watch.stop(); console.writeline("picture analyzed in " + watch.elapsed.totalseconds.tostring("#,##0.0000")); console.writeline (dotsdetected + " dots detected");
Comments
Post a Comment