|
|
Регистрация Восстановить пароль |
Повторная активизация e-mail |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
|
Опции темы | Поиск в этой теме |
21.01.2011, 14:06 | #1 |
Регистрация: 15.01.2010
Сообщений: 4
|
Морфологические алгоритмы на полутоновых изображениях: Раскрывание (отмыкание) на C#
Написал программу "эрозия полутонового изображения", сейчас необходимо вместо эрозии реализовать раскрывание (отмыкание) полутонового изображения.
Если говорить проще к эрозии нужно применить дилатацию. Ничего не получается. Вот исходный код эрозии: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; namespace Erosion { public partial class formMain : Form { int size; Bitmap sourceImage; Bitmap grayscaleImage; Bitmap erodedImage; string fileName=""; bool fileOpened = false; bool imageTransformed=false; bool imageEroded = false; bool[,] structuringElement; CheckBox[,] checks; public formMain() { InitializeComponent(); btnSetRank_Click(btnSetRank,new EventArgs()); prbErode.Visible = false; } private void btnOpen_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { fileName = openFileDialog1.FileName; sourceImage = new Bitmap(fileName); fileOpened = true; this.Refresh(); grayscaleImage = ToGrayscale(sourceImage); imageTransformed = true; imageEroded = false; this.Refresh(); } } private Bitmap ToGrayscale(Bitmap image) { Color pixel; Color newpixel; byte R, G, B, Gray; int sum; float br; Bitmap result = new Bitmap(image.Width, image.Height); prbErode.Value = 0; prbErode.Maximum = image.Width-1; prbErode.Visible = true; for (int i = 0; i < image.Width; i++) { for (int j = 0; j < image.Height; j++) { pixel = image.GetPixel(i, j); R = (byte)((float)pixel.R * 0.3); G = (byte)((float)pixel.G * 0.59); B = (byte)((float)pixel.B * 0.11); sum = R + G + B; Gray =(byte)( sum <= 255 ? (sum >= 0 ? sum : 0) : 255); br = pixel.GetBrightness(); newpixel = Color.FromArgb(Gray, Gray, Gray); result.SetPixel(i, j, newpixel); } prbErode.Value = i; } prbErode.Value = prbErode.Maximum; prbErode.Visible = false; return result; } private Bitmap Erode(Bitmap image, bool[,]strel) { int x, y, i, j, k, m; Color pixel; Color newpixel; Bitmap result = new Bitmap(image.Width, image.Height); int arrayHalfWidth,arrayHalfHeight; arrayHalfWidth=(int)Math.Floor((dou ble)strel.GetUpperBound(0)/2); arrayHalfHeight=(int)Math.Floor((do uble)strel.GetUpperBound(1)/2); prbErode.Value = 0; prbErode.Maximum = image.Width; prbErode.Visible = true; byte[,] nHood = new byte[strel.GetUpperBound(0)+1, strel.GetUpperBound(1)+1]; for (i = arrayHalfWidth; i < image.Width-arrayHalfWidth; i++) { for (j = arrayHalfHeight; j < image.Height-arrayHalfHeight; j++) { for (k = i - arrayHalfWidth,x =0; k <= i + arrayHalfWidth; k++,x++) { for (m = j - arrayHalfHeight,y=0; m <= j + arrayHalfHeight; m++,y++) { pixel = image.GetPixel(k, m); if (strel[x, y]) { nHood[x, y] = (byte)((pixel.R + pixel.G + pixel.B) / 3); } else { nHood[x, y] = 255; } } } int min = 255; foreach (byte el in nHood) if (el < min) min = el; newpixel = Color.FromArgb(min, min, min); result.SetPixel(i, j, newpixel); } prbErode.Value = i; } prbErode.Value = prbErode.Maximum; prbErode.Visible = false; return result; } |
21.01.2011, 14:07 | #2 |
Регистрация: 15.01.2010
Сообщений: 4
|
Продолжение кода
private void btnTransform_Click(object sender, EventArgs e)
{ if (fileOpened && imageTransformed) { structuringElement = GetStructuringElement(); erodedImage = Erode(grayscaleImage,structuringEle ment); imageEroded = true; this.Refresh(); } } private void formMain_Resize(object sender, EventArgs e) { gbxSourceImage.Width = this.Width / 2 - 130; gbxTransformed.Width = gbxSourceImage.Width; } private void gbx_Paint(object sender, PaintEventArgs e) { if (fileOpened) { Rectangle rect = new Rectangle(); Graphics g = e.Graphics; double hResizeFactor = ((double)sourceImage.Width) / (((GroupBox)sender).Width - 30); double vResizeFactor = ((double)sourceImage.Height) / (((GroupBox)sender).Width - 30); double resizeFactor = hResizeFactor > vResizeFactor ? hResizeFactor : vResizeFactor; rect.Width = Convert.ToInt32(sourceImage.Width / resizeFactor); rect.Height = Convert.ToInt32(sourceImage.Height / resizeFactor); rect.Y = 20; rect.X = 20; if(imageTransformed && sender.Equals(gbxSourceImage)) g.DrawImage(grayscaleImage, rect); if (imageEroded && sender.Equals(gbxTransformed)) g.DrawImage(erodedImage, rect); } } private void btnSetRank_Click(object sender, EventArgs e) { size = (int)nudRank.Value; if (checks != null) { foreach (CheckBox ch in checks) { gbxStrEl.Controls.Remove(ch); } } checks = new CheckBox[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { checks[i, j] = new CheckBox(); checks[i, j].Location = new Point(20 * i + 10, 20 * j + 20); checks[i, j].Size = new Size(20, 20); checks[i, j].Checked = true; gbxStrEl.Controls.Add(checks[i, j]); } } } private bool[,] GetStructuringElement() { bool[,] result = new bool[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { result[i, j] = checks[i, j].Checked; } } return result; } } } |
Опции темы | Поиск в этой теме |
Похожие темы | ||||
Тема | Автор | Раздел | Ответов | Последнее сообщение |
Задача из раздела Комбинаторные алгоритмы и алгоритмы на гра-фах в Паскале | Klik_1602 | Помощь студентам | 1 | 04.01.2011 01:18 |
Алгоритмы | CodeNOT | Общие вопросы C/C++ | 3 | 11.12.2010 19:20 |
алгоритмы | boomeer | Общие вопросы C/C++ | 6 | 04.12.2010 20:05 |
Методы диффузии в изображениях | risya | Мультимедиа в Delphi | 1 | 07.06.2009 11:30 |
Алгоритмы | Levhik | Общие вопросы Delphi | 7 | 14.01.2009 17:38 |