c# get rectangle bounds from image -


lets have image enter image description here

i find black rectangle bounds(right,left,width,height) in image(lets there's no other black pixels in image). code far is:

    private unsafe bitmap getdiffbitmap(bitmap bmp)         {               bmdata = bmp.lockbits(new rectangle(0, 0, bmp.width, bmp.height), system.drawing.imaging.imagelockmode.readonly, bmp.pixelformat);              intptr scan0 = bmdata.scan0;               int stride = bmdata.stride;               int nwidth = bmp.width;             int nheight = bmp.height;              for(int y = 0; y < nheight; y++)              {                 //define pointers inside first loop parallelizing                 byte* p = (byte*)scan0.topointer();                 p += y * stride;                    (int x = 0; x < nwidth; x++)                 {                     //always complete pixel when differences found                      if(p[0]==0 && p[1]==0 && p[2]==0)                     {                             // p[0] = 255;                            // p[1] = 255;                            // p[2] =255;                              right = nwidth;//geting position of lastest black pixel;                        }                          p += 4;                   }              }                bmp.unlockbits(bmdata);               return bmp;         } 

its seems nwidth image width-so not working.. got acces these pixels , can change them dont know why can count them , find proper bounds of black rectangle... if me apperciate it,

there few issues kind of image analysis:

1 replace

if(p[0]==0 && p[1]==0 && p[2]==0) {     right = nwidth;//geting position of lastest black pixel; } 

with

if(p[0]==0 && p[1]==0 && p[2]==0) {      right = x; //geting position of lastest black pixel; }   

your x-iteration variable counts pixel on.

2 code provided works 32bpp pixel formats. if on purpose should check if bitmap analysing in compatible format.

3 compressed image formats won't 0 on 3 color channels black, should "less dark" check instead of zero.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -