/// <returns>true if 'm' is symmetric up to a small epsilon constant</returns> public static bool IsSymmetric(DotNetMatrix.GeneralMatrix m) { const double EPS = 1e-6; DotNetMatrix.GeneralMatrix Z = m.Transpose(); Z = Z.Subtract(m); return(Z.NormInf() < EPS); }
public calibrationvalues sensorToTarget(int target, int chart, imagestats.colorvals[] meanvalues, int OBLevel) { // apply OB offset and do gain correction //int OBLevel = (int)num_OBLevel.Value; double OBComp = 4095.0 / (4095.0 - OBLevel); for (int i = 0; i < 24; i++) { meanvalues[i].R -= OBLevel >> 2; meanvalues[i].R *= OBComp; meanvalues[i].GR -= OBLevel >> 2; meanvalues[i].GR *= OBComp; meanvalues[i].GB -= OBLevel >> 2; meanvalues[i].GB *= OBComp; meanvalues[i].B -= OBLevel >> 2; meanvalues[i].B *= OBComp; } // reorder colors to B,G,G,R /*double temp; for (int i = 0; i < 24; i++) { //for (int j = 0; j < 4; j++) { if (rawBayerOrder == 2) { temp = meanvalues[i, 0]; meanvalues[i, 0] = meanvalues[i, 3]; meanvalues[i, 3] = temp; } //} }*/ // measure WB correction double[,] WBRatios = new double[4, 2]; // 4 patches, 2 ratios (R/G, B/G) //for (int i = 0; i < 24; i++) { for (int j = 0; j < 4; j++) { WBRatios[j, 0] = meanvalues[j + 19].GR / meanvalues[j + 19].R; WBRatios[j, 1] = meanvalues[j + 19].GB / meanvalues[j + 19].B; } //} // calculate WB correction double RGain = 0, BGain = 0; for (int i = 0; i < 4; i++) { BGain += WBRatios[i, 0]; RGain += WBRatios[i, 1]; } RGain /= 4; BGain /= 4; // apply WB correction for (int i = 0; i < 24; i++) { meanvalues[i].R = meanvalues[i].R * BGain; meanvalues[i].B = meanvalues[i].B * RGain; } // measure exposure compensation double[] GainRatios = new double[4]; // 4 patches, green channel double[] macBethGreens = new double[4] { 148.4 * 4, 90.6 * 4, 47.8 * 4, 22.2 * 4 }; for (int j = 0; j < 4; j++) { GainRatios[j] = macBethGreens[j] / ((meanvalues[j + 19].GR + meanvalues[j + 19].GB)/2); } // calculate exposure compensation double ExposureComp = 0; for (int i = 0; i < 4; i++) { ExposureComp += GainRatios[i]; } ExposureComp /= 4; // apply exposure compensation for (int i = 0; i < 24; i++) { meanvalues[i].R = meanvalues[i].R * ExposureComp; meanvalues[i].GR = meanvalues[i].GR * ExposureComp; meanvalues[i].GB = meanvalues[i].GB * ExposureComp; meanvalues[i].B = meanvalues[i].B * ExposureComp; } // target macbeth color values (linear RGB from sRGB) double[][] macBethColors = {new double[]{44,21,15}, new double[]{140,76,55}, new double[]{28,50,86}, new double[]{27,38,13}, new double[]{57,56,110}, new double[]{31,132,103}, new double[]{183,51,7}, new double[]{17,27,100}, new double[]{138,23,31}, new double[]{27,11,36}, new double[]{90,129,12}, new double[]{199,90,6}, new double[]{6,13,74}, new double[]{17,77,16}, new double[]{109,8,10}, new double[]{219,147,2}, new double[]{128,23,78}, new double[]{0,63,98}, new double[]{234,233,222}, new double[]{148,151,149}, new double[]{91,92,92}, new double[]{48,49,49}, new double[]{22,23,23}, new double[]{8,8,8}}; double[][] inputRGB = { new double[24], new double[24], new double[24] }; for (int i = 0; i < 24; i++) { inputRGB[0][i] = meanvalues[i].B / 4; inputRGB[1][i] = (meanvalues[i].GB + meanvalues[i].GR) / 8; inputRGB[2][i] = meanvalues[i].R / 4; } DotNetMatrix.GeneralMatrix RGBin = new DotNetMatrix.GeneralMatrix(inputRGB); RGBin = RGBin.Transpose(); double[][] RedOut = { new double[24] }; double[][] GreenOut = { new double[24] }; double[][] BlueOut = { new double[24] }; for (int i = 0; i < 24; i++) { RedOut[0][i] = macBethColors[i][0]; GreenOut[0][i] = macBethColors[i][1]; BlueOut[0][i] = macBethColors[i][2]; } DotNetMatrix.GeneralMatrix invA = RGBin.Inverse(); DotNetMatrix.GeneralMatrix b = new DotNetMatrix.GeneralMatrix(RedOut); DotNetMatrix.GeneralMatrix bT = b.Transpose(); DotNetMatrix.GeneralMatrix RGB2RGB_R = invA.Multiply(bT); b = new DotNetMatrix.GeneralMatrix(GreenOut); bT = b.Transpose(); DotNetMatrix.GeneralMatrix RGB2RGB_G = invA.Multiply(bT); b = new DotNetMatrix.GeneralMatrix(BlueOut); bT = b.Transpose(); DotNetMatrix.GeneralMatrix RGB2RGB_B = invA.Multiply(bT); calibrationvalues output = new calibrationvalues(); //loadingControls = true; output.exposuregain = ExposureComp; output.RGain = RGain; output.BGain = BGain; output.RGB_RR = 1 - (double)RGB2RGB_R.Array[1][0] - (double)RGB2RGB_R.Array[2][0]; output.RGB_RG = (double)RGB2RGB_R.Array[1][0]; output.RGB_RB = (double)RGB2RGB_R.Array[2][0]; output.RGB_GR = (double)RGB2RGB_G.Array[0][0]; output.RGB_GG = 1 - (double)RGB2RGB_G.Array[0][0] - (double)RGB2RGB_G.Array[2][0]; output.RGB_GB = (double)RGB2RGB_G.Array[2][0]; output.RGB_BR = (double)RGB2RGB_B.Array[0][0]; output.RGB_BG = (double)RGB2RGB_B.Array[1][0]; output.RGB_BB = 1 - (double)RGB2RGB_B.Array[0][0] - (double)RGB2RGB_B.Array[1][0]; //loadingControls = false; return output; }
/// <summary> /// Find the documents related to the query /// </summary> private void FindThings() { // Declaring variables required for calculations Hashtable WordList = new Hashtable(); Hashtable DocList = new Hashtable(); DotNetMatrix.GeneralMatrix uk = null; DotNetMatrix.GeneralMatrix skinv = null; DotNetMatrix.GeneralMatrix oWTDM = null; DocIndex oDocIndex; oDocIndex.DocList = null; oDocIndex.WordList = null; oDocIndex.uk = null; oDocIndex.WTDM = null; oDocIndex.skinv = null; // Read and load the index document string index_path = LSICommon.Instance.LSIAppPath + @"\index.bin"; bool bRead = false; BinaryFormatter bf = new BinaryFormatter(); if (File.Exists(index_path)) { try { FileStream f = File.Open(LSICommon.Instance.LSIAppPath + @"\index.bin", FileMode.Open); oDocIndex = (DocIndex)bf.Deserialize(f); f.Close(); bRead = true; } catch { bRead = false; } } else { bRead = false; } if (!bRead) { MessageBox.Show("Index not created. Please index the documents"); return; } // Restore the wordlist for (int i = 0; i < oDocIndex.WordList.Length; i++) { WordList.Add(oDocIndex.WordList[i].key, oDocIndex.WordList[i].value); } // Restore the documentlist for (int i = 0; i < oDocIndex.DocList.Length; i++) { DocList.Add(oDocIndex.DocList[i].key, oDocIndex.DocList[i].value); } // Restore the other SVD derived vectors and weighted TDM uk = new DotNetMatrix.GeneralMatrix(oDocIndex.uk); skinv = new DotNetMatrix.GeneralMatrix(oDocIndex.skinv); oWTDM = new DotNetMatrix.GeneralMatrix(oDocIndex.WTDM); // Get the query string newquery = txtQuery.Text; string[] newqwords = LSICommon.Instance.GetWords(newquery); // Create the query vector DotNetMatrix.GeneralMatrix qt = new DotNetMatrix.GeneralMatrix(1, WordList.Count); for (int i = 0; i < WordList.Count; i++) { qt.Array[0][i] = 0; } // Apply stemming to the query vector PorterStemmer oStemmer = new PorterStemmer(); for (int i = 0; i < newqwords.Length; i++) { newqwords[i] = oStemmer.stemTerm(newqwords[i]); if (WordList.ContainsKey(newqwords[i])) { qt.Array[0][(int)WordList[newqwords[i]]] += 1; } } // Normalizing the query vector double qtmax = 0; for (int i = 0; i < qt.Array[0].Length; i++) { if (qt.Array[0][i] > qtmax) { qtmax = qt.Array[0][i]; } } double[] simarray = new double[DocList.Count]; if (qtmax != 0) { for (int i = 0; i < qt.Array[0].Length; i++) { qt.Array[0][i] = (qt.Array[0][i] / qtmax); } // Calculate the resultant query vector DotNetMatrix.GeneralMatrix qt_uk = qt.Multiply(uk); // qT * Uk DotNetMatrix.GeneralMatrix q = qt_uk.Multiply(skinv); // Resultant q // Calculate similarities now ( with each derived document vector) for (int l = 0; l < DocList.Count; l++) { DotNetMatrix.GeneralMatrix d_v = new DotNetMatrix.GeneralMatrix(WordList.Count, 1); for (int k = 0; k < WordList.Count; k++) { d_v.Array[k][0] = oWTDM.Array[k][l]; } DotNetMatrix.GeneralMatrix dt = d_v.Transpose(); // dT DotNetMatrix.GeneralMatrix dt_uk = dt.Multiply(uk); // dT * uk DotNetMatrix.GeneralMatrix d = dt_uk.Multiply(skinv); // Resultant d // Calculate cosine similarity: simarray[l] = 0; simarray[l] = calculate_cosine_sim(q.Array[0], d.Array[0]); } } else { MessageBox.Show("No results found"); // Since qtmax is zero, the query vector is null. This implies that there can be no results } // Display the results in datagrid DataTable oResTable = new DataTable(); DataRow oResDR = null; oResTable.Columns.Add("filename", typeof(string)); oResTable.Columns.Add("val", typeof(double)); for (int i = 0; i < simarray.Length; i++) { oResDR = oResTable.NewRow(); oResDR["val"] = (Math.Round(simarray[i] * 100, 6)); oResDR["filename"] = DocList[i].ToString(); oResTable.Rows.Add(oResDR); } dataGridView1.DataSource = oResTable; // Sort the results in descending order dataGridView1.Sort(dataGridView1.Columns["val"], ListSortDirection.Descending); // Formatting the results dataGridView1.Columns["val"].HeaderText = "Rating/100"; dataGridView1.Columns["filename"].HeaderText = "Document path"; dataGridView1.Columns["val"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; dataGridView1.Columns["filename"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.RowHeadersVisible = false; DataGridViewCellStyle oCellStyle = new DataGridViewCellStyle(); oCellStyle.BackColor = Color.AliceBlue; dataGridView1.AlternatingRowsDefaultCellStyle = oCellStyle; try { foreach (DataGridViewRow oRow in dataGridView1.Rows) { oRow.Cells["filename"].ToolTipText = ReadShortTextContent(oRow.Cells["filename"].Value.ToString().Trim()); } } catch { //Supress any exception here } }