1:  public partial class Form1 : Form {
   2:   
   3:          public enum eColor { roof, lawn, garden, street, tree, unknown, mix };
   4:   
   5:          public static string colorValue_white = "ffffffff";
   6:   
   7:          public int MaskSize = 3;
   8:   
   9:          Dictionary<Color, eColor> myDictionary = new Dictionary<Color, eColor>();
  10:   
  11:          public Form1() {
  12:              InitializeComponent();
  13:          }
  14:   
  15:          private void Form1_Load(object sender, EventArgs e) {
  16:   
  17:          }
  18:   
  19:          private Bitmap SwitchPixel(Bitmap bmOriginal) {
  20:   
  21:              // Create a Bitmap object from a file.
  22:              Bitmap myBitmap = new Bitmap(bmOriginal.Width, bmOriginal.Height);
  23:   
  24:              Color cOut = Color.White;
  25:              // cOld = Color.White;
  26:   
  27:              // Set each pixel in myBitmap to the right default color
  28:              for (int Xcount = 0; Xcount < bmOriginal.Width; Xcount++) {
  29:                  for (int Ycount = 0; Ycount < bmOriginal.Height; Ycount++) {
  30:                      // Get pixel color 
  31:                      Color c = bmOriginal.GetPixel(Xcount, Ycount);
  32:   
  33:                      if (myDictionary.ContainsKey(c)) {
  34:                          if (c.Name == colorValue_white) {
  35:                              cOut = Color.Black;
  36:                          }
  37:                          else {
  38:                              switch (myDictionary[c]) {
  39:                                  case eColor.lawn:
  40:                                      cOut = Color.Green;
  41:                                      break;
  42:                                  case eColor.roof:
  43:                                      cOut = Color.Red;
  44:                                      break;
  45:                                  case eColor.street:
  46:                                      cOut = Color.Gray;
  47:                                      break;
  48:                                  case eColor.garden:
  49:                                      cOut = Color.Pink;
  50:                                      break;
  51:                                  case eColor.tree:
  52:                                      cOut = Color.DarkOliveGreen;
  53:                                      break;
  54:                                  case eColor.mix:
  55:                                      cOut = Color.Orange;
  56:                                      break;
  57:                                  default:
  58:                                      cOut = Color.White;
  59:                                      break;
  60:                              }
  61:                          }
  62:                      }
  63:                      else {
  64:                          cOut = Color.White;
  65:                      }
  66:                      myBitmap.SetPixel(Xcount, Ycount, cOut);
  67:                  }
  68:              }
  69:              return myBitmap;
  70:          }
  71:   
  72:          private Bitmap FillAsLawn(Bitmap bmOriginal) {
  73:   
  74:              // Create a Bitmap object from a file.
  75:              Bitmap myBitmap = new Bitmap(bmOriginal.Width, bmOriginal.Height);
  76:   
  77:              // Set each pixel in myBitmap to the right default color
  78:              for (int Xcount = 0; Xcount < bmOriginal.Width; Xcount++) {
  79:                  for (int Ycount = 0; Ycount < bmOriginal.Height; Ycount++) {
  80:                      Color c = bmOriginal.GetPixel(Xcount, Ycount);
  81:                      if (c.ToArgb() == Color.White.ToArgb()) { c = Color.Green; }
  82:                      myBitmap.SetPixel(Xcount, Ycount, c);
  83:                  }
  84:              }
  85:              return myBitmap;
  86:          }
  87:   
  88:          private Bitmap RemoveSaltAndPepperRumor(Bitmap bmOriginal) {
  89:              int WhitePx = 0;
  90:   
  91:              // Create a Bitmap object from a file.
  92:              Bitmap myBitmap = new Bitmap(bmOriginal.Width, bmOriginal.Height);
  93:              // Set each pixel in myBitmap to the right default color
  94:              for (int Xcount = 0 + MaskSize; Xcount < (bmOriginal.Width - MaskSize); Xcount++) {
  95:                  for (int Ycount = 0 + MaskSize; Ycount < (bmOriginal.Height - MaskSize); Ycount++) {
  96:                      // Get pixel color 
  97:                      Color c = bmOriginal.GetPixel(Xcount, Ycount);
  98:                      // Se il colore  arancione (mix color)
  99:                      // Oppure bianco (che non  di nessuno)
 100:                      // entro nella maschera che mi decide di chi  il px
 101:                      if ((c.ToArgb() == Color.Orange.ToArgb()) || (c.ToArgb() == Color.White.ToArgb())) {
 102:                          List<Color> tempList = new List<Color>(MaskSize * 4);
 103:                          int HalfMaskSize = MaskSize / 2;
 104:                          bool RightColor = false;
 105:                          // Finch non trovo un colore che mi soddisfa resto nel ciclo
 106:                          int maxValue = 0;
 107:                          try {
 108:                              while (RightColor == false) {
 109:                                  // Riempio una lista con i colori della maschera
 110:                                  // In base alla maschera parto da un 3x3 poi faccio un 5x5 ed infine un 7x7
 111:                                  for (int i = (Xcount - HalfMaskSize); i <= (Xcount + HalfMaskSize); i++) {
 112:                                      for (int j = (Ycount - HalfMaskSize); j <= (Ycount + HalfMaskSize); j++) {
 113:                                          Color tempColor = bmOriginal.GetPixel(i, j);
 114:                                          tempList.Add(tempColor);
 115:                                      }
 116:                                  }
 117:                                  // In base alla lista che gli passo mi restituisce il colore maggiore
 118:                                  c = MediumFilter(tempList);
 119:                                  // Se il colore mi fa bene esco dal ciclo
 120:                                  // Se invece il colore non va bene (poich  arancio o bianco)
 121:                                  // Allargo la maschera e riprovo
 122:                                  // quando sono al max (ovvero la maschera 7x7 setto il colore di prato 
 123:                                  if ((c.ToArgb() != Color.Orange.ToArgb()) && (c.ToArgb() != Color.White.ToArgb())) {
 124:                                      RightColor = true;
 125:                                  }
 126:                                  if (HalfMaskSize > 2) {
 127:                                      c = Color.White;
 128:                                      WhitePx++;
 129:                                      //c = Color.Green;
 130:                                      break;
 131:                                  }
 132:                                  // Controllo che la maschera non sfori i bordi!
 133:                                  if (Xcount + HalfMaskSize + 2 > bmOriginal.Width) { break; }
 134:                                  if (Ycount + HalfMaskSize + 2 > bmOriginal.Height) { break; }
 135:                                  HalfMaskSize += 1;
 136:   
 137:                                  maxValue++;
 138:                                  if (maxValue > 50) {
 139:                                      break;
 140:                                  }
 141:                              }
 142:                              tempList.Clear();
 143:                          }
 144:                          catch { c = Color.Lime; }
 145:                      }
 146:                      myBitmap.SetPixel(Xcount, Ycount, c);
 147:                  }
 148:              }
 149:   
 150:              lblCountWhitePx.Text = lblCountWhitePx.Text + " , " + WhitePx;
 151:              return myBitmap;
 152:          }
 153:   
 154:          private Color MediumFilter(List<Color> myList) {
 155:   
 156:              int countGreen = 0;
 157:              int countRed = 0;
 158:              int countGray = 0;
 159:              int countPink = 0;
 160:              int countDarkOliveGreen = 0;
 161:              int countOrange = 0;
 162:              int countWhite = 0;
 163:              int countBlack = 0;
 164:   
 165:              foreach (Color c in myList) {
 166:                  if (c.ToArgb() == Color.Black.ToArgb()) { countBlack++; continue; }
 167:                  if (c.ToArgb() == Color.Green.ToArgb()) { countGreen++; continue; }
 168:                  if (c.ToArgb() == Color.Red.ToArgb()) { countRed++; continue; }
 169:                  if (c.ToArgb() == Color.Gray.ToArgb()) { countGray++; continue; }
 170:                  if (c.ToArgb() == Color.Pink.ToArgb()) { countPink++; continue; }
 171:                  if (c.ToArgb() == Color.DarkOliveGreen.ToArgb()) { countDarkOliveGreen++; continue; }
 172:                  if (c.ToArgb() == Color.Orange.ToArgb()) { countOrange++; continue; }
 173:                  if (c.ToArgb() == Color.White.ToArgb()) { countWhite++; continue; }
 174:              }
 175:   
 176:              for (int i = myList.Count; i >= 0; i--) {
 177:                  if (countBlack == i) return Color.Black;
 178:                  if (countGreen == i) return Color.Green;
 179:                  if (countRed == i) return Color.Red;
 180:                  if (countGray == i) return Color.Gray;
 181:                  if (countPink == i) return Color.Pink;
 182:                  if (countDarkOliveGreen == i) return Color.DarkOliveGreen;
 183:                  if (countOrange == i) return Color.Orange;
 184:                  if (countWhite == i) return Color.White;
 185:              }
 186:   
 187:              return Color.Gold;
 188:   
 189:   
 190:   
 191:   
 192:   
 193:          }
 194:   
 195:          private void SetDictionaryColor(Bitmap _bitmap, eColor _eColorName) {
 196:              for (int x = 0; x < _bitmap.Width; x++) {
 197:                  for (int y = 0; y < _bitmap.Height; y++) {
 198:                      // Get pixel color 
 199:                      Color c = _bitmap.GetPixel(x, y);
 200:                      // ho una variabile dizionario con la coppia Colore - Costante
 201:                      // Per ogni px guardo se il colore preso  nel dizionario ...
 202:                      // se c' faccio un ulteriore controllo, se non c' lo aggiungo nel dizionario.
 203:                      if (myDictionary.ContainsKey(c)) {
 204:                          eColor tempvalue = myDictionary[c];
 205:                          // se il valore  gi presente:
 206:                          // se il suo enum colore  uguale a passato nella funzione - tutto bene non faccio niente (non ci sono conflitti)
 207:                          // se  diverso allo vuol dire che c' stato un conflito (setto la coppia colore-mix per indicare che questo colore 
 208:                          //  condiviso tra pi aree)  
 209:                          if (tempvalue.Equals(_eColorName) == false) {
 210:                              myDictionary[c] = eColor.mix;
 211:                          }
 212:                      }
 213:                      else {
 214:                          myDictionary.Add(c, _eColorName);
 215:                      }
 216:                  }
 217:              }
 218:          }
 219:   
 220:          private void button1_Click(object sender, EventArgs e) {
 221:   
 222:              try {
 223:                  // Immagine originale
 224:                  Bitmap bmOriginal = new Bitmap(picOriginal.Image);
 225:   
 226:                  // Tetto
 227:                  Bitmap bmRoof = new Bitmap(picColorsRoot.Image);
 228:                  SetDictionaryColor(bmRoof, eColor.roof);
 229:                  bmRoof = null;
 230:   
 231:                  // Orto
 232:                  Bitmap bmGarden = new Bitmap(picColorsGarden.Image);
 233:                  SetDictionaryColor(bmGarden, eColor.garden);
 234:                  bmGarden = null;
 235:   
 236:                  // Prato
 237:                  Bitmap bmLawn = new Bitmap(picColorsLawn.Image);
 238:                  SetDictionaryColor(bmLawn, eColor.lawn);
 239:                  bmLawn = null;
 240:   
 241:                  // Strada
 242:                  Bitmap bmStreet = new Bitmap(picColorsStreet.Image);
 243:                  SetDictionaryColor(bmStreet, eColor.street);
 244:                  bmStreet = null;
 245:   
 246:                  // Alberi
 247:                  Bitmap bmTree = new Bitmap(picColorsTree.Image);
 248:                  SetDictionaryColor(bmTree, eColor.tree);
 249:                  bmTree = null;
 250:   
 251:                  // Divide l'immagine nei diversi colori in base al colore
 252:                  picFirstOutput.Image = SwitchPixel(bmOriginal);
 253:                  myDictionary = null;
 254:   
 255:                  // Immagine Originale
 256:                  Bitmap bmStep1 = new Bitmap(picFirstOutput.Image);
 257:                  picFirstOutput.Image = picMedianFilter.Image = RemoveSaltAndPepperRumor(bmStep1);
 258:   
 259:                  // Ciclo del filtro
 260:                  for (int i = 0; i < 20; i++) {
 261:                      Bitmap bmStep = new Bitmap(picMedianFilter.Image);
 262:                      picMedianFilter.Image = RemoveSaltAndPepperRumor(bmStep);
 263:                      picMedianFilter.Refresh();
 264:                      lblFitro.Text = "filtro n:" + i;
 265:                      lblFitro.Refresh();
 266:                      bmStep.Save("C:\\Francesco\\RegionGrowing" + i + ".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
 267:                  }
 268:   
 269:                  //Ultimo giro del filtro
 270:                  Bitmap bmLastStep = new Bitmap(picMedianFilter.Image);
 271:                  picLastMedianFilter.Image = FillAsLawn(bmLastStep);
 272:   
 273:                  Bitmap bmSaveLastStep = new Bitmap(picLastMedianFilter.Image);
 274:                  bmSaveLastStep.Save("C:\\Francesco\\RegionGrowingLast.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
 275:                  bmSaveLastStep.Save("C:\\Francesco\\RegionGrowingLast.Bmp", System.Drawing.Imaging.ImageFormat.Bmp);
 276:                  
 277:                  //---------------------
 278:   
 279:                  CalculateArea(bmSaveLastStep);
 280:   
 281:              }
 282:              catch {
 283:   
 284:              }
 285:   
 286:          }
 287:   
 288:          private void button2_Click(object sender, EventArgs e) {
 289:   
 290:              // Immagine originale
 291:              Bitmap bmOriginal = new Bitmap(picOriginal.Image);
 292:              int NpxImmagine = bmOriginal.Width * bmOriginal.Height;
 293:              textBox1.AppendText("px totali immagine originale: " + NpxImmagine  + Environment.NewLine);
 294:              
 295:              // Immagine legenda
 296:              Bitmap bmLegend = new Bitmap(picLegend.Image);
 297:              int NpxLegenda = bmLegend.Width * bmLegend.Height;
 298:              textBox1.AppendText("px totali legenda: " + NpxLegenda + Environment.NewLine);
 299:              
 300:              //Ultimo giro del filtro
 301:              Bitmap bmLastStep = new Bitmap(picLastMedianFilter.Image);
 302:              int NpxOutPutImages = bmLastStep.Width * bmLastStep.Height;
 303:              textBox1.AppendText("px totali immagine finale: " + NpxOutPutImages + Environment.NewLine);
 304:   
 305:              CalculateArea(bmLastStep);
 306:   
 307:          }
 308:   
 309:          public void CalculateArea(Bitmap bmOriginal) {
 310:   
 311:              int total = 0;
 312:              int countGreen = 0;
 313:              int countRed = 0;
 314:              int countGray = 0;
 315:              int countPink = 0;
 316:              int countDarkOliveGreen = 0;
 317:              int countOrange = 0;
 318:              int countWhite = 0;
 319:              int countBlack = 0;
 320:              int error = 0;
 321:   
 322:              // Set each pixel in myBitmap to the right default color
 323:              for (int Xcount = 0; Xcount < bmOriginal.Width; Xcount++) {
 324:                  for (int Ycount = 0; Ycount < bmOriginal.Height; Ycount++) {
 325:                      total++;
 326:                      // Get pixel color 
 327:                      Color c = bmOriginal.GetPixel(Xcount, Ycount);
 328:                      if (c.ToArgb() == Color.Black.ToArgb()) { countBlack++; continue; }
 329:                      if (c.ToArgb() == Color.Green.ToArgb()) { countGreen++; continue; }
 330:                      if (c.ToArgb() == Color.Red.ToArgb()) { countRed++; continue; }
 331:                      if (c.ToArgb() == Color.Gray.ToArgb()) { countGray++; continue; }
 332:                      if (c.ToArgb() == Color.Pink.ToArgb()) { countPink++; continue; }
 333:                      if (c.ToArgb() == Color.DarkOliveGreen.ToArgb()) { countDarkOliveGreen++; continue; }
 334:                      if (c.ToArgb() == Color.Orange.ToArgb()) { countOrange++; continue; }
 335:                      if (c.ToArgb() == Color.White.ToArgb()) { countWhite++; continue; }
 336:                      error++;
 337:                  }
 338:              }
 339:   
 340:              textBox1.AppendText(Environment.NewLine);
 341:                          
 342:   
 343:              textBox1.AppendText("countGreen: " + countGreen + Environment.NewLine);
 344:              textBox1.AppendText("countRed: " + countRed + Environment.NewLine);
 345:              textBox1.AppendText("countGray: " + countGray + Environment.NewLine);
 346:              textBox1.AppendText("countPink: " + countPink + Environment.NewLine);
 347:              textBox1.AppendText("countDarkOliveGreen: " + countDarkOliveGreen + Environment.NewLine);
 348:              textBox1.AppendText("countOrange: " + countOrange + Environment.NewLine);
 349:              textBox1.AppendText("countWhite: " + countWhite + Environment.NewLine);
 350:              textBox1.AppendText("countBlack: " + countBlack + Environment.NewLine);
 351:              textBox1.AppendText("error: " + error + Environment.NewLine);
 352:              textBox1.AppendText("totalPx: " + total + Environment.NewLine);
 353:   
 354:   
 355:          }
 356:          
 357:      }