private void btnPredict_OP_tb4_Click(object sender, EventArgs e) { // Start stop watch to time Stopwatch s = new Stopwatch(); s.Start(); int finalgen; try { finalgen = int.Parse(txtFinalYearstb4.Text); } catch { MessageBox.Show("Enter number of years greater than 0."); return; } // Get value of rows & col if (list.Count == 0) { MessageBox.Show("Driving factors have not been assigned"); return; } if (finalpath.Count == 0) { MessageBox.Show("Load final layers to start prediction."); return; } using (Tiff image = Tiff.Open(finalpath.ElementAt(0), "r")) { if (image == null) { MessageBox.Show("Could not open layer: " + list.ElementAt(0).LayerName); return; } // Check if it is a type we support FieldValue[] value = image.GetField(TiffTag.SAMPLESPERPIXEL); if (value == null) { MessageBox.Show("Undefined number of samples per pixel"); return; } short spp = value[0].ToShort(); if (spp != 1) { MessageBox.Show("Samples per pixel is not 1"); return; } value = image.GetField(TiffTag.BITSPERSAMPLE); if (value == null) { MessageBox.Show("Undefined number of bits per sample"); return; } short bps = value[0].ToShort(); if (bps != 8) { MessageBox.Show("Bits per sample is not 8"); return; } value = image.GetField(TiffTag.IMAGEWIDTH); if (value == null) { MessageBox.Show("Image does not define its width"); return; } col = value[0].ToInt(); value = image.GetField(TiffTag.IMAGELENGTH); if (value == null) { MessageBox.Show("Image does not define its width"); return; } row = value[0].ToInt(); image.Close(); } // Start of computation byte[] ca_check = new byte[row * col]; for (int gencnt = 0; gencnt < finalgen; gencnt++) { double[,] Dsp1 = new double[row, col]; if (gencnt == 0) { Read_Write r = new Read_Write(); byte[] ca_array = new byte[row * col]; ca_check = r.ReadImage(finalpath.ElementAt(0), row, col); if (ca_check == null) { MessageBox.Show("Error in loading layer: " + list.ElementAt(0).LayerName); return; } ca_array = CA(ca_check); Dsp1 = CA_Suitability(ca_array); } else { byte[] ca_array = new byte[row * col]; ca_array = CA(ca_check); Dsp1 = CA_Suitability(ca_array); } for (int t = 1; t < attrno; t++) { byte[] buf; Read_Write ro = new Read_Write(); buf = ro.ReadImage(finalpath.ElementAt(t), row, col); if (buf == null) { MessageBox.Show("Error in loading layer: " + finalpath.ElementAt(t)); return; } Parallel.For(0, row, R => { int r = R * col; for (int y = 0; y < col; y++) { double gain = Math.Pow(list.ElementAt(t).LayerWeight, p); double growth = ((list.ElementAt(t).LayerGrowth) / 100) + 1; double groyr = Math.Pow(growth, gencnt); double cellvalue = (double)(buf[r + y]) * groyr; if (cellvalue <= 100) { double change = Math.Pow(cellvalue, p); Dsp1[R, y] += gain * change; } else if (cellvalue > 100) { double change = Math.Pow(100, p); Dsp1[R, y] += gain * change; } } }); } double DSpmax = 0; // Find DSpmax Parallel.For(0, row, R => { int r = R * col; for (int j = 0; j < col; j++) { if (DSpmax < Dsp1[R, j]) { DSpmax = Dsp1[R, j]; } } }); // Compute and compare with cut of prob Parallel.For(0, row, R => { int r = R * col; for (int y = 0; y < col; y++) { if (ca_check[r + y] == 50) { double val = α * (1 - Math.Pow(Dsp1[R, y] / DSpmax, 1.0 / p)); val = Math.Pow(2.71828, val); Dsp1[R, y] = 1.0 / val; if (Dsp1[R, y] > Pnu) { ca_check[r + y] = 100; } } } }); } Read_Write w = new Read_Write(); try { picbx_Output_tb4.Load(w.WriteImagePrediction(ca_check, row, col)); } catch { MessageBox.Show("Error in writing prediction image."); } s.Stop(); long end = s.ElapsedMilliseconds; MessageBox.Show("Time taken to complete " + end + " Milliseconds."); }
private void btnV_Loadtb3_Click(object sender, EventArgs e) { string s = loadnewV_filetb3(); if (s == "") { MessageBox.Show("Could not load file."); return; } // Get result of testing stage Read_Write test = new Read_Write(); string file = "ans" + Read_Write.simcnt.ToString() + ".tif"; byte[] predicted = test.ReadImage(file, row, col); if (predicted == null) { MessageBox.Show("No simulation image found.. !! Check if simulation was done."); return; } Read_Write calibrate = new Read_Write(); byte[] orginal = calibrate.ReadImage(txtV_FilePathtb3.Text, row, col); if (orginal == null) { MessageBox.Show("Could not real image to be calibrated."); return; } // Initialise counters int CPcnt = 0; int CError = 0; int OError = 0; int excluded_cell = 0; byte[] calibimage = new byte[row * col]; for (int i = 0; i < row * col; i++) { // Correctly predicted if (predicted[i] == orginal[i] && orginal[i] != 0) { CPcnt++; calibimage[i] = 255; } // Comission error else if (predicted[i] == 100 && orginal[i] == 50) { CError++; calibimage[i] = 150; } // Omission Error else if (predicted[i] == 50 && orginal[i] == 100) { OError++; calibimage[i] = 50; } // Excluded cells else if (orginal[i] == 0) { excluded_cell++; calibimage[i] = 0; } } // Set these values into textboxes txtV_CP_tb3.Text = CPcnt.ToString(); txtV_CError_tb3.Text = CError.ToString(); txtV_OError_tb3.Text = OError.ToString(); // Compute % of correct cells int total = row * col - excluded_cell; double correct = ((double)((CPcnt * 100.0) / total)); double CE = ((double)((CError * 100.0) / total)); double OE = ((double)((OError * 100.0) / total)); txtV_CPercent_tb3.Text = correct.ToString(); txtV_CEPercent_tb3.Text = CE.ToString(); txtV_OEPercent_tb3.Text = OE.ToString(); picbxV_op_tb3.Load(test.WriteImageCV(calibimage, row, col, 2)); }