public void Copy(CImage inp, bool full) // Copies the image "inp" into "this". If full==false then the array "Grid" // will be alloced but its content will not be copied. { int i, size; if (inp.N_Bits == 8) { size = inp.width * inp.height; } else { size = 3 * inp.width * inp.height; } N_Bits = inp.N_Bits; width = inp.width; height = inp.height; Grid = new byte[size]; Lab = new int[size]; if (full) { for (i = 0; i < size; i++) { Grid[i] = inp.Grid[i]; } } }
} //********************************* end DarkNoise ******************************* public int LightNoise(ref CImage Image, int MinGV, int MaxGV, int MALS, Form1 fm1) { bool COLOR = (Image.N_Bits == 24); int ind3 = 0, Label2, Lum, rv = 0; if (MALS == 0) { return(0); } int cnt = 0; int Len = Image.width * Image.height, jump, nStep = 25; if (Len > 2 * nStep) { jump = Len / nStep; } else { jump = 2; } fm1.progressBar1.Step = 1; for (int light = MinGV; light <= 255; light++) //========================================= { int index; for (int i = 0; i < nPixel[light]; i++) //======================================== { cnt++; if ((cnt % jump) == jump - 1) { fm1.progressBar1.PerformStep(); } ind3 = 3 * Index[light][i]; index = Index[light][i]; if (COLOR) { Label2 = Image.Grid[2 + 3 * Index[light][i]] & 1; Lum = (int)MaxC(Image.Grid[0 + ind3], Image.Grid[1 + ind3], Image.Grid[2 + ind3]); } else { Label2 = Image.Grid[Index[light][i]] & 2; Lum = Image.Grid[Index[light][i]]; } if (Lum == light && Label2 == 0) { rv = BreadthFirst_L(ref Image, i, light, MALS); } } //============================= end for (int i.. ======================= } //=============================== end for (int light.. ======================== return(rv); } //********************************* end LightNoise ******************************
} //****************************** end BitmapToImage **************************************** public void BitmapToImageOld(Bitmap bmp, CImage Image) { int nbyteIm = Image.N_Bits / 8; if (bmp.PixelFormat != PixelFormat.Format24bppRgb && bmp.PixelFormat != PixelFormat.Format8bppIndexed) { MessageBox.Show("BitmapToGridOld: Not suitable pixel format=" + bmp.PixelFormat); return; } Color color; for (int y = 0; y < bmp.Height; y++) { int y1 = nLoop * bmp.Height / denomProg; if (y % y1 == 0) { progressBar1.PerformStep(); } for (int x = 0; x < bmp.Width; x++) { int i = x + width * y; color = bmp.GetPixel(x, y); if (nbyteIm == 1) { Image.Grid[i] = MaxC(color.R, color.G, color.B); } else // nbyteIm == 3 { for (int c = 0; c < nbyteIm; c++) { if (c == 0) { Image.Grid[nbyteIm * i] = color.B; } if (c == 1) { Image.Grid[nbyteIm * i + 1] = color.G; } if (c == 2) { Image.Grid[nbyteIm * i + 2] = color.R; } } } } } } //****************************** end BitmapToImageOld ****************************************
} //****************************** end Open image **************************************** private void button4_Click(object sender, EventArgs e) // Segment { if (ORIG == false) { MessageBox.Show("Please click to 'Open image' and open an image"); return; } SegmentIm = new CImage(width, height, 8); for (int light = 0; light < 256; light++) { SegmentIm.Palette[light] = Color.FromArgb(light, light, light); } SegmentIm.nLoop = 2; SegmentIm.denomProg = denomProg; if (OrigIm.N_Bits == 24) { SegmentIm.ColorToGray(OrigIm, this); } else { SegmentIm.Copy(OrigIm); } byte gw = OrigIm.Grid[5]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { SegmentIm.Grid[x + width * y] /= 24; SegmentIm.Grid[x + width * y] *= 24; } } SegmentBmp = new Bitmap(width, height, PixelFormat.Format24bppRgb); ImageToBitmap(SegmentBmp, SegmentIm); label2.Text = " Segmented image "; label2.Visible = true; button2.Visible = true; label4.Visible = true; label5.Visible = true; numericUpDown1.Visible = true; numericUpDown2.Visible = true; label6.Text = "Click 'Impulse noise'"; pictureBox2.Image = SegmentBmp; progressBar1.Visible = false; SEGMENT = true; IMPULSE = false; BREAD = false; ROOT = false; } //****************************** end Segment ****************************************
public int ColorToGray(CImage inp, Form1 fm1) /* Transforms the colors of the color image "inp" in luminance=(r+g+b)/3 * and puts these values to this.Grid. --------- */ { if (N_Bits != 8) { MessageBox.Show("ColorToGray: The output image has a not suitable format; N_Bits=" + N_Bits); return(-1); } if (inp.N_Bits != 24) { MessageBox.Show("ColorToGray: The input image has a not suitable format; inp.N_Bits=" + inp.N_Bits); return(-1); } int x, y; if (inp.N_Bits != 24) { return(-1); } fm1.progressBar1.Value = 0; fm1.progressBar1.Visible = true; N_Bits = 8; width = inp.width; height = inp.height; Grid = new byte[width * height * 8]; int y1 = 1 + nLoop * height / denomProg; for (y = 0; y < height; y++) //================================================= { if ((y % y1) == 0) { fm1.progressBar1.PerformStep(); } for (x = 0; x < width; x++) // ============================================= { Grid[y * width + x] = (byte)MaxC(inp.Grid[2 + 3 * (x + width * y)], inp.Grid[1 + 3 * (x + width * y)], inp.Grid[0 + 3 * (x + width * y)]); } // ================= end for (x. ======================================== } // =================== end for (x. ========================================== return(1); } //********************** end ColorToGray *****************************************
} //******************************** end Sort ********************************************************* public int Neighb(CImage Image, int W, int n) // Returns the index of the nth neighboor of the pixel with the index W. If the neighboor // is outside the grid, then it returns -1. { int dx, dy, x, y, xn, yn; if (n == 4) { return(-1); } yn = y = W / Image.width; xn = x = W % Image.width; dx = (n % 3) - 1; dy = n / 3 - 1; xn += dx; yn += dy; if (xn < 0 || xn >= Image.width || yn < 0 || yn >= Image.height) { return(-2); } return(xn + Image.width * yn); }
public void Copy(CImage inp) { if (N_Bits != inp.N_Bits) { MessageBox.Show("Copy: The formats of input and output are not equal. input.N_Bits=" + inp.N_Bits + "; ouput.N_Bits=" + N_Bits); return; } width = inp.width; height = inp.height; N_Bits = inp.N_Bits; if (N_Bits == 8) { for (int i = 0; i < 256; i++) { Palette[i] = inp.Palette[i]; } } for (int i = 0; i < width * height * N_Bits / 8; i++) { Grid[i] = inp.Grid[i]; } }
} //********************************* end LightNoise ****************************** public int BreadthFirst_L(ref CImage Image, int i, int light, int size) // Looks for pixels with gray values >=light composing with the pixel "Index[light][i]" // an 8-connected subset. The size of the subset must be less than "size". // Instead of labeling the pixels of the subset, indices of pixels of the subset are saved in Comp. // Variable "i" is the index of the starting pixel in Index[light][i]; // Pixels which are put into queue and into Comp[] are labeled in "Image.Grid(green)" by setting Bit 0 to 1. // Pixels wich belong to a too big component and having the gray value equal to "light" are // labeled in "Image.Grid(red)" by setting Bit 0 to 1. If such a labeled pixel is found in the while loop // then "small" is set to 0. The insruction for breaking the loop is at the end of the loop. { int lightNeib, index, Label1, Label2, maxNeib = 8, Nei, nextIndex; bool small = true; int[] MaxBound = new int[3]; bool COLOR = (Image.N_Bits == 24); index = Index[light][i]; for (int c = 0; c < 3; c++) { MaxBound[c] = -255; } for (int p = 0; p < size; p++) { Comp[p] = -1; } int numbPix = 0; Comp[numbPix] = index; numbPix++; if (COLOR == true) { Image.Grid[1 + 3 * index] |= 1; // Labeling as in Comp } else { Image.Grid[index] |= 1; // Labeling as in Comp } Q1.input = Q1.output = 0; Q1.Put(index); // putting index into the queue while (!Q1.Empty()) //=== loop running while queue not empty ======================== { nextIndex = Q1.Get(); for (int n = 0; n <= maxNeib; n++) //======== all neighbors of nextIndex ================ { Nei = Neighb(Image, nextIndex, n); // the index of the nth neighbor of nextIndex if (Nei < 0) { continue; // Nei<0 means outside the image } if (COLOR) { Label1 = Image.Grid[1 + 3 * Nei] & 1; Label2 = Image.Grid[2 + 3 * Nei] & 1; lightNeib = MaxC(Image.Grid[2 + 3 * Nei], Image.Grid[1 + 3 * Nei], Image.Grid[0 + 3 * Nei]) & MaskColor; } else { Label1 = Image.Grid[Nei] & 1; Label2 = Image.Grid[Nei] & 2; lightNeib = Image.Grid[Nei] & MaskGV; } if (lightNeib == light && Label2 > 0) { small = false; } if (lightNeib >= light) //------------------------------------------------------------ { if (Label1 > 0) { continue; } Comp[numbPix] = Nei; // putting the element with index Nei into Comp numbPix++; if (COLOR) { Image.Grid[1 + 3 * Nei] |= 1; // Labeling with "1" as in Comp } else { Image.Grid[Nei] |= 1; // Labeling with "1" as in Comp } if (numbPix > size) { small = false; break; } Q1.Put(Nei); } else // lightNeib<light { if (Nei != index) //----------------------------------------------------- { if (COLOR) { if (lightNeib > MaxC((byte)MaxBound[2], (byte)MaxBound[1], (byte)MaxBound[0])) { for (int c = 0; c < 3; c++) { MaxBound[c] = (Image.Grid[c + 3 * Nei] & MaskColor); } } } else { if (lightNeib > MaxBound[0]) { MaxBound[0] = lightNeib; } } } //------------------ end if (Nei!=index) ---------------------------- } //-------------------- end if (lightNeib<=light) and else ------------------------ } // =================== end for (n=0; .. ==================================== if (!small) { break; } } // ===================== end while ============================================== int lightComp, nChanged = 0; for (int m = 0; m < numbPix; m++) //======================================================== { if (small && MaxBound[0] >= 0) //it was >-255; ----"-1" means MaxBound was not calculated --------- { if (COLOR == true) { for (int c = 0; c < 3; c++) { Image.Grid[c + 3 * Comp[m]] = (byte)MaxBound[c]; } } else { Image.Grid[Comp[m]] = (byte)MaxBound[0]; nChanged++; } } else { if (COLOR == true) { lightComp = MaxC(Image.Grid[2 + 3 * Comp[m]], Image.Grid[1 + 3 * Comp[m]], Image.Grid[0 + 3 * Comp[m]]) & MaskColor; } else { lightComp = Image.Grid[Comp[m]] & MaskGV; } if (lightComp == light) //------------------------------------------------------ { if (COLOR == true) { Image.Grid[2 + 3 * Comp[m]] |= 1; } else { Image.Grid[Comp[m]] |= 2; } } else { if (COLOR == true) { Image.Grid[1 + 3 * Comp[m]] &= (byte)MaskColor; // deleting label 1 Image.Grid[2 + 3 * Comp[m]] &= (byte)MaskColor; // deleting label 2 } else { Image.Grid[Comp[m]] &= (byte)MaskGV; // deleting the labels } } //----------------------- end if (lightComp==light) and else --------------- } //------------------------- end if (small && MaxBound[0]>0) and else ---------- } //=========================== end for (int m=0 .. ================================ return(nChanged); // numbPix; } //************************************ end BreadthFirst_L *****************************
public int Sort(CImage Image, int[] histo, Form1 fm1) { Index = new int[256][]; for (int gw = 0; gw < 256; gw++) { Index[gw] = new int[histo[gw] + 1]; } for (int gw = 0; gw < 256; gw++) { nPixel[gw] = 0; for (int j = 0; j < histo[gw] + 1; j++) { Index[gw][j] = 0; } } int ind; int light; bool COLOR; if (Image.N_Bits == 24) { COLOR = true; } else { COLOR = false; } int Len = Image.height, jump, nStep = 25; if (Len > 2 * nStep) { jump = Len / nStep; } else { jump = 2; } fm1.progressBar1.Value = 0; fm1.progressBar1.Step = 1; for (int y = 0; y < Image.height; y++) //============================================================ { if (y % jump == jump - 1) { fm1.progressBar1.PerformStep(); } for (int x = 0; x < Image.width; x++) //========================================================== { ind = x + y * Image.width; if (COLOR) { light = MaxC(Image.Grid[3 * ind + 2], Image.Grid[3 * ind + 1], Image.Grid[3 * ind + 0]) & 254; } else { light = Image.Grid[ind] & 252; } if (light < 0) { light = 0; } if (light > 255) { light = 255; } int nP = nPixel[light]; Index[light][nP] = ind; if (nPixel[light] < histo[light]) { nPixel[light]++; } } //============================ end for (int x = 0; .. ======================================== } //============================== end for (int y = 0; .. =========================================== return(1); } //******************************** end Sort *********************************************************
} //****************************** end ImageToBitmap **************************************** public void ImageToBitmapOld(Bitmap bmp, CImage Image) { int nbyteIm = Image.N_Bits / 8; int light = 0; switch (bmp.PixelFormat) { case PixelFormat.Format24bppRgb: nbyte = 3; break; case PixelFormat.Format8bppIndexed: nbyte = 1; break; default: MessageBox.Show("ImageToBitmap: Not suitable pixel format=" + bmp.PixelFormat); return; } Color color; int Len = bmp.Height, jump, nStep = 25; if (Len > 2 * nStep) { jump = Len / nStep; } else { jump = 2; } for (int y = 0; y < bmp.Height; y++) { if (y % jump == jump - 1) { progressBar1.PerformStep(); } for (int x = 0; x < bmp.Width; x++) { if (nbyte == 1) // nbyte of bmp; { if (nbyteIm == 1) // nbyteIm == 1; { light = (int)Image.Grid[x + bmp.Width * y]; bmp.SetPixel(x, y, Color.FromArgb(light, light, light)); } else // nbyte == 1; nbyteIm == 3 { bmp.SetPixel(x, y, Color.FromArgb(Image.Grid[nbyte * (x + width * y) + 2], Image.Grid[nbyte * (x + width * y) + 1], Image.Grid[nbyte * (x + width * y) + 0])); } } else // nbyte == 3 { if (nbyteIm == 1) { light = Image.Grid[x + bmp.Width * y]; color = Color.FromArgb(light, light, light); bmp.SetPixel(x, y, Color.FromArgb(light, light, light)); } else //nbyteIm ==3; nbyte == 3 { bmp.SetPixel(x, y, Color.FromArgb(Image.Grid[nbyteIm * (x + width * y) + 2], Image.Grid[nbyteIm * (x + width * y) + 1], Image.Grid[nbyteIm * (x + width * y) + 0])); } //------------------ end if (nbyteIm ==1) ------------------------ } //-------------------- end if (nbyte == 1) ---------------------------- } } } //****************************** end ImageToBitmapOld ****************************************/
} //****************************** end BitmapToImageOld **************************************** public void ImageToBitmap(Bitmap bmp, CImage Image) { int nbyteBmp = 0, nbyteIm = Image.N_Bits / 8; Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); switch (bmp.PixelFormat) { case PixelFormat.Format24bppRgb: nbyteBmp = 3; break; case PixelFormat.Format8bppIndexed: nbyteBmp = 1; break; default: MessageBox.Show("ImageToBitmap: Not suitable pixel format=" + bmp.PixelFormat); return; } IntPtr ptr = bmpData.Scan0; int size = bmp.Width * bmp.Height; int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] rgbValues = new byte[bytes]; byte light = 0; Color color; int index = 0; progressBar1.Visible = true; int Len = bmp.Height, jump, nStep = 50; if (Len > 2 * nStep) { jump = Len / nStep; } else { jump = 2; } for (int y = 0; y < bmp.Height; y++) { if (y % jump == jump - 1) { progressBar1.PerformStep(); } for (int x = 0; x < bmp.Width; x++) { if (nbyteBmp == 1) // nbyte of bmp; { if (nbyteIm == 1) // nbyteIm == 1; { color = (Color)Image.Palette[Image.Grid[x + bmp.Width * y]]; rgbValues[3 * (x + Math.Abs(bmpData.Stride) * y) + 0] = color.B; rgbValues[3 * (x + Math.Abs(bmpData.Stride) * y) + 1] = color.G; rgbValues[3 * (x + Math.Abs(bmpData.Stride) * y) + 2] = color.R; } else // nbyteBmp == 1; nbyteIm == 3 { color = bmp.Palette.Entries[Image.Grid[3 * (x + bmp.Width * y)]]; rgbValues[3 * (x + Math.Abs(bmpData.Stride) * y) + 0] = color.B; rgbValues[3 * (x + Math.Abs(bmpData.Stride) * y) + 1] = color.G; rgbValues[3 * (x + Math.Abs(bmpData.Stride) * y) + 2] = color.R; } } else // nbyteBmp == 3 { if (nbyteIm == 1) { light = Image.Grid[x + Image.width * y]; { index = 3 * x + Math.Abs(bmpData.Stride) * y; rgbValues[index + 0] = light; // color.B; rgbValues[index + 1] = light; // color.G; rgbValues[index + 2] = light; // color.R; } } else //nbyteIm ==3 { for (int c = 0; c < nbyte; c++) { rgbValues[c + nbyte * x + Math.Abs(bmpData.Stride) * y] = Image.Grid[c + nbyteIm * (x + bmp.Width * y)]; } } //------------------ end if (nbyteIm ==1) ------------------------ } //-------------------- end if (nbyte == 1) ---------------------------- } } System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); bmp.UnlockBits(bmpData); } //****************************** end ImageToBitmap ****************************************
public void BitmapToImage(Bitmap bmp, CImage Image) { int nbyteBmp, nbyteIm; nbyteIm = Image.N_Bits / 8; Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); switch (bmp.PixelFormat) { case PixelFormat.Format24bppRgb: nbyteBmp = 3; break; case PixelFormat.Format8bppIndexed: nbyteBmp = 1; break; default: MessageBox.Show("BitmapToGrid: Inappropriate pixel format=" + bmp.PixelFormat); return; } IntPtr ptr = bmpData.Scan0; int Str = bmpData.Stride; int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] rgbValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); progressBar1.Visible = true; int y1 = 1 + bmp.Height / 100; for (int y = 0; y < bmp.Height; y++) { if (y % y1 == 0) { progressBar1.PerformStep(); } for (int x = 0; x < bmp.Width; x++) { if (nbyteBmp == 1) { Color color = bmp.Palette.Entries[rgbValues[x + Math.Abs(bmpData.Stride) * y]]; if (nbyteIm == 1) { Image.Grid[x + bmp.Width * y] = color.R; } else { Image.Grid[3 * (x + bmp.Width * y) + 0] = color.B; Image.Grid[3 * (x + bmp.Width * y) + 1] = color.G; Image.Grid[3 * (x + bmp.Width * y) + 2] = color.R; } } else // nbyteBmp == 3 { if (nbyteIm == 1) { Image.Grid[x + bmp.Width * y] = rgbValues[2 + nbyte * x + Math.Abs(bmpData.Stride) * y]; } else // nbyteIm == 3 { for (int c = 0; c < nbyteBmp; c++) { Image.Grid[c + nbyteIm * (x + bmp.Width * y)] = rgbValues[c + nbyteBmp * x + Math.Abs(bmpData.Stride) * y]; } } } } } bmp.UnlockBits(bmpData); } //****************************** end BitmapToImage ****************************************
private void button1_Click(object sender, EventArgs e) // Open image { label1.Visible = false; label2.Visible = false; label3.Visible = false; label4.Visible = false; label5.Visible = false; label6.Visible = false; button2.Visible = false; button3.Visible = false; button4.Visible = false; button5.Visible = false; button6.Visible = false; button8.Visible = false; numericUpDown1.Visible = false; numericUpDown2.Visible = false; OpenFileDialog openFileDialog1 = new OpenFileDialog(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { OrigBmp = new Bitmap(openFileDialog1.FileName); OpenImageFile = openFileDialog1.FileName; } catch (Exception ex) { MessageBox.Show("Open image; Could not read file from disk. Error: " + ex.Message); } } else { return; } label1.Visible = true; label3.Text = "Opened image:" + openFileDialog1.FileName; label3.Visible = true; button4.Visible = true; label6.Visible = true; label6.Text = "Click 'Segment'"; width = OrigBmp.Width; height = OrigBmp.Height; ORIG = true; progressBar1.Maximum = 100; progressBar1.Value = 0; progressBar1.Step = 1; nLoop = 1; denomProg = progressBar1.Maximum / progressBar1.Step; if (OrigBmp.PixelFormat == PixelFormat.Format8bppIndexed) { OrigIm = new CImage(width, height, 8); BitmapToImageOld(OrigBmp, OrigIm); } else if (OrigBmp.PixelFormat == PixelFormat.Format24bppRgb) { OrigIm = new CImage(width, height, 24); BitmapToImage(OrigBmp, OrigIm); } else { MessageBox.Show("Not suitable pixel format=" + OrigBmp.PixelFormat); return; } pictureBox1.Image = OrigBmp; ImpulseIm = new CImage(width, height, 8); //, Grid); SegmentIm = new CImage(width, height, 8); BreadthFirIm = new CImageComp(width, height, 8); RootIm = new CImageComp(width, height, 8); progressBar1.Value = 0; label1.Text = " Original image "; } //****************************** end Open image ****************************************