Падение производительности метода обработки изображения - C#

Узнай цену своей работы

Формулировка задачи:

Есть 2 метода, осуществляющие фильтрацию, они почти идентичны, однако один отрабатывает 7 секунд, второй 3 минуты. Не могу найти причину таких тормозов 2го метода. 1й, быстрый:
            double c = 0, m = 1, c1 = 0, c2 = 0, c3 = 0;
            int count = 0;                              
            int inc = Convert.ToInt16(comboBox1.SelectedItem);
            Bitmap bmp = new Bitmap(pictureBox1.Image);      
            Color color;                                                         
 
            for (int y = 0; y < img_h; y++) {                           
                for (int x = 0; x < img_w; x++) {                      
                    for (int j = -(inc - 1) / 2; j <= (inc - 1) / 2; j++) {   
                        for (int i = -(inc - 1) / 2; i <= (inc - 1) / 2; i++) {  
                            if (x + i >= 0 && x + i < img_w && y + j >= 0 && y + j < img_h) {    
                                color = bmp.GetPixel(x + i, y + j);                                              
                                        c1 = Convert.ToDouble(color.R);                                          
                                        c2 = Convert.ToDouble(color.G);                                          
                                        c3 = Convert.ToDouble(color.B);                                          
                                        HSV(color, out c1, out c2, out c3);                                       
                                        if (c3 * 100 > 10) {                                        
                                            m = m * c3 * 100;                                     
                                            count++; }                                               
                            } 
                        } 
                    }
                    c3 = Math.Pow(m, 1d / count) / 100;                                       
                    RGB(c1, c2, c3);                                                                   
                    bmp.SetPixel(x, y, Color.FromArgb(x1, x2, x3));                        
                    c = 0;                                                                                 
                    m = 1;                                                                                
                    count = 0;                                                                           
                }
                progressBar1.Value = y;                                                            
                progressBar1.Refresh();                                                            
                Invalidate();                                                                            
                Application.DoEvents();                                                             
            }
            pictureBox1.Image = bmp;                                                            
            progressBar1.Value = 0;
2й, медленный:
            double c1 = 0, c2 = 0, c3 = 0;             
            var list = new List<double>();
 
            int cx = (int)comboBox1.SelectedItem;
            int cy = (int)comboBox2.SelectedItem;
 
            int l = cx - 1;
            int r = mx - cx;
            int u = cy - 1;
            int d = my - cy;
 
            Bitmap bmp = new Bitmap(pictureBox1.Image);            
            Color color;                                                             
 
            for (int y = 0; y < h; y++)  {                                                                      
                for (int x = 0; x < w; x++) {                                                                
                    for (int j = -u; j <= d; j++) {               
                        for (int i = -l; i <= r; i++)  {                                     
                            if (x + i >= 0 && x + i < w && y + j >= 0 && y + j < h) {       
                                color = bmp.GetPixel(x + i, y + j);                           
                                                                                                   
                                c1 = Convert.ToDouble(color.R);                               
                                c2 = Convert.ToDouble(color.G);                               
                                c3 = Convert.ToDouble(color.B);                               
                                RGBtoHSV(color, out c1, out c2, out c3);                    
 
                                for (int t = 1; t <= Convert.ToInt16(textBox1.Lines[cy - 1 + j][cx - 1 + i].ToString()); t++) {
                                    list.Add(c3); }
                            }
                        }
                    }
 
                    list.Sort();
 
                    if (list.Count() % 2 == 0)
                    {
                        c3 = (list[(list.Count() / 2) - 1] + list[(list.Count() / 2)]) / 2;
                    }
                    else
                    {
                        c3 = list[(list.Count() - 1) / 2];
                    }
                            
                    HSVtoRGB(c1, c2, c3);                                                   
                    bmp.SetPixel(x, y, Color.FromArgb(x1, x2, x3));                
                    list.Clear();
                }
                button2.Text = Math.Round((double)y / (double)h * 100, 0).ToString() + " %";
                Invalidate();                                             
                Application.DoEvents();                              
            }
            pictureBox1.Image = bmp;                             
            button2.Text = "Фильтр";
Оба цикла проходят по всем пикселям картинки 1024х768, накладывая маску 3х3 на них, преобразуя каждый(1024х768х9) пиксель из цветового пространства RGB в HSV, находя среднее значение яркости, и преобразовывая найденную яркость обратно в RGB. Огромных отличий в методах на первый взгляд не видно, однако разница в производительности существенна. Что может тормозить процесс?

Решение задачи: «Падение производительности метода обработки изображения»

textual
Листинг программы
for (int t = 1; t <= Convert.ToInt16(textBox1.Lines[cy - 1 + j][cx - 1 + i].ToString()); t++)

Оцени полезность:

14   голосов , оценка 4.071 из 5
Похожие ответы