How to segmentation object c# -


i used code segmentation, i'm trying detect pixels 1 one because object binary, not grayscale. when run program, draws 2 object. first object drawn (object still has black color , red rectangle), second object fails drawn. screenshot here. please me, why happen?

#region edge detection  private void btnsegmentasi_click(object sender, eventargs e) {     segments = new list<imagesegment>();     bitmap bmp = (bitmap)pb2.image;     imagearea = new rectangle(0, 0, pb2.image.width - 1, pb2.image.height - 1);     (int y = 0; y < pb2.image.height; y++)     {         (int x = 0; x < pb2.image.width; x++)         {             bool skip = false;             foreach (imagesegment segment in segments)             {                 if (pointisinrect(x, y, segment.rect))                 {                     skip = true;                     break;                 }             }             if (skip) continue;             color warna = bmp.getpixel(x, y);             if (warna.g == 0)                 startedgedetection(x, y, ref bmp);         }     }      dgvproses.datasource = segments;      if (segments.count > 0)     {         graphics g = pb2.creategraphics();         rectangle[] rects = (from thesegment in segments select thesegment.rect).toarray();         g.drawrectangles(new pen(brushes.red), rects);         g.dispose();     } }  private void startedgedetection(int x, int y, ref bitmap bmp) {     point startpoint = new point(x, y);     point currpoint = new point(x, y);      int sudut = 180;     int xmin = x, ymin = y, xmax = x, ymax = y;         {         sudut -= 45;         point offset = angletopoint(ref sudut);         point trialpoint = new point(currpoint.x + offset.x, currpoint.y + offset.y);         if (!pointisinrect(trialpoint.x, trialpoint.y, imagearea))             continue;         color thecolor = bmp.getpixel(trialpoint.x, trialpoint.y);         if (thecolor.g == 0)         {             currpoint = trialpoint;             sudut -= 180;             if (currpoint.x > xmax)                 xmax = currpoint.x;             else if (currpoint.x < xmin)                 xmin = currpoint.x;             if (currpoint.y > ymax)                 ymax = currpoint.y;             else if (currpoint.y < ymin)                 ymin = currpoint.y;             if (sudut < 0)                 sudut += 360;             if (currpoint == startpoint && sudut == 180)                 break;         }     }     while (!(currpoint == startpoint && sudut == 180));     rectangle r = new rectangle(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);     bitmap newimage = new bitmap(r.width + 2, r.height + 2);     using (graphics g = graphics.fromimage(newimage))     {         g.fillrectangle(brushes.white, 0, 0, newimage.width, newimage.height);         g.drawimage(bmp, new rectangle(1, 1, r.width, r.height), r, graphicsunit.pixel);         g.dispose();     }     segments.add(new imagesegment(r, newimage)); }  private point angletopoint(ref int sudut) {     if (sudut < 0)         sudut += 360;     switch (sudut)     {         case 135: return new point(-1, -1);         case 90: return new point(0, -1);         case 45: return new point(1, -1);         case 0: return new point(1, 0);         case 315: return new point(1, 1);         case 270: return new point(0, 1);         case 225: return new point(-1, 1);         default: return new point(-1, 0);     } }  private bool pointisinrect(int x, int y, rectangle rect) {     if (x < rect.x)         return false;     if (x > rect.x + rect.width)         return false;     if (x < rect.y)         return false;     if (x > rect.y + rect.height)         return false;     return true;  } #endregion 

okay, think i've got clue of how algorithm supposed work. i'd guess running around in circles within object. not know why not happen first object, story.

when enter startedgedetection start @ point, check if it's black, move angle , repeat whole procedure. stop when current point reaches starting point. crux is, algorithm not guarantee walk whole object, may following (i not know this, pretty much):

oooooo o####o o####o oooooo  oooooo o*###o o####o oooooo  oooooo o**##o o####o oooooo  oooooo o**##o o#*##o oooooo  oooooo o**##o o**##o oooooo  o = pixels filled white # = pixels filled black * = pixels stepped through 

you've reached starting point again , algorithm stops, bounding box not contain whole object, part. if of objects bounding boxes have either width or height of 1 fill whole object bounding boxes, hence appears red.

you'll have fix startedgedetection avoid described case , make sure detect edge.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -