/// <summary> /// Deletes the categories of the series /// </summary> /// <param name="DBC">The controller through which categories deletion will be done</param> private bool DeleteCats(databaseController DBC) { string sqlstr; if (catList.Count < 1) { return(true); } try { sqlstr = "DELETE FROM Series_Cats WHERE (SID = " + this.id + " ) ;"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Updates an existing patiant /// </summary> /// <param name="DBC">The controller through which patiant update will be done</param> public bool UpdatePatiant(databaseController DBC) { //Declarations string sqlstr; try { sqlstr = "UPDATE Patiant SET "; sqlstr = sqlstr + "Name ='" + this.name + "',"; sqlstr = sqlstr + "Age = " + this.age + " ,"; sqlstr = sqlstr + "Info = '" + this.info + "' "; sqlstr = sqlstr + "WHERE PID =" + this.id + " ;"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Inserts image's features into the database /// </summary> /// <param name="DBC">The controller through which features insertion will be done</param> private bool InsertImageFeatures(databaseController DBC) { try { string f_insert = "INSERT INTO FeaturesVector VALUES ( " + this.id; foreach (Feature f in featureVector) { f_insert += "," + f.FeatureValue; } f_insert += " );"; f_insert = f_insert.ToLower().Replace("nan", "0"); if (DBC.IsConnected) { DBC.CommandText = f_insert; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception ex) { return(false); } }
/// <summary> /// Updates image's features in the database /// </summary> /// <param name="DBC">The controller through which features update will be done</param> private bool UpdateImageFeatures(databaseController DBC) { //Declarations string sqlstr; try { sqlstr = "UPDATE FeaturesVector SET "; foreach (Feature f in featureVector) { sqlstr = sqlstr + f.FeatureName + " = " + f.FeatureValue.ToString() + " ,"; } //sqlstr = sqlstr + "Tags ='" + this.tags + "' ,"; //sqlstr = sqlstr + "ImagePath ='" + this.url + "',"; //sqlstr = sqlstr + "Category = " + this.cat.ID + " ,"; //sqlstr = sqlstr + "Size = " + this.imageInfo[0] + " ,"; //sqlstr = sqlstr + "Dimensions = '" + this.imageInfo[1] + "' ,"; sqlstr = sqlstr.Trim(','); sqlstr = sqlstr + " WHERE ImageID =" + this.id + " ;"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Inserts the notes of this series /// </summary> /// <param name="DBC">The controller through which notes insertion will be done</param> private bool InsertNotes(databaseController DBC) { string sqlstr = ""; if (this.notes == "") { return(true); } try { sqlstr = "INSERT INTO Series_Notes VALUES ( " + this.id; sqlstr += " , '" + this.notes; sqlstr += "' );"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception ex) { return(false); } }
/// <summary> /// Updates an existing series /// </summary> /// <param name="DBC">The controller through which series update will be done</param> public bool UpdateSeries(databaseController DBC) { string sqlstr; try { sqlstr = "UPDATE Series SET "; sqlstr = sqlstr + "PID = " + this.pid + " ,"; sqlstr = sqlstr + "FolderPath = '" + this.path + "' "; sqlstr = sqlstr + "WHERE SID =" + this.id + " ;"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; if (DBC.ExecuteNonQuery() > 0) { DBC.CommandText = "DELETE FROM Series_Notes WHERE SID = " + this.id + " ;"; DBC.ExecuteNonQuery(); if (InsertNotes(DBC)) { DBC.CommandText = "DELETE FROM Series_Cats WHERE SID = " + this.id + " ;"; DBC.ExecuteNonQuery(); if (InsertCats(DBC)) { return(true); } else { throw new Exception("Failed to modify categories of series !"); } } else { throw new Exception("Failed to modify notes of series !"); } } else { throw new Exception("Failed to modify series !"); } } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Inserts new series to the system /// </summary> /// <param name="DBC">The controller through which series insertion will be done</param> public bool InsertSeries(databaseController DBC) { string sqlstr; try { sqlstr = "INSERT INTO Series VALUES ( " + this.id + " , " + this.pid + " , '" + this.Path + "' );"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; if (DBC.ExecuteNonQuery() > 0) { if (InsertCats(DBC)) { if (InsertNotes(DBC)) { return(true); } else { throw new Exception("Failed to insert the note of series !"); } } else { throw new Exception("Failed to insert the categories of series !"); } } else { throw new Exception("Failed to insert the series !"); } } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Deletes an existing series /// </summary> /// <param name="DBC">The controller through which series deletion will be done</param> public bool DeleteSeries(databaseController DBC) { string sqlstr; try { if (DeleteCats(DBC)) { if (DeleteNotes(DBC)) { if (DBC.IsConnected) { sqlstr = "DELETE FROM Series WHERE SID = " + this.id + " ;"; DBC.CommandText = sqlstr; if (DBC.ExecuteNonQuery() > 0) { return(true); } else { throw new Exception("Failed to delete the series !"); } } else { throw new Exception("The database is not connected!"); } } else { throw new Exception("Failed to delete the notes of series !"); } } else { throw new Exception("Failed to delete the categories of series !"); } } catch (Exception e) { return(false); } }
/// <summary> /// Modifies an image in the database /// </summary> /// <param name="DBC">The controller through which the modification will be done</param> public bool ModifyImage(databaseController DBC) { //Declarations string sqlstr; try { sqlstr = "UPDATE Image SET "; sqlstr = sqlstr + "FileName ='" + this.fileName + "',"; sqlstr = sqlstr + "Size = " + this.imageInfo[0] + " ,"; sqlstr = sqlstr + "Dimensions = '" + this.imageInfo[1] + "' "; sqlstr = sqlstr + "WHERE ImageID =" + this.id + " ;"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; if (DBC.ExecuteNonQuery() > 0) { if (UpdateImageFeatures(DBC)) { return(true); } else { throw new Exception("Failed to modify the image !"); } } else { throw new Exception("Failed to modify the image !"); } } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Deletes an existing patiant from the database /// </summary> /// <param name="DBC">The controller through which patiant deletion will be done</param> public bool DeletePatiant(databaseController DBC) { string sqlstr; try { if (DBC.IsConnected) { sqlstr = "DELETE FROM Patiant WHERE PID= " + this.id + " ;"; DBC.CommandText = sqlstr; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Inserts an image into the database /// </summary> /// <param name="DBC">The controller through which the insertion will be done</param> public bool InsertImage(databaseController DBC) { //Declarations string sqlstr; try { sqlstr = "INSERT INTO Image VALUES ( " + this.id + " , '" + this.fileName + "' , " + this.imageInfo[0] + " , '" + this.imageInfo[1] + "' );"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; if (DBC.ExecuteNonQuery() > 0) { if (InsertImageFeatures(DBC)) { return(true); } else { throw new Exception("Failed to insert the features of image !"); } } else { throw new Exception("Failed to insert the image !"); } } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Deletes an image category from the database /// </summary> /// <param name="dbc">The controller through which the insertion will be done</param> /// <param name="DBC">The controller through which the deletion will be done</param> public bool DeleteCategory(databaseController DBC) { //Declarations string sqlstr; try { sqlstr = "DELETE FROM Category WHERE CatID= " + this.id + " ;"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Deletes image's features from the database /// </summary> /// <param name="DBC">The controller through which features deletion will be done</param> private bool DeleteImageFeatures(databaseController DBC) { //Declarations string sqlstr; try { sqlstr = "DELETE FROM FeaturesVector WHERE ImageID= " + this.id + " ;"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Deletes an image from the database. /// </summary> /// <param name="DBC">The controller through which the deletion will be done</param> public bool DeleteImage(databaseController DBC) { //Declarations string sqlstr; try { if (DeleteImageFeatures(DBC)) { if (DBC.IsConnected) { sqlstr = "DELETE FROM Image WHERE ImageID= " + this.id + " ;"; DBC.CommandText = sqlstr; if (DBC.ExecuteNonQuery() > 0) { return(true); } else { throw new Exception("Failed to delete the image !"); } } else { throw new Exception("The database is not connected!"); } } else { throw new Exception("Failed to delete the image !"); } } catch (Exception e) { return(false); } }
/// <summary> /// Inserts the categories of this series /// </summary> /// <param name="DBC">The controller through which categories insertion will be done</param> private bool InsertCats(databaseController DBC) { string sqlstr = ""; if (catList.Count < 1) { return(true); } int inserted_cats = 0; try { if (DBC.IsConnected) { foreach (category c in catList) { sqlstr = "INSERT INTO Series_Cats VALUES ( " + this.id; sqlstr += " , " + c.ID; sqlstr += " );"; DBC.CommandText = sqlstr; if (DBC.ExecuteNonQuery() > 0) { inserted_cats++; } } return(inserted_cats == catList.Count); } else { throw new Exception("The database is not connected!"); } } catch (Exception ex) { return(false); } }
/// <summary> /// Inserts new patiant into the database /// </summary> /// <param name="DBC">The controller through which patiant insertion will be done</param> public bool InsertPatiant(databaseController DBC) { string sqlstr; try { sqlstr = "INSERT INTO Patiant VALUES ( " + this.id + " , '" + this.name + "' , " + this.age + " , '" + this.info + "' );"; if (DBC.IsConnected) { DBC.CommandText = sqlstr; return(DBC.ExecuteNonQuery() > 0); } else { throw new Exception("The database is not connected!"); } } catch (Exception e) { return(false); } }
/// <summary> /// Finds an image that matches a semantic query /// </summary> /// <param name="query">The semantic query</param> /// <param name="DBC">The controller through which the search will be done</param> public List <string[]> SearchBySemantic(string Query, databaseController DBC) { List <string[]> res = new List <string[]>(); return(res); }
/// <summary> /// Finds an image that matches the example image /// </summary> /// <param name="ExampleImagePath">The path of the example image</param> /// <param name="DBC">The controller through which the search will be done</param> public List <string[]> SearchByExample(string ExampleImagePath, databaseController DBC, double Max, double Min) { try { List <string[]> res = new List <string[]>(); //Get the features of the example image featureExtractor fe = new featureExtractor(ExampleImagePath); List <Feature> fv = fe.AllFeatures(); double area = fv[fv.Count - 1].FeatureValue * fv[fv.Count - 2].FeatureValue; double MaxArea = area * Max; double MinArea = area * Min; fv.RemoveAt(fv.Count - 1); fv.RemoveAt(fv.Count - 1); fv.RemoveAt(fv.Count - 1); fv.RemoveAt(fv.Count - 1); //Example color features double[] exCF = new double[4]; for (int i = 0; i < 4; i++) { exCF[i] = fv[i].FeatureValue; } //Example shape features double[] exSF = new double[7]; for (int i = 4; i < 11; i++) { exSF[i - 4] = fv[i].FeatureValue; } //Example texture features double[] exTF = new double[24]; for (int i = 11; i < 35; i++) { exTF[i - 11] = fv[i].FeatureValue; } //Example edge features double[] exEF = new double[37]; for (int i = 35; i < 72; i++) { exEF[i - 35] = fv[i].FeatureValue; } //Calculate similarities between example image and images in the database FeatureExtractorclass fec = new FeatureExtractorclass(); MWArray mwXCF = new MWNumericArray(exCF.Length, 1, exCF); MWArray mwXSF = new MWNumericArray(exSF.Length, 1, exSF); MWArray mwXTF = new MWNumericArray(exTF.Length, 1, exTF); MWArray mwXEF = new MWNumericArray(exEF.Length, 1, exEF); DBC.CommandText = "SELECT * FROM FeaturesVector where (CropH*CropW >=" + MinArea.ToString() + ") AND (CropH*CropW <=" + MaxArea.ToString() + ");"; DataSet ds = DBC.ExecuteQuery(); if (ds.Tables[0].Rows.Count < 1) { throw new Exception("The database is empty or there are no matches!"); } //Calculate similiraties for each group of feature . . . List <Result> ColorSimilarties = new List <Result>(); List <Result> ShapeSimilarties = new List <Result>(); List <Result> TextureSimilarties = new List <Result>(); List <Result> EdgeSimilarties = new List <Result>(); double[] Cfrow; double[] Sfrow; double[] Tfrow; double[] Efrow; MWArray mwRes = new MWNumericArray(1); MWArray mwCC; //Color features for current image MWArray mwCS; //Shape features for current image MWArray mwCT; //Texture features for current image MWArray mwCE; //Edge features for current image for (int j = 0; j < ds.Tables[0].Rows.Count; j++) { int ID = int.Parse(ds.Tables[0].Rows[j].ItemArray[0].ToString()); //Current image color features Cfrow = new double[4]; for (int i = 1; i < 5; i++) { Cfrow[i - 1] = double.Parse(ds.Tables[0].Rows[j].ItemArray[i].ToString()); } mwCC = new MWNumericArray(Cfrow.Length, 1, Cfrow); mwRes = fec.minkowski(mwXCF, mwCC, 4); double Distance = double.Parse(mwRes.ToString()); Result sim = new Result(ID, Distance); ColorSimilarties.Add(sim); //Current image shape features Sfrow = new double[7]; for (int i = 5; i < 12; i++) { Sfrow[i - 5] = double.Parse(ds.Tables[0].Rows[j].ItemArray[i].ToString()); } mwCS = new MWNumericArray(Sfrow.Length, 1, Sfrow); mwRes = fec.minkowski(mwXSF, mwCS, 4); Distance = double.Parse(mwRes.ToString()); sim = new Result(ID, Distance); ShapeSimilarties.Add(sim); //Current image texture features Tfrow = new double[24]; for (int i = 12; i < 36; i++) { Tfrow[i - 12] = double.Parse(ds.Tables[0].Rows[j].ItemArray[i].ToString()); } mwCT = new MWNumericArray(Tfrow.Length, 1, Tfrow); mwRes = fec.minkowski(mwXTF, mwCT, 4); Distance = double.Parse(mwRes.ToString()); sim = new Result(ID, Distance); TextureSimilarties.Add(sim); //Current image edge features Efrow = new double[37]; for (int i = 36; i < 73; i++) { Efrow[i - 36] = double.Parse(ds.Tables[0].Rows[j].ItemArray[i].ToString()); } mwCE = new MWNumericArray(Efrow.Length, 1, Efrow); mwRes = fec.minkowski(mwXEF, mwCE, 4); Distance = double.Parse(mwRes.ToString()); sim = new Result(ID, Distance); EdgeSimilarties.Add(sim); } ShapeSimilarties = sortList(ShapeSimilarties); double resAvg = ListAverag(ShapeSimilarties); while (ShapeSimilarties[ShapeSimilarties.Count - 1].Distance > resAvg) { Result R = ShapeSimilarties[ShapeSimilarties.Count - 1]; ShapeSimilarties.RemoveAt(ShapeSimilarties.Count - 1); for (int r = 0; r < EdgeSimilarties.Count; r++) { if (EdgeSimilarties[r].ID == R.ID) { EdgeSimilarties.RemoveAt(r); TextureSimilarties.RemoveAt(r); ColorSimilarties.RemoveAt(r); break; } } } EdgeSimilarties = sortList(EdgeSimilarties); resAvg = ListAverag(EdgeSimilarties); while (EdgeSimilarties[EdgeSimilarties.Count - 1].Distance > resAvg) { Result R = EdgeSimilarties[EdgeSimilarties.Count - 1]; EdgeSimilarties.RemoveAt(EdgeSimilarties.Count - 1); for (int r = 0; r < TextureSimilarties.Count; r++) { if (TextureSimilarties[r].ID == R.ID) { TextureSimilarties.RemoveAt(r); ColorSimilarties.RemoveAt(r); break; } } } TextureSimilarties = sortList(TextureSimilarties); resAvg = ListAverag(TextureSimilarties); while (TextureSimilarties[TextureSimilarties.Count - 1].Distance > resAvg) { Result R = TextureSimilarties[TextureSimilarties.Count - 1]; TextureSimilarties.RemoveAt(TextureSimilarties.Count - 1); for (int r = 0; r < ColorSimilarties.Count; r++) { if (ColorSimilarties[r].ID == R.ID) { ColorSimilarties.RemoveAt(r); break; } } } ColorSimilarties = sortList(ColorSimilarties); resAvg = ListAverag(ColorSimilarties); while (ColorSimilarties[ColorSimilarties.Count - 1].Distance > resAvg) { ColorSimilarties.RemoveAt(ColorSimilarties.Count - 1); } List <Result> similarties = ColorSimilarties; //Ranking : double farestImg = (double)similarties[similarties.Count - 1].Distance; //The worst match. double rank = 0; string[] resStr; for (int i = 0; i < similarties.Count; i++) { resStr = new string[4]; int x = similarties[i].ID; DBC.CommandText = "SELECT I.FileName, S.FolderPath "; DBC.CommandText += "FROM Image AS I INNER JOIN Series AS S ON FLOOR(I.ImageID / 100) = S.SID "; DBC.CommandText += "WHERE (I.ImageID = " + x + ")"; ds = DBC.ExecuteQuery(); rank = (1 - ((double)similarties[i].Distance / farestImg)) * 100; rank = Math.Round(rank, 5); resStr[0] = ds.Tables[0].Rows[0].ItemArray[0].ToString(); resStr[1] = ds.Tables[0].Rows[0].ItemArray[1].ToString(); resStr[2] = " - " + rank.ToString() + "%"; resStr[3] = x.ToString(); res.Add(resStr); resStr = null; } return(res); } catch (Exception ex) { return(null); } }