} //******************************* end pictureBox4_MouseClick *************************************** private void pictureBox5_MouseClick(object sender, MouseEventArgs e) // Thresholding DivIm { if (!SHADING) { MessageBox.Show("Please click the button 'Shading correction'"); return; } Threshold = e.X; Graphics g = pictureBox5.CreateGraphics(); Pen bluePen = new Pen(Color.Blue); g.DrawLine(bluePen, Threshold, 0, Threshold, pictureBox5.Height); progressBar1.Visible = true; progressBar1.Value = 0; int nbyte = DivIm.N_Bits / 8; int jump = height / 100; for (int y = 0; y < height; y++) { if (y % jump == jump - 1) { progressBar1.PerformStep(); } for (int x = 0; x < width; x++) { int i = x + width * y; if (nbyte == 1) { if (DivIm.Grid[i] > Threshold) { BinIm.Grid[i] = 255; } else { BinIm.Grid[i] = 0; } } else { if (DivIm.MaxC(DivIm.Grid[2 + 3 * i], DivIm.Grid[1 + 3 * i], DivIm.Grid[0 + 3 * i]) > Threshold) { BinIm.Grid[i] = 255; } else { BinIm.Grid[i] = 0; } } Div_Bitmap.SetPixel(x, y, Color.FromArgb(BinIm.Grid[i], BinIm.Grid[i], BinIm.Grid[i])); } } pictureBox3.Image = Div_Bitmap; button4.Visible = true; label4.Text = "If threshold OK click 'Save division'"; label4.Visible = true; Threshold = -1; button4.Visible = true; progressBar1.Visible = false; } //*************************** end pictureBox5_MouseClick ************************************
public void CorrectShading() { int c, i, x, y; int[] color = { 0, 0, 0 }; int[] color1 = { 0, 0, 0 }; int Lightness = (int)numericUpDown2.Value; int hWind = (int)(numericUpDown1.Value * width / 2000); MeanIm.FastAverageM(GrayIm, hWind, this); // uses numericUpDown1 progressBar1.Visible = true; progressBar1.Value = 0; pictureBox5.Visible = true; int[] histoSub = new int[256]; int[] histoDiv = new int[256]; for (i = 0; i < 256; i++) { histoSub[i] = histoDiv[i] = 0; } byte lum = 0; byte lum1 = 0; int jump = height / 17; // width and height are properties of Form1 for (y = 0; y < height; y++) //================================================== { if (y % jump == jump - 1) { progressBar1.PerformStep(); } for (x = 0; x < width; x++) { // nbyteIm is member of 'Form1' for (c = 0; c < nbyteIm; c++) //============================================== { color[c] = Round(SigmaIm.Grid[c + nbyteIm * (x + width * y)] * Lightness / (double)MeanIm.Grid[x + width * y]); // Division if (color[c] < 0) { color[c] = 0; } if (color[c] > 255) { color[c] = 255; } DivIm.Grid[c + nbyteIm * (x + width * y)] = (byte)color[c]; color1[c] = SigmaIm.Grid[c + nbyteIm * (x + width * y)] + Lightness - MeanIm.Grid[x + width * y]; // Subtraction if (color1[c] < 0) { color1[c] = 0; } if (color1[c] > 255) { color1[c] = 255; } SubIm.Grid[c + nbyteIm * (x + width * y)] = (byte)color1[c]; } //======================= end for (c... ================================== if (nbyteIm == 1) { lum = (byte)color[0]; lum1 = (byte)color1[0]; } else { lum = SigmaIm.MaxC((byte)color[2], (byte)color[1], (byte)color[0]); lum1 = SigmaIm.MaxC((byte)color1[2], (byte)color1[1], (byte)color1[0]); } histoDiv[lum]++; histoSub[lum1]++; } } //============================ end for (y... =================================== // Calculating MinLight and MaxLight for 'Div': int MaxLightDiv, MaxLightSub, MinLightDiv, MinLightSub, Sum = 0; for (MinLightDiv = 0; MinLightDiv < 256; MinLightDiv++) { Sum += histoDiv[MinLightDiv]; if (Sum > width * height / 100) { break; } } Sum = 0; for (MaxLightDiv = 255; MaxLightDiv >= 0; MaxLightDiv--) { Sum += histoDiv[MaxLightDiv]; if (Sum > width * height / 100) { break; } } // Calculating MinLight and MaxLight for 'Sub': Sum = 0; for (MinLightSub = 0; MinLightSub < 256; MinLightSub++) { Sum += histoSub[MinLightSub]; if (Sum > width * height / 100) { break; } } Sum = 0; for (MaxLightSub = 255; MaxLightSub >= 0; MaxLightSub--) { Sum += histoSub[MaxLightSub]; if (Sum > width * height / 100) { break; } } // Calculating LUT for 'Div': byte[] LUT = new byte[256]; for (i = 0; i < 256; i++) { if (i <= MinLightDiv) { LUT[i] = 0; } else if (i > MinLightDiv && i <= MaxLightDiv) { LUT[i] = (byte)(255 * (i - MinLightDiv) / (MaxLightDiv - MinLightDiv)); } else { LUT[i] = 255; } } // Calculating LUTsub for 'Sub': byte[] LUTsub = new byte[256]; for (i = 0; i < 256; i++) { if (i <= MinLightSub) { LUTsub[i] = 0; } else if (i > MinLightSub && i <= MaxLightSub) { LUTsub[i] = (byte)(255 * (i - MinLightSub) / (MaxLightSub - MinLightSub)); } else { LUTsub[i] = 255; } } // Calculating contrasted "Div" and "Sub": for (i = 0; i < 256; i++) { histoDiv[i] = histoSub[i] = 0; } jump = width * height / 17; for (i = 0; i < width * height; i++) //==================================== { if (i % jump == jump - 1) { progressBar1.PerformStep(); } for (c = 0; c < nbyteIm; c++) { DivIm.Grid[c + nbyteIm * i] = LUT[DivIm.Grid[c + nbyteIm * i]]; SubIm.Grid[c + nbyteIm * i] = LUTsub[SubIm.Grid[c + nbyteIm * i]]; } if (nbyteIm == 1) { lum = DivIm.Grid[0 + nbyteIm * i]; lum1 = SubIm.Grid[0 + nbyteIm * i]; } else { lum = SigmaIm.MaxC(DivIm.Grid[2 + nbyteIm * i], DivIm.Grid[1 + nbyteIm * i], DivIm.Grid[0 + nbyteIm * i]); lum1 = SigmaIm.MaxC(SubIm.Grid[2 + nbyteIm * i], SubIm.Grid[1 + nbyteIm * i], SubIm.Grid[0 + nbyteIm * i]); } histoDiv[lum]++; histoSub[lum1]++; } //========================== end for (i = 0; ... ============================== // Displaying the histograms: Bitmap BmpPictBox4 = new Bitmap(pictureBox4.Width, pictureBox4.Height); Graphics g4 = Graphics.FromImage(BmpPictBox4); pictureBox4.Image = BmpPictBox4; Bitmap BmpPictBox5 = new Bitmap(pictureBox5.Width, pictureBox5.Height); Graphics g5 = Graphics.FromImage(BmpPictBox5); pictureBox5.Image = BmpPictBox5; int MaxHisto1 = 0, SecondMax1 = 0; int MaxHisto = 0, SecondMax = 0; for (i = 0; i < 256; i++) { if (histoSub[i] > MaxHisto1) { MaxHisto1 = histoSub[i]; } if (histoDiv[i] > MaxHisto) { MaxHisto = histoDiv[i]; } } for (i = 0; i < 256; i++) { if (histoSub[i] != MaxHisto1 && histoSub[i] > SecondMax1) { SecondMax1 = histoSub[i]; } } MaxHisto1 = SecondMax1 * 4 / 3; for (i = 0; i < 256; i++) { if (histoDiv[i] != MaxHisto && histoDiv[i] > SecondMax) { SecondMax = histoDiv[i]; } } MaxHisto = SecondMax * 4 / 3; Pen redPen = new Pen(Color.Red), yellowPen = new Pen(Color.Yellow), bluePen = new Pen(Color.Blue), greenPen = new Pen(Color.Green); SolidBrush whiteBrush = new SolidBrush(Color.White); Rectangle Rect1 = new Rectangle(0, 0, pictureBox4.Width, pictureBox4.Height); g4.FillRectangle(whiteBrush, Rect1); Rectangle Rect = new Rectangle(0, 0, pictureBox5.Width, pictureBox5.Height); g5.FillRectangle(whiteBrush, Rect); //Drawing the histograms: for (i = 0; i < 256; i++) { g4.DrawLine(redPen, i, pictureBox4.Height - histoSub[i] * 200 / MaxHisto1, i, pictureBox4.Height); g5.DrawLine(redPen, i, pictureBox5.Height - histoDiv[i] * 200 / MaxHisto, i, pictureBox5.Height); } // Vertical lines in histogram: for (i = 0; i < 256; i += 50) { g4.DrawLine(greenPen, i, pictureBox4.Height - 200, i, pictureBox4.Height); g5.DrawLine(greenPen, i, pictureBox5.Height - 200, i, pictureBox5.Height); } pictureBox4.Image = BmpPictBox4; pictureBox5.Image = BmpPictBox5; } //***************************** end CorrectShading **********************************************