// convert RGB TO HSI private HSI RGB_2_HSI(int width, int height, byte[] RData, byte[] GData, byte[] BData) { // declare HSI hsiImage = new HSI(); int pixels = width * height; hsiImage.HData = new double[pixels]; hsiImage.SData = new double[pixels]; hsiImage.IData = new double[pixels]; // calculate HSI for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { int pos = i * width + j; double R = RData[pos]; double G = GData[pos]; double B = BData[pos]; double theta = Math.Acos(0.5 * (R + R - G - B) / Math.Sqrt((R - G) * (R - G) + (R - B) * (G - B))); theta *= 180 / Math.PI; if (GData[pos] < BData[pos]) { theta = 360 - theta; } hsiImage.HData[pos] = theta; hsiImage.SData[pos] = 1.0 - 3.0 * Math.Min(R, Math.Min(G, B)) / (R + G + B); hsiImage.IData[pos] = (R + G + B) / 3.0; } } return(hsiImage); }
// show HSI image private void button9_Click(object sender, EventArgs e) { if (pictureBox1.Image == null) { return; } HSI hsiImage = RGB_2_HSI(img.Width, img.Height, RData, GData, BData); RGB rgbImage = HSI_2_RGB(img.Width, img.Height, hsiImage); }
// convert HSI TO RGB private RGB HSI_2_RGB(int width, int height, HSI hsiImage) { // declare RGB rgbImage = new RGB(); int pixels = width * height; rgbImage.RData = new byte[pixels]; rgbImage.GData = new byte[pixels]; rgbImage.BData = new byte[pixels]; // calculate RGB for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { int pos = i * width + j; int H = (int)hsiImage.HData[pos]; double S = hsiImage.SData[pos]; double I = hsiImage.IData[pos]; if (H >= 0 && H < 120) { rgbImage.BData[pos] = (byte)(I * (1 - S)); rgbImage.RData[pos] = (byte)(I * (1 + S * Math.Cos(H * Math.PI / 180.0) / Math.Cos((60 - H) * Math.PI / 180.0))); rgbImage.GData[pos] = (byte)(3 * I - rgbImage.BData[pos] - rgbImage.RData[pos]); } else if (H >= 120 && H < 240) { H -= 120; rgbImage.RData[pos] = (byte)(I * (1 - S)); rgbImage.GData[pos] = (byte)(I * (1 + S * Math.Cos(H * Math.PI / 180.0) / Math.Cos((60 - H) * Math.PI / 180.0))); rgbImage.BData[pos] = (byte)(3 * I - rgbImage.GData[pos] - rgbImage.RData[pos]); } else { H -= 240; rgbImage.GData[pos] = (byte)(I * (1 - S)); rgbImage.BData[pos] = (byte)(I * (1 + S * Math.Cos(H * Math.PI / 180) / Math.Cos((60 - H) * Math.PI / 180))); rgbImage.RData[pos] = (byte)(3 * I - rgbImage.GData[pos] - rgbImage.BData[pos]); } } } BuildBitmap(img.Width, img.Height, rgbImage.RData, rgbImage.GData, rgbImage.BData); return(rgbImage); }
// histogram equalization on I channel private void button10_Click(object sender, EventArgs e) { if (pictureBox1.Image == null) { return; } // calculate hsi image; HSI hsiImage = RGB_2_HSI(img.Width, img.Height, RData, GData, BData); // convert i channel to byte byte[] IData = new byte[img.Width * img.Height]; for (int i = 0; i < img.Width * img.Height; ++i) { IData[i] = (byte)hsiImage.IData[i]; } // I channel histogram int[] IHisto = Cal_Hist(IData); int[] histoChange = Equalize_Hist(img.Width, img.Height, IHisto); HSI_Histo_Equal(img.Width, img.Height, histoChange, IData, hsiImage); }
// show HSI histogram on I channel private void button12_Click(object sender, EventArgs e) { if (pictureBox1.Image == null) { return; } // calculate hsi image; HSI hsiImage = RGB_2_HSI(img.Width, img.Height, RData, GData, BData); // convert i channel to byte byte[] IData = new byte[img.Width * img.Height]; for (int i = 0; i < img.Width * img.Height; ++i) { IData[i] = (byte)hsiImage.IData[i]; } // I channel histogram int[] IHisto = Cal_Hist(IData); Form2 Iform2 = new Form2(IHisto, "I"); Iform2.Show(); }
// HSI histogram equalization post-operation private void HSI_Histo_Equal(int width, int height, int[] histoChange, byte[] IData, HSI hsiImage) { // pixels of the image int pixels = width * height; // change the original image; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { hsiImage.IData[i * width + j] = histoChange[IData[i * width + j]]; } } HSI_2_RGB(width, height, hsiImage); }