Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private byte[,] rangPixels;
- private byte[,] domainPixels;
- Bitmap decodeImg;
- private List<_Domain_Struct> domains = new List<_Domain_Struct>();
- private List<_code_record> list_rangs;
- ImageProcessing proc = new ImageProcessing();
- public parametrsForm._coding_params @params = new parametrsForm._coding_params();
- AdditionalMethods add = new AdditionalMethods();
- string img_encode_filename;
- public fractFuzzyImgProc()
- {
- InitializeComponent();
- }
- private void CalcDomainBlocks()
- {
- //3
- domains.Clear();
- int cur_domain_index = 0;
- for (int cur_level = 1; cur_level <= @params.domain_levels; cur_level++)
- {
- double buf1 = (@params.domain_cols * Math.Pow(2, (cur_level - 1)));
- double buf2 = (@params.domain_rows * Math.Pow(2, (cur_level - 1)));
- int domain_w = (int)((domainPixels.GetUpperBound(0) + 1) / buf1);
- int domain_h = (int)((domainPixels.GetUpperBound(1) + 1) / buf2);
- int domains_per_hor_line = (int)(@params.domain_cols * Math.Pow(2, (cur_level - 1)));
- if (@params.overlap_horz == 50)
- {
- domains_per_hor_line += @params.domain_cols * cur_level - 1;
- }
- int domains_per_ver_line = (int)(@params.domain_rows * Math.Pow(2, (cur_level - 1)));
- if (@params.overlap_horz == 50)
- {
- domains_per_ver_line += @params.domain_rows * cur_level - 1;
- }
- for (int cur_row = 0; cur_row < domains_per_hor_line; cur_row++)
- {
- for (int cur_col = 0; cur_col < domains_per_ver_line; cur_col++)
- {
- float k = 1F;
- if (@params.overlap_horz == 50)
- {
- k = 0.5F;
- }
- _Domain_Struct cur_domain = new _Domain_Struct();
- cur_domain.index = cur_domain_index;
- cur_domain.level = cur_level;
- int left = 0;
- if (cur_row > 0)
- {
- left = (int)(cur_row * domain_w * k);
- }
- int rigth = 0;
- if (cur_col > 0)
- {
- rigth = (int)(cur_col * domain_h * k);
- }
- cur_domain.rect = new TRect(left, rigth, left + domain_w, rigth + domain_h);
- cur_domain.pixels = new byte[domain_w, domain_h];
- for (int i = cur_domain.rect.left; i < cur_domain.rect.right; i++)
- {
- for (int j = cur_domain.rect.top; j < cur_domain.rect.bottom; j++)
- {
- cur_domain.pixels[i - cur_domain.rect.left, j - cur_domain.rect.top] = domainPixels[i, j];
- }
- }
- cur_domain.wigth = domain_w;
- cur_domain.height = domain_h;
- cur_domain.s = 0F;
- cur_domain.o = 0F;
- domains.Add(cur_domain);
- cur_domain_index++;
- }
- }
- }
- }
- private void cutTargetDomain(ref _Domain_Struct domain, ref _Domain_Struct new_domain)
- { //11
- int aver_cols = (int)(domain.height * (1.0f / new_domain.wigth) + 0.5f);
- int aver_rows = (int)(domain.height * (1.0f / new_domain.wigth) + 0.5f);
- int sum;
- int n_aver = aver_cols * aver_rows;
- int row = 0;
- int col = 0;
- for (int i = 0; i < new_domain.height; i++)
- {
- col = 0;
- for (int j = 0; j < new_domain.wigth; j++)
- {
- sum = 0;
- for (int k = row; k < row + aver_rows-1; k++)
- {
- for (int m = col; m < col + aver_cols-1; m++)
- {
- sum += domain.pixels[k, m];
- }
- }
- if (n_aver == 0)
- {
- MessageBox.Show("Деление на 0 !!!");
- }
- new_domain.pixels[i, j] = Convert.ToByte(sum / n_aver);
- col += aver_cols;
- if (col + aver_cols - 1 > domain.wigth)
- {
- col = domain.wigth - aver_cols + 1;
- }
- } // end j
- row += aver_rows;
- if (row + aver_rows - 1 > domain.height)
- {
- row = domain.height - aver_rows + 1;
- }
- } // end i
- return;
- }
- private void decodeBtn_Click(object sender, EventArgs e)
- {
- proB.Value = 0;
- @params = parametrs.Value;
- string path = Directory.GetCurrentDirectory() + "//temp.bmp";
- if (decClick == false)
- {
- using (decodeImg)
- {
- decodeImg.Save(path);
- decClick = true;
- }
- }
- informDgv.Rows.Clear();
- codingBtn.Enabled = false;
- changeParamsBtn.Enabled = false;
- decodeBtn.Enabled = false;
- openRangBtn.Enabled = false;
- saveRangBtn.Enabled = false;
- inputImgBox1.Enabled = false;
- Bitmap temp = decodeImg;
- t1 = new Thread(() => StartDecoding(path, 1));
- //t1 = new Thread(() => StartDecoding2(img_encode_filename,img_encode_filename, temp, 1));
- t1.Start();
- }
- public void StartDecoding2(string str_img_decode, string str_img_domain, Bitmap vis_img, int iter_count)
- {
- try
- {
- string work_str_img_decode = str_img_decode;
- string work_str_img_domain = str_img_domain;
- for (int cur_iter = 0; cur_iter < iter_count; cur_iter++)
- {
- Bitmap image_encode;
- //this.@params = @params;
- using (var fs = File.OpenRead(work_str_img_decode))
- {
- vis_img = new Bitmap(fs);
- }
- using (var fs = File.OpenRead(work_str_img_decode))
- {
- image_encode = new Bitmap(fs);
- }
- rangPixels = new byte[image_encode.Width, image_encode.Height];
- rangPixels = proc.BitmapToByteRgb(image_encode);
- Bitmap image_domain;
- using (var fs = File.OpenRead(work_str_img_domain))
- {
- image_domain = new Bitmap(fs);
- }
- domainPixels = new byte[image_domain.Width, image_domain.Height];
- domainPixels = proc.BitmapToByteRgb(image_domain);
- CalcDomainBlocks();
- for (int cur_rang_index = 0; cur_rang_index < GetRangsCount(); cur_rang_index++)
- {
- _code_record curRang = GetRange(cur_rang_index);
- TRect rect = add.GetRangeBlockRect(curRang.range_index, @params.quadtree_depth, ref rangPixels);
- int domain_index = Convert.ToInt32(curRang.domain_index);
- _Domain_Struct cur_domain = new _Domain_Struct();
- cur_domain.rect = domains[domain_index].rect;
- cur_domain.wigth = domains[domain_index].wigth;
- cur_domain.height = domains[domain_index].height;
- cur_domain.index = domains[domain_index].index;
- cur_domain.s = Convert.ToSingle(curRang.s);
- cur_domain.o = Convert.ToSingle(curRang.o);
- ArrayResize(ref cur_domain.pixels, cur_domain.wigth, cur_domain.height);
- for (int i = 0; i < cur_domain.wigth; i++)
- {
- for (int j = 0; j < cur_domain.height; j++)
- {
- cur_domain.pixels[i, j] = domains[domain_index].pixels[i, j];
- }
- }
- _Domain_Struct rotate_domain = new _Domain_Struct();
- rotate_domain.rect = domains[domain_index].rect;
- rotate_domain.wigth = domains[domain_index].wigth;
- rotate_domain.height = domains[domain_index].height;
- rotate_domain.index = domains[domain_index].index;
- rotate_domain.s = Convert.ToSingle(curRang.s);
- rotate_domain.o = Convert.ToSingle(curRang.o);
- ArrayResize(ref rotate_domain.pixels, cur_domain.wigth, cur_domain.height);
- int trans_index = Convert.ToInt32(curRang.trans_index);
- if (trans_index != 0)
- {
- add.RotateDomainRect(ref cur_domain, ref rotate_domain, trans_index);
- }
- else
- {
- add.RotateDomainRect(ref cur_domain, ref rotate_domain, 0);
- }
- _Domain_Struct new_domain = new _Domain_Struct();
- new_domain.rect = rect;
- new_domain.wigth = rect.Width();
- new_domain.height = rect.Height();
- new_domain.index = cur_domain.index;
- new_domain.s = cur_domain.s;
- new_domain.o = cur_domain.o;
- ArrayResize(ref new_domain.pixels, rect.Width(), rect.Height());
- cutTargetDomain(ref rotate_domain, ref new_domain);
- add.ApplySO(ref new_domain);
- PutDomainIntoRang(new_domain, rect);
- }
- work_str_img_decode = GetTempFilename();
- work_str_img_domain = GetTempFilename();
- SaveEncodeImageMatrix(GetTempFilename());
- if (vis_img != null)
- {
- CopyEncodeMatrixToImage(ref vis_img);
- }
- inputImgBox1.Image = vis_img;
- }
- }
- catch (NullReferenceException)
- {
- Console.WriteLine("darova");
- }
- endingThreads();
- }
- public _code_record GetRange(int index)
- {
- return list_rangs[index];
- }
- string work_str_img_decode;
- public void StartDecoding(string str_img_decode, int iter_count)
- {
- if (work_str_img_decode== null)
- work_str_img_decode = str_img_decode;
- vis_img = new Bitmap(512, 512);
- Bitmap vis = inputImgBox1.Image as Bitmap; ; //new Bitmap(512,512);
- using (Graphics g = Graphics.FromImage(vis_img))
- {
- g.DrawImage(vis, 0, 0);
- }
- for (int cur_iter = 0; cur_iter < iter_count; cur_iter++)
- {
- Bitmap image_encode;
- using (var fs = File.OpenRead(Directory.GetCurrentDirectory() + "\temp1.bmp"))// work_str_img_decode))
- {
- image_encode = new Bitmap(fs);
- }
- Bitmap image_domain;
- work_str_img_decode = Directory.GetCurrentDirectory() + "\temp1.bmp";
- //image_domain = decodeImg.Clone(new Rectangle(0,0,decodeImg.Width,decodeImg.Height), decodeImg.PixelFormat);
- using (var fs = File.OpenRead(work_str_img_decode))
- {
- image_domain = new Bitmap(fs);
- }
- rangPixels = new byte[image_encode.Height, image_encode.Width];
- rangPixels = proc.BitmapToByteRgb(image_encode);
- domainPixels = new byte[image_domain.Height, image_domain.Width];
- domainPixels = proc.BitmapToByteRgb(image_domain);
- CalcDomainBlocks();
- int coun = GetRangsCount();
- proB.BeginInvoke(new ThreadStart(delegate
- {
- proB.Maximum = coun;
- }));
- for (int cur_rang_index = 0; cur_rang_index < coun; cur_rang_index++)
- {
- proB.BeginInvoke(new ThreadStart(delegate
- {
- ProgressHandler(cur_rang_index);
- }));
- _code_record curRang = GetRange(cur_rang_index);
- TRect rect = add.GetRangeBlockRect(curRang.range_index, @params.quadtree_depth, ref domainPixels);
- int domain_index = Convert.ToInt32(curRang.domain_index);
- _Domain_Struct cur_domain = new _Domain_Struct();
- cur_domain.rect = domains[domain_index].rect;
- cur_domain.wigth = domains[domain_index].wigth;
- cur_domain.height = domains[domain_index].height;
- cur_domain.index = domains[domain_index].index;
- cur_domain.s = Convert.ToSingle(curRang.s);
- cur_domain.o = Convert.ToSingle(curRang.o);
- cur_domain.pixels = new byte[cur_domain.wigth, cur_domain.height];
- for (int i = 0; i < cur_domain.wigth; i++)
- {
- for (int j = 0; j < cur_domain.height; j++)
- {
- cur_domain.pixels[i, j] = domains[domain_index].pixels[i, j];
- }
- }
- _Domain_Struct rotate_domain = new _Domain_Struct();
- rotate_domain.rect = domains[domain_index].rect;
- rotate_domain.wigth = domains[domain_index].wigth;
- rotate_domain.height = domains[domain_index].height;
- rotate_domain.index = domains[domain_index].index;
- rotate_domain.s = Convert.ToSingle(curRang.s);
- rotate_domain.o = Convert.ToSingle(curRang.o);
- rotate_domain.pixels = new byte[cur_domain.wigth, cur_domain.height];
- int trans_index = Convert.ToInt32(curRang.trans_index);
- if (trans_index != 0)
- {
- add.RotateDomainRect(ref cur_domain, ref rotate_domain, trans_index);
- }
- else
- {
- add.RotateDomainRect(ref cur_domain, ref rotate_domain, 0);
- }
- _Domain_Struct new_domain = new _Domain_Struct();
- new_domain.rect = rect;
- new_domain.wigth = rect.Width();
- new_domain.height = rect.Height();
- new_domain.index = cur_domain.index;
- new_domain.s = cur_domain.s;
- new_domain.o = cur_domain.o;
- new_domain.pixels = new byte[rect.Width(), rect.Height()];
- cutTargetDomain(ref rotate_domain, ref new_domain);
- add.ApplySO(ref new_domain);
- PutDomainIntoRang(new_domain, rect);
- }
- work_str_img_decode = GetTempFilename();
- SaveEncodeImageMatrix(GetTempFilename());
- if (vis_img != null)
- {
- CopyEncodeMatrixToImage(ref vis_img);
- }
- inputImgBox1.Image = vis_img;
- }
- endingThreads();
- }
- private void PutDomainIntoRang(_Domain_Struct domain, TRect range)
- {
- for (int i = 0; i < domain.wigth; i++)
- {
- for (int j = 0; j < domain.height; j++)
- {
- rangPixels[range.left + i,range.top + j] = domain.pixels[i,j];
- }
- }
- }
- private void CopyEncodeMatrixToImage(ref Bitmap image)
- {
- Bitmap vis = new Bitmap(image.Width,image.Height);
- using (Graphics g = Graphics.FromImage(vis))
- {
- g.DrawImage(image, 0, 0);
- }
- for (int i = 0; i < image.Height; i++)
- {
- for (int j = 0; j < image.Width; j++)
- {
- vis.SetPixel(i,j, Color.FromArgb(rangPixels[i,j], rangPixels[i,j], rangPixels[i,j]));
- }
- }
- image = vis;
- }
- private void SaveEncodeImageMatrix(string filename)
- {
- Bitmap image = new Bitmap(rangPixels.GetUpperBound(0) + 1,rangPixels.GetUpperBound(1) + 1);
- for (int i = 0; i < image.Height; i++)
- {
- for (int j = 0; j < image.Width; j++)
- {
- image.SetPixel(i, j, Color.FromArgb(rangPixels[i, j], rangPixels[i, j], rangPixels[i, j]));
- }
- }
- using (image)
- {
- image.Save(filename);
- //inputImgBox1.Image = image;
- }
- }
- private string GetTempFilename()
- {
- return Directory.GetCurrentDirectory() + "\temp1.bmp";
- }
- public int GetRangsCount()
- {
- return list_rangs.Count;
- }
- public string StrChangeCharacter(string str, int index, Char newSymb)
- {
- return str.Remove(index-1, 1).Insert(index-1, newSymb.ToString());
- }
- public void SaveRangeFile(string filename)
- {
- var rangfile = new List<string>();
- rangfile.Clear();
- rangfile.Add((@params.domain_levels).ToString());
- rangfile.Add((@params.domain_cols).ToString());
- rangfile.Add((@params.domain_rows).ToString());
- rangfile.Add((@params.overlap_horz).ToString());
- rangfile.Add((@params.overlap_vert).ToString());
- rangfile.Add((@params.quadtree_depth).ToString());
- if (list_rangs != null)
- {
- proB.BeginInvoke(new ThreadStart(delegate
- {
- proB.Maximum = list_rangs.Count;
- }));
- for (int i = 0; i < list_rangs.Count; i++)
- {
- _code_record cur_rang = GetRange(i);
- rangfile.Add(cur_rang.range_index + ";" + cur_rang.domain_index + ";" + cur_rang.trans_index + ";"
- + cur_rang.s + ";" + cur_rang.o + ";" + (cur_rang.error).ToString());
- proB.BeginInvoke(new ThreadStart(delegate
- {
- ProgressHandler(i);
- }));
- }
- }
- else
- MessageBox.Show("Отсутствует таблица рангов!");
- SaveToFile(rangfile, filename);
- }
- private void SaveToFile(List<string> list, string fil)
- {
- TextWriter tw = new StreamWriter(fil);
- for (int i = 0; i < list.Count; i++)
- {
- tw.WriteLine(list[i]);
- }
- MessageBox.Show("Ранговый файл сохранен!");
- tw.Close();
- if (t1 != null)
- {
- t1.Abort();
- t1.Join();
- }
- }
- public struct _Domain_Struct
- {
- public TRect rect;
- public int level;
- public int wigth;
- public int height;
- public int index;
- public float s;
- public float o;
- public byte[,] pixels;
- }
- public struct _code_record
- {
- public string range_index;
- public string domain_index;
- public string trans_index;
- public string s;
- public string o;
- public float error;
- public List<_rang_record> allerrors;
- }
- public struct _rang_record
- {
- public string domain_index;
- public string trans_index;
- public string s;
- public string o;
- public float error;
- //min max sr
- //summerropr
- // (нормированную) поделить на сумму
- //list (newcoderecord)
- }
- public struct _blocks_sings
- {
- public float stand_dev;
- public float skewness;
- public float contrast;
- public float beta;
- public float hor_grad;
- public float vert_grad;
- public float max_grad;
- }
Add Comment
Please, Sign In to add comment