private void labelImage(LABEL_PRED pred, Point click) { int width = dispBmp.Width, height = dispBmp.Height; float threshold = 0F, deviation = 0F, startGray = 0F; BitmapData srcData = dispBmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); Bitmap polar = new Bitmap(width, height); BitmapData polarData = polar.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); System.IntPtr srcPtr = srcData.Scan0; System.IntPtr polarPtr = polarData.Scan0; int nBytes = width * height * 3; byte[] srcColors = new byte[nBytes]; byte[] polarColors = new byte[nBytes]; Marshal.Copy(srcPtr, srcColors, 0, nBytes); switch (pred) { case LABEL_PRED.THRESHOLD: threshold = getThreshold(srcColors, 1.0F); break; case LABEL_PRED.DEVIANCE: deviation = getDeviation(srcColors); int index = click.X * 3 + click.Y * width * 3; startGray = (float)((int)srcColors[index + 2] + (int)srcColors[index + 1] + (int)srcColors[index]) / 3F; break; } int xLim = width * 3; for (int y = 0; y < height; ++y) { for (int x = 0; x < xLim; x += 3) { int i = x + y * xLim; byte grayVal = (byte)(((int)srcColors[i + 2] + (int)srcColors[i + 1] + (int)srcColors[i]) / 3); bool fill = false; switch (pred) { case LABEL_PRED.THRESHOLD: fill = (grayVal > threshold); break; case LABEL_PRED.DEVIANCE: fill = (Math.Abs(startGray - grayVal) < deviation); break; } if (fill) { polarColors[i + 2] = 255; polarColors[i + 1] = 255; polarColors[i] = 255; } else { polarColors[i + 2] = 0; polarColors[i + 1] = 0; polarColors[i] = 0; } } } Marshal.Copy(polarColors, 0, polarPtr, nBytes); dispBmp.UnlockBits(srcData); polar.UnlockBits(polarData); idTable = new int[width * height]; polarData = polar.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); Bitmap objs = new Bitmap(width, height); BitmapData objsData = objs.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); polarPtr = polarData.Scan0; System.IntPtr objsPtr = objsData.Scan0; byte[] objsColors = new byte[nBytes]; Marshal.Copy(polarPtr, polarColors, 0, nBytes); toolStripProgressBar1.Visible = true; nObjects = 0; for (int y = 0; y < height; ++y) { toolStripProgressBar1.Value = (int)((float)100 * (float)y / (float)height); for (int x = 0; x < xLim; x += 3) { int colorIndexA = x + y * xLim; int colorIndexB = (x - 3) + y * xLim; int colorIndexC = x + (y - 1) * xLim; int colorIndexD = (x - 3) + (y - 1) * xLim; int labelIndexA = x / 3 + y * width; int labelIndexB = (x / 3 - 1) + y * width; int labelIndexC = x / 3 + (y - 1) * width; int labelIndexD = (x / 3 - 1) + (y - 1) * width; int labeled = -1, oldLabel = -1; bool runUpdate = false; // check 1: top left (D) if (x != 0 && y != 0) { if (polarColors[colorIndexA] == polarColors[colorIndexD]) { idTable[labelIndexA] = idTable[labelIndexD]; labeled = idTable[labelIndexA]; } } // check 2: top (C) if (y != 0) { if (polarColors[colorIndexA] == polarColors[colorIndexC]) { idTable[labelIndexA] = idTable[labelIndexC]; if (labeled != -1) { if (labeled != idTable[labelIndexA]) { runUpdate = true; oldLabel = Math.Max(labeled, idTable[labelIndexA]); labeled = Math.Min(labeled, idTable[labelIndexA]); } } else { labeled = idTable[labelIndexA]; } } } // check 3: left (B) if (x != 0) { if (polarColors[colorIndexA] == polarColors[colorIndexB]) { idTable[labelIndexA] = idTable[labelIndexB]; if (labeled != -1) { if (labeled != idTable[labelIndexA]) { runUpdate = true; oldLabel = Math.Max(labeled, idTable[labelIndexA]); labeled = Math.Min(labeled, idTable[labelIndexA]); } } else { labeled = idTable[labelIndexA]; } } } // if no friends, give new label if (labeled == -1) idTable[labelIndexA] = nObjects++; if (runUpdate) updateLabels(labeled, oldLabel, x, y); } } int selectedID = idTable[click.X + click.Y * width]; for (int y = 0; y < height; ++y) { for (int x = 0; x < xLim; x += 3) { int colorIndex = x + y * xLim; int idIndex = x / 3 + y * width; switch (pred) { case LABEL_PRED.THRESHOLD: objsColors[colorIndex + 2] = (byte)((977 * idTable[idIndex]) + 173 % 256); objsColors[colorIndex + 1] = (byte)((644 * idTable[idIndex]) + 45 % 256); objsColors[colorIndex] = (byte)((311 * idTable[idIndex]) + 247 % 256); break; case LABEL_PRED.DEVIANCE: if (idTable[idIndex] == selectedID) { objsColors[colorIndex + 2] = (!dispBinary) ? (byte)0 : (byte)255; objsColors[colorIndex + 1] = 255; objsColors[colorIndex] = (!dispBinary) ? (byte)0 : (byte)255; } else if (!dispBinary) { objsColors[colorIndex + 2] = srcColors[colorIndex + 2]; objsColors[colorIndex + 1] = srcColors[colorIndex + 1]; objsColors[colorIndex] = srcColors[colorIndex]; } break; } } } Marshal.Copy(objsColors, 0, objsPtr, nBytes); polar.UnlockBits(polarData); objs.UnlockBits(objsData); pictureBox1.Image = objs; toolStripProgressBar1.Visible = false; switch (pred) { case LABEL_PRED.THRESHOLD: toolStripStatusLabel1.Text = "Objects: " + nObjects.ToString(); toolStripStatusLabel2.Text = "Threshold: " + threshold.ToString(); break; case LABEL_PRED.DEVIANCE: toolStripStatusLabel1.Text = "Object Selected: " + selectedID.ToString(); toolStripStatusLabel2.Text = "Deviance: " + deviation.ToString(); break; } }
private void labelImage(LABEL_PRED pred, Point click) { int width = dispBmp.Width, height = dispBmp.Height; float threshold = 0F, deviation = 0F, startGray = 0F; BitmapData srcData = dispBmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); Bitmap polar = new Bitmap(width, height); BitmapData polarData = polar.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); System.IntPtr srcPtr = srcData.Scan0; System.IntPtr polarPtr = polarData.Scan0; int nBytes = width * height * 3; byte[] srcColors = new byte[nBytes]; byte[] polarColors = new byte[nBytes]; Marshal.Copy(srcPtr, srcColors, 0, nBytes); switch (pred) { case LABEL_PRED.THRESHOLD: threshold = getThreshold(srcColors, 1.0F); break; case LABEL_PRED.DEVIANCE: deviation = getDeviation(srcColors); int index = click.X * 3 + click.Y * width * 3; startGray = (float)((int)srcColors[index + 2] + (int)srcColors[index + 1] + (int)srcColors[index]) / 3F; break; } int xLim = width * 3; for (int y = 0; y < height; ++y) { for (int x = 0; x < xLim; x += 3) { int i = x + y * xLim; byte grayVal = (byte)(((int)srcColors[i + 2] + (int)srcColors[i + 1] + (int)srcColors[i]) / 3); bool fill = false; switch (pred) { case LABEL_PRED.THRESHOLD: fill = (grayVal > threshold); break; case LABEL_PRED.DEVIANCE: fill = (Math.Abs(startGray - grayVal) < deviation); break; } if (fill) { polarColors[i + 2] = 255; polarColors[i + 1] = 255; polarColors[i] = 255; } else { polarColors[i + 2] = 0; polarColors[i + 1] = 0; polarColors[i] = 0; } } } Marshal.Copy(polarColors, 0, polarPtr, nBytes); dispBmp.UnlockBits(srcData); polar.UnlockBits(polarData); idTable = new int[width * height]; polarData = polar.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); Bitmap objs = new Bitmap(width, height); BitmapData objsData = objs.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); polarPtr = polarData.Scan0; System.IntPtr objsPtr = objsData.Scan0; byte[] objsColors = new byte[nBytes]; Marshal.Copy(polarPtr, polarColors, 0, nBytes); toolStripProgressBar1.Visible = true; nObjects = 0; for (int y = 0; y < height; ++y) { toolStripProgressBar1.Value = (int)((float)100 * (float)y / (float)height); for (int x = 0; x < xLim; x += 3) { int colorIndexA = x + y * xLim; int colorIndexB = (x - 3) + y * xLim; int colorIndexC = x + (y - 1) * xLim; int colorIndexD = (x - 3) + (y - 1) * xLim; int labelIndexA = x / 3 + y * width; int labelIndexB = (x / 3 - 1) + y * width; int labelIndexC = x / 3 + (y - 1) * width; int labelIndexD = (x / 3 - 1) + (y - 1) * width; int labeled = -1, oldLabel = -1; bool runUpdate = false; // check 1: top left (D) if (x != 0 && y != 0) { if (polarColors[colorIndexA] == polarColors[colorIndexD]) { idTable[labelIndexA] = idTable[labelIndexD]; labeled = idTable[labelIndexA]; } } // check 2: top (C) if (y != 0) { if (polarColors[colorIndexA] == polarColors[colorIndexC]) { idTable[labelIndexA] = idTable[labelIndexC]; if (labeled != -1) { if (labeled != idTable[labelIndexA]) { runUpdate = true; oldLabel = Math.Max(labeled, idTable[labelIndexA]); labeled = Math.Min(labeled, idTable[labelIndexA]); } } else { labeled = idTable[labelIndexA]; } } } // check 3: left (B) if (x != 0) { if (polarColors[colorIndexA] == polarColors[colorIndexB]) { idTable[labelIndexA] = idTable[labelIndexB]; if (labeled != -1) { if (labeled != idTable[labelIndexA]) { runUpdate = true; oldLabel = Math.Max(labeled, idTable[labelIndexA]); labeled = Math.Min(labeled, idTable[labelIndexA]); } } else { labeled = idTable[labelIndexA]; } } } // if no friends, give new label if (labeled == -1) { idTable[labelIndexA] = nObjects++; } if (runUpdate) { updateLabels(labeled, oldLabel, x, y); } } } int selectedID = idTable[click.X + click.Y * width]; for (int y = 0; y < height; ++y) { for (int x = 0; x < xLim; x += 3) { int colorIndex = x + y * xLim; int idIndex = x / 3 + y * width; switch (pred) { case LABEL_PRED.THRESHOLD: objsColors[colorIndex + 2] = (byte)((977 * idTable[idIndex]) + 173 % 256); objsColors[colorIndex + 1] = (byte)((644 * idTable[idIndex]) + 45 % 256); objsColors[colorIndex] = (byte)((311 * idTable[idIndex]) + 247 % 256); break; case LABEL_PRED.DEVIANCE: if (idTable[idIndex] == selectedID) { objsColors[colorIndex + 2] = (!dispBinary) ? (byte)0 : (byte)255; objsColors[colorIndex + 1] = 255; objsColors[colorIndex] = (!dispBinary) ? (byte)0 : (byte)255; } else if (!dispBinary) { objsColors[colorIndex + 2] = srcColors[colorIndex + 2]; objsColors[colorIndex + 1] = srcColors[colorIndex + 1]; objsColors[colorIndex] = srcColors[colorIndex]; } break; } } } Marshal.Copy(objsColors, 0, objsPtr, nBytes); polar.UnlockBits(polarData); objs.UnlockBits(objsData); pictureBox1.Image = objs; toolStripProgressBar1.Visible = false; switch (pred) { case LABEL_PRED.THRESHOLD: toolStripStatusLabel1.Text = "Objects: " + nObjects.ToString(); toolStripStatusLabel2.Text = "Threshold: " + threshold.ToString(); break; case LABEL_PRED.DEVIANCE: toolStripStatusLabel1.Text = "Object Selected: " + selectedID.ToString(); toolStripStatusLabel2.Text = "Deviance: " + deviation.ToString(); break; } }