Guest User

Untitled

a guest
Mar 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.97 KB | None | 0 0
  1. private byte[,] rangPixels;
  2. private byte[,] domainPixels;
  3. Bitmap decodeImg;
  4. private List<_Domain_Struct> domains = new List<_Domain_Struct>();
  5. private List<_code_record> list_rangs;
  6. ImageProcessing proc = new ImageProcessing();
  7. public parametrsForm._coding_params @params = new parametrsForm._coding_params();
  8. AdditionalMethods add = new AdditionalMethods();
  9. string img_encode_filename;
  10.  
  11. public fractFuzzyImgProc()
  12. {
  13. InitializeComponent();
  14. }
  15.  
  16.  
  17. private void CalcDomainBlocks()
  18. {
  19. //3
  20. domains.Clear();
  21. int cur_domain_index = 0;
  22. for (int cur_level = 1; cur_level <= @params.domain_levels; cur_level++)
  23. {
  24. double buf1 = (@params.domain_cols * Math.Pow(2, (cur_level - 1)));
  25. double buf2 = (@params.domain_rows * Math.Pow(2, (cur_level - 1)));
  26. int domain_w = (int)((domainPixels.GetUpperBound(0) + 1) / buf1);
  27. int domain_h = (int)((domainPixels.GetUpperBound(1) + 1) / buf2);
  28.  
  29. int domains_per_hor_line = (int)(@params.domain_cols * Math.Pow(2, (cur_level - 1)));
  30. if (@params.overlap_horz == 50)
  31. {
  32. domains_per_hor_line += @params.domain_cols * cur_level - 1;
  33. }
  34. int domains_per_ver_line = (int)(@params.domain_rows * Math.Pow(2, (cur_level - 1)));
  35. if (@params.overlap_horz == 50)
  36. {
  37. domains_per_ver_line += @params.domain_rows * cur_level - 1;
  38. }
  39.  
  40. for (int cur_row = 0; cur_row < domains_per_hor_line; cur_row++)
  41. {
  42. for (int cur_col = 0; cur_col < domains_per_ver_line; cur_col++)
  43. {
  44. float k = 1F;
  45. if (@params.overlap_horz == 50)
  46. {
  47. k = 0.5F;
  48. }
  49.  
  50. _Domain_Struct cur_domain = new _Domain_Struct();
  51. cur_domain.index = cur_domain_index;
  52. cur_domain.level = cur_level;
  53. int left = 0;
  54. if (cur_row > 0)
  55. {
  56. left = (int)(cur_row * domain_w * k);
  57. }
  58. int rigth = 0;
  59. if (cur_col > 0)
  60. {
  61. rigth = (int)(cur_col * domain_h * k);
  62. }
  63.  
  64. cur_domain.rect = new TRect(left, rigth, left + domain_w, rigth + domain_h);
  65. cur_domain.pixels = new byte[domain_w, domain_h];
  66. for (int i = cur_domain.rect.left; i < cur_domain.rect.right; i++)
  67. {
  68. for (int j = cur_domain.rect.top; j < cur_domain.rect.bottom; j++)
  69. {
  70. cur_domain.pixels[i - cur_domain.rect.left, j - cur_domain.rect.top] = domainPixels[i, j];
  71. }
  72. }
  73. cur_domain.wigth = domain_w;
  74. cur_domain.height = domain_h;
  75. cur_domain.s = 0F;
  76. cur_domain.o = 0F;
  77. domains.Add(cur_domain);
  78. cur_domain_index++;
  79. }
  80. }
  81. }
  82. }
  83.  
  84. private void cutTargetDomain(ref _Domain_Struct domain, ref _Domain_Struct new_domain)
  85. { //11
  86. int aver_cols = (int)(domain.height * (1.0f / new_domain.wigth) + 0.5f);
  87. int aver_rows = (int)(domain.height * (1.0f / new_domain.wigth) + 0.5f);
  88. int sum;
  89. int n_aver = aver_cols * aver_rows;
  90. int row = 0;
  91. int col = 0;
  92.  
  93. for (int i = 0; i < new_domain.height; i++)
  94. {
  95. col = 0;
  96. for (int j = 0; j < new_domain.wigth; j++)
  97. {
  98. sum = 0;
  99. for (int k = row; k < row + aver_rows-1; k++)
  100. {
  101. for (int m = col; m < col + aver_cols-1; m++)
  102. {
  103. sum += domain.pixels[k, m];
  104. }
  105. }
  106. if (n_aver == 0)
  107. {
  108. MessageBox.Show("Деление на 0 !!!");
  109. }
  110. new_domain.pixels[i, j] = Convert.ToByte(sum / n_aver);
  111. col += aver_cols;
  112. if (col + aver_cols - 1 > domain.wigth)
  113. {
  114. col = domain.wigth - aver_cols + 1;
  115. }
  116. } // end j
  117. row += aver_rows;
  118. if (row + aver_rows - 1 > domain.height)
  119. {
  120. row = domain.height - aver_rows + 1;
  121. }
  122. } // end i
  123.  
  124. return;
  125. }
  126.  
  127. private void decodeBtn_Click(object sender, EventArgs e)
  128. {
  129.  
  130. proB.Value = 0;
  131. @params = parametrs.Value;
  132. string path = Directory.GetCurrentDirectory() + "//temp.bmp";
  133. if (decClick == false)
  134. {
  135. using (decodeImg)
  136. {
  137. decodeImg.Save(path);
  138. decClick = true;
  139. }
  140. }
  141. informDgv.Rows.Clear();
  142. codingBtn.Enabled = false;
  143. changeParamsBtn.Enabled = false;
  144. decodeBtn.Enabled = false;
  145. openRangBtn.Enabled = false;
  146. saveRangBtn.Enabled = false;
  147. inputImgBox1.Enabled = false;
  148. Bitmap temp = decodeImg;
  149. t1 = new Thread(() => StartDecoding(path, 1));
  150. //t1 = new Thread(() => StartDecoding2(img_encode_filename,img_encode_filename, temp, 1));
  151. t1.Start();
  152. }
  153.  
  154. public void StartDecoding2(string str_img_decode, string str_img_domain, Bitmap vis_img, int iter_count)
  155. {
  156. try
  157. {
  158. string work_str_img_decode = str_img_decode;
  159. string work_str_img_domain = str_img_domain;
  160. for (int cur_iter = 0; cur_iter < iter_count; cur_iter++)
  161. {
  162. Bitmap image_encode;
  163. //this.@params = @params;
  164. using (var fs = File.OpenRead(work_str_img_decode))
  165. {
  166. vis_img = new Bitmap(fs);
  167. }
  168. using (var fs = File.OpenRead(work_str_img_decode))
  169. {
  170. image_encode = new Bitmap(fs);
  171. }
  172. rangPixels = new byte[image_encode.Width, image_encode.Height];
  173. rangPixels = proc.BitmapToByteRgb(image_encode);
  174. Bitmap image_domain;
  175. using (var fs = File.OpenRead(work_str_img_domain))
  176. {
  177. image_domain = new Bitmap(fs);
  178. }
  179. domainPixels = new byte[image_domain.Width, image_domain.Height];
  180. domainPixels = proc.BitmapToByteRgb(image_domain);
  181. CalcDomainBlocks();
  182. for (int cur_rang_index = 0; cur_rang_index < GetRangsCount(); cur_rang_index++)
  183. {
  184. _code_record curRang = GetRange(cur_rang_index);
  185.  
  186. TRect rect = add.GetRangeBlockRect(curRang.range_index, @params.quadtree_depth, ref rangPixels);
  187.  
  188. int domain_index = Convert.ToInt32(curRang.domain_index);
  189.  
  190. _Domain_Struct cur_domain = new _Domain_Struct();
  191. cur_domain.rect = domains[domain_index].rect;
  192. cur_domain.wigth = domains[domain_index].wigth;
  193. cur_domain.height = domains[domain_index].height;
  194. cur_domain.index = domains[domain_index].index;
  195. cur_domain.s = Convert.ToSingle(curRang.s);
  196. cur_domain.o = Convert.ToSingle(curRang.o);
  197. ArrayResize(ref cur_domain.pixels, cur_domain.wigth, cur_domain.height);
  198. for (int i = 0; i < cur_domain.wigth; i++)
  199. {
  200. for (int j = 0; j < cur_domain.height; j++)
  201. {
  202. cur_domain.pixels[i, j] = domains[domain_index].pixels[i, j];
  203. }
  204. }
  205.  
  206. _Domain_Struct rotate_domain = new _Domain_Struct();
  207. rotate_domain.rect = domains[domain_index].rect;
  208. rotate_domain.wigth = domains[domain_index].wigth;
  209. rotate_domain.height = domains[domain_index].height;
  210. rotate_domain.index = domains[domain_index].index;
  211. rotate_domain.s = Convert.ToSingle(curRang.s);
  212. rotate_domain.o = Convert.ToSingle(curRang.o);
  213. ArrayResize(ref rotate_domain.pixels, cur_domain.wigth, cur_domain.height);
  214.  
  215. int trans_index = Convert.ToInt32(curRang.trans_index);
  216. if (trans_index != 0)
  217. {
  218. add.RotateDomainRect(ref cur_domain, ref rotate_domain, trans_index);
  219. }
  220. else
  221. {
  222. add.RotateDomainRect(ref cur_domain, ref rotate_domain, 0);
  223. }
  224.  
  225. _Domain_Struct new_domain = new _Domain_Struct();
  226. new_domain.rect = rect;
  227. new_domain.wigth = rect.Width();
  228. new_domain.height = rect.Height();
  229. new_domain.index = cur_domain.index;
  230. new_domain.s = cur_domain.s;
  231. new_domain.o = cur_domain.o;
  232. ArrayResize(ref new_domain.pixels, rect.Width(), rect.Height());
  233.  
  234. cutTargetDomain(ref rotate_domain, ref new_domain);
  235.  
  236. add.ApplySO(ref new_domain);
  237.  
  238. PutDomainIntoRang(new_domain, rect);
  239.  
  240. }
  241. work_str_img_decode = GetTempFilename();
  242. work_str_img_domain = GetTempFilename();
  243. SaveEncodeImageMatrix(GetTempFilename());
  244. if (vis_img != null)
  245. {
  246. CopyEncodeMatrixToImage(ref vis_img);
  247. }
  248. inputImgBox1.Image = vis_img;
  249. }
  250. }
  251. catch (NullReferenceException)
  252. {
  253. Console.WriteLine("darova");
  254. }
  255. endingThreads();
  256.  
  257. }
  258. public _code_record GetRange(int index)
  259. {
  260. return list_rangs[index];
  261. }
  262. string work_str_img_decode;
  263. public void StartDecoding(string str_img_decode, int iter_count)
  264. {
  265. if (work_str_img_decode== null)
  266. work_str_img_decode = str_img_decode;
  267. vis_img = new Bitmap(512, 512);
  268. Bitmap vis = inputImgBox1.Image as Bitmap; ; //new Bitmap(512,512);
  269. using (Graphics g = Graphics.FromImage(vis_img))
  270. {
  271. g.DrawImage(vis, 0, 0);
  272. }
  273. for (int cur_iter = 0; cur_iter < iter_count; cur_iter++)
  274. {
  275. Bitmap image_encode;
  276. using (var fs = File.OpenRead(Directory.GetCurrentDirectory() + "\temp1.bmp"))// work_str_img_decode))
  277. {
  278. image_encode = new Bitmap(fs);
  279. }
  280. Bitmap image_domain;
  281. work_str_img_decode = Directory.GetCurrentDirectory() + "\temp1.bmp";
  282. //image_domain = decodeImg.Clone(new Rectangle(0,0,decodeImg.Width,decodeImg.Height), decodeImg.PixelFormat);
  283. using (var fs = File.OpenRead(work_str_img_decode))
  284. {
  285. image_domain = new Bitmap(fs);
  286. }
  287. rangPixels = new byte[image_encode.Height, image_encode.Width];
  288. rangPixels = proc.BitmapToByteRgb(image_encode);
  289. domainPixels = new byte[image_domain.Height, image_domain.Width];
  290. domainPixels = proc.BitmapToByteRgb(image_domain);
  291.  
  292. CalcDomainBlocks();
  293. int coun = GetRangsCount();
  294. proB.BeginInvoke(new ThreadStart(delegate
  295. {
  296. proB.Maximum = coun;
  297. }));
  298.  
  299. for (int cur_rang_index = 0; cur_rang_index < coun; cur_rang_index++)
  300. {
  301. proB.BeginInvoke(new ThreadStart(delegate
  302. {
  303. ProgressHandler(cur_rang_index);
  304. }));
  305. _code_record curRang = GetRange(cur_rang_index);
  306. TRect rect = add.GetRangeBlockRect(curRang.range_index, @params.quadtree_depth, ref domainPixels);
  307. int domain_index = Convert.ToInt32(curRang.domain_index);
  308.  
  309. _Domain_Struct cur_domain = new _Domain_Struct();
  310. cur_domain.rect = domains[domain_index].rect;
  311. cur_domain.wigth = domains[domain_index].wigth;
  312. cur_domain.height = domains[domain_index].height;
  313. cur_domain.index = domains[domain_index].index;
  314. cur_domain.s = Convert.ToSingle(curRang.s);
  315. cur_domain.o = Convert.ToSingle(curRang.o);
  316. cur_domain.pixels = new byte[cur_domain.wigth, cur_domain.height];
  317. for (int i = 0; i < cur_domain.wigth; i++)
  318. {
  319. for (int j = 0; j < cur_domain.height; j++)
  320. {
  321. cur_domain.pixels[i, j] = domains[domain_index].pixels[i, j];
  322. }
  323. }
  324.  
  325. _Domain_Struct rotate_domain = new _Domain_Struct();
  326. rotate_domain.rect = domains[domain_index].rect;
  327. rotate_domain.wigth = domains[domain_index].wigth;
  328. rotate_domain.height = domains[domain_index].height;
  329. rotate_domain.index = domains[domain_index].index;
  330. rotate_domain.s = Convert.ToSingle(curRang.s);
  331. rotate_domain.o = Convert.ToSingle(curRang.o);
  332. rotate_domain.pixels = new byte[cur_domain.wigth, cur_domain.height];
  333.  
  334. int trans_index = Convert.ToInt32(curRang.trans_index);
  335. if (trans_index != 0)
  336. {
  337. add.RotateDomainRect(ref cur_domain, ref rotate_domain, trans_index);
  338. }
  339. else
  340. {
  341. add.RotateDomainRect(ref cur_domain, ref rotate_domain, 0);
  342. }
  343.  
  344. _Domain_Struct new_domain = new _Domain_Struct();
  345. new_domain.rect = rect;
  346. new_domain.wigth = rect.Width();
  347. new_domain.height = rect.Height();
  348. new_domain.index = cur_domain.index;
  349. new_domain.s = cur_domain.s;
  350. new_domain.o = cur_domain.o;
  351. new_domain.pixels = new byte[rect.Width(), rect.Height()];
  352.  
  353. cutTargetDomain(ref rotate_domain, ref new_domain);
  354.  
  355. add.ApplySO(ref new_domain);
  356.  
  357. PutDomainIntoRang(new_domain, rect);
  358.  
  359. }
  360. work_str_img_decode = GetTempFilename();
  361. SaveEncodeImageMatrix(GetTempFilename());
  362. if (vis_img != null)
  363. {
  364. CopyEncodeMatrixToImage(ref vis_img);
  365. }
  366. inputImgBox1.Image = vis_img;
  367.  
  368. }
  369. endingThreads();
  370. }
  371.  
  372. private void PutDomainIntoRang(_Domain_Struct domain, TRect range)
  373. {
  374. for (int i = 0; i < domain.wigth; i++)
  375. {
  376. for (int j = 0; j < domain.height; j++)
  377. {
  378. rangPixels[range.left + i,range.top + j] = domain.pixels[i,j];
  379. }
  380. }
  381. }
  382.  
  383. private void CopyEncodeMatrixToImage(ref Bitmap image)
  384. {
  385. Bitmap vis = new Bitmap(image.Width,image.Height);
  386. using (Graphics g = Graphics.FromImage(vis))
  387. {
  388. g.DrawImage(image, 0, 0);
  389. }
  390. for (int i = 0; i < image.Height; i++)
  391. {
  392. for (int j = 0; j < image.Width; j++)
  393. {
  394. vis.SetPixel(i,j, Color.FromArgb(rangPixels[i,j], rangPixels[i,j], rangPixels[i,j]));
  395. }
  396. }
  397. image = vis;
  398. }
  399.  
  400. private void SaveEncodeImageMatrix(string filename)
  401. {
  402. Bitmap image = new Bitmap(rangPixels.GetUpperBound(0) + 1,rangPixels.GetUpperBound(1) + 1);
  403. for (int i = 0; i < image.Height; i++)
  404. {
  405. for (int j = 0; j < image.Width; j++)
  406. {
  407. image.SetPixel(i, j, Color.FromArgb(rangPixels[i, j], rangPixels[i, j], rangPixels[i, j]));
  408. }
  409. }
  410. using (image)
  411. {
  412. image.Save(filename);
  413. //inputImgBox1.Image = image;
  414. }
  415. }
  416.  
  417. private string GetTempFilename()
  418. {
  419. return Directory.GetCurrentDirectory() + "\temp1.bmp";
  420. }
  421.  
  422. public int GetRangsCount()
  423. {
  424. return list_rangs.Count;
  425. }
  426.  
  427. public string StrChangeCharacter(string str, int index, Char newSymb)
  428. {
  429. return str.Remove(index-1, 1).Insert(index-1, newSymb.ToString());
  430. }
  431.  
  432. public void SaveRangeFile(string filename)
  433. {
  434. var rangfile = new List<string>();
  435. rangfile.Clear();
  436.  
  437. rangfile.Add((@params.domain_levels).ToString());
  438. rangfile.Add((@params.domain_cols).ToString());
  439. rangfile.Add((@params.domain_rows).ToString());
  440. rangfile.Add((@params.overlap_horz).ToString());
  441. rangfile.Add((@params.overlap_vert).ToString());
  442. rangfile.Add((@params.quadtree_depth).ToString());
  443.  
  444. if (list_rangs != null)
  445. {
  446. proB.BeginInvoke(new ThreadStart(delegate
  447. {
  448. proB.Maximum = list_rangs.Count;
  449. }));
  450. for (int i = 0; i < list_rangs.Count; i++)
  451. {
  452. _code_record cur_rang = GetRange(i);
  453. rangfile.Add(cur_rang.range_index + ";" + cur_rang.domain_index + ";" + cur_rang.trans_index + ";"
  454. + cur_rang.s + ";" + cur_rang.o + ";" + (cur_rang.error).ToString());
  455. proB.BeginInvoke(new ThreadStart(delegate
  456. {
  457. ProgressHandler(i);
  458. }));
  459.  
  460. }
  461. }
  462. else
  463. MessageBox.Show("Отсутствует таблица рангов!");
  464. SaveToFile(rangfile, filename);
  465. }
  466.  
  467. private void SaveToFile(List<string> list, string fil)
  468. {
  469. TextWriter tw = new StreamWriter(fil);
  470. for (int i = 0; i < list.Count; i++)
  471. {
  472. tw.WriteLine(list[i]);
  473. }
  474. MessageBox.Show("Ранговый файл сохранен!");
  475. tw.Close();
  476. if (t1 != null)
  477. {
  478. t1.Abort();
  479. t1.Join();
  480. }
  481. }
  482.  
  483. public struct _Domain_Struct
  484. {
  485. public TRect rect;
  486. public int level;
  487. public int wigth;
  488. public int height;
  489. public int index;
  490. public float s;
  491. public float o;
  492. public byte[,] pixels;
  493. }
  494.  
  495. public struct _code_record
  496. {
  497. public string range_index;
  498. public string domain_index;
  499. public string trans_index;
  500. public string s;
  501. public string o;
  502. public float error;
  503. public List<_rang_record> allerrors;
  504. }
  505. public struct _rang_record
  506. {
  507. public string domain_index;
  508. public string trans_index;
  509. public string s;
  510. public string o;
  511. public float error;
  512. //min max sr
  513. //summerropr
  514. // (нормированную) поделить на сумму
  515. //list (newcoderecord)
  516. }
  517.  
  518. public struct _blocks_sings
  519. {
  520. public float stand_dev;
  521. public float skewness;
  522. public float contrast;
  523. public float beta;
  524. public float hor_grad;
  525. public float vert_grad;
  526. public float max_grad;
  527. }
Add Comment
Please, Sign In to add comment