// Constructor public ColorImageStatisticsDescription(Bitmap image) { // get image dimension int width = image.Width; int height = image.Height; // lock it BitmapData imgData = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); // gather statistics statRGB = new ImageStatistics(imgData); statHSL = new ImageStatisticsHSL(imgData); statYCbCr = new ImageStatisticsYCbCr(imgData); // unlock image image.UnlockBits(imgData); }
private void button2_Click(object sender, EventArgs e) { if (pictureBox1.Image == null) { MessageBox.Show("请先拖拽图片文件到窗口中!"); return; } char[] chars = new char[] { 'M', 'Q', 'H', 'N', 'O', 'S', '2', 'C', '?', ')', '>', '!', ':', ',', '.' }; StringBuilder sb = new StringBuilder(); string html_head = "<!DOCTYPE html><html><body style = \"font-family: Monospace;font-size: 2px;line-height: 50%;\">"; string html_tail = "</body></html>"; sb.Append(html_head); Bitmap bmp = (Bitmap)pictureBox1.Image; int p_fac = Convert.ToInt32(textBox1.Text); //最大像素个数 double w_fac = Convert.ToDouble(textBox3.Text); //宽度因子 double c_fac = Convert.ToDouble(textBox2.Text); //对比度因子 bool binarization = false; int binary_threshold = 127; int m = Math.Max(bmp.Height, (int)(bmp.Width * w_fac)); double delta = Math.Max(m * 1.0 / p_fac, 1); int w = (int)((int)(bmp.Width * w_fac) / delta); int h = (int)(bmp.Height / delta); FiltersSequence filter = new FiltersSequence(); filter.Add(new ResizeNearestNeighbor(w, h)); if (checkBox1.Checked) { filter.Add(new HistogramEqualization()); } if (checkBox2.Checked) { filter.Add(new Sharpen()); } if (checkBox3.Checked) { binarization = true; } using (Bitmap newbmp = filter.Apply(bmp)) { if (binarization) { ImageStatisticsYCbCr stat = new ImageStatisticsYCbCr(newbmp); ContinuousHistogram y = stat.Y; binary_threshold = (int)(y.Mean * 162); } BitmapData bmpData = newbmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); unsafe { byte *ptr = (byte *)(bmpData.Scan0); int tmp; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { tmp = ptr[1]; tmp = (int)((tmp - 127) * c_fac + 127); tmp = tmp < 0 ? 0 : (tmp > 255 ? 255 : tmp); int l = (int)(0.299 * ptr[2] + 0.587 * tmp + 0.114 * ptr[0]); if (binarization) { l = l > binary_threshold ? 255 : 0; } sb.Append(chars[l * 14 / 255]); ptr += 3; } sb.Append("<br/>"); ptr += bmpData.Stride - bmpData.Width * 3; } } newbmp.UnlockBits(bmpData); } sb.Append(html_tail); string tempFile = Path.GetTempFileName() + ".html"; File.WriteAllText(tempFile, sb.ToString().Replace("...", ".. ")); System.Diagnostics.Process.Start(tempFile); }
public ImageStat GetImageStats(string fullPath, FormOptions options) { if (cancelAnalysis) { return(null); } Messaging.Talk($"{fullPath}"); ImageStat res = null; try { res = new ImageStat(); res.ImagePath = fullPath.Replace(options.ImagesSoureDirectory, string.Empty); res.Length = _fileSystemService.GetFileLength(fullPath); // load file using (var image = _imageService.GetThumb(fullPath, _configService.TempBitmapHeight)) { // hsl //Talk($"{fullPath} HSL... "); var statsHSL = new ImageStatisticsHSL(image); res.Luminance = (decimal)statsHSL.Luminance.Median; res.Pixels = statsHSL.PixelsCount; // rgb //Talk($"{fullPath} RGB... "); var stats = new ImageStatistics(image); res.Blue = stats.Blue.Median; res.Red = stats.Red.Median; res.Green = stats.Green.Median; res.Pixels = stats.PixelsCount; res.BlueMax = stats.Blue.Max; res.RedMax = stats.Red.Max; res.GreenMax = stats.Green.Max; res.BlueMin = stats.Blue.Min; res.RedMin = stats.Red.Min; res.GreenMin = stats.Green.Min; // YCbCr // Talk($"{fullPath} YCbCr... "); var statsYCbCr = new ImageStatisticsYCbCr(image); res.Cb = statsYCbCr.Cb.Mean; res.Cr = statsYCbCr.Cr.Mean; res.Y = statsYCbCr.Y.Mean; } } catch (Exception e) { res = null; Messaging.Talk($"{fullPath} Error! {e.Message}... "); Messaging.RaiseProgress(null, new ProgressBarEventArgs { EventKindOf = EventKind.IncrementError }); } finally { Messaging.RaiseProgress(null, new ProgressBarEventArgs { EventKindOf = EventKind.Increment }); } return(res); }