/** * Extract themes from images in the image directory */ private void extractTheme_Click(object sender, EventArgs e) { if (backgroundWorker.IsBusy) { statusBox.Text = "Please wait..."; return; } backgroundWorker = MakeBackgroundWorker(delegate { backgroundWorker.ReportProgress(0, "Extracting Themes from Image Dir"); //Use the Palette Extractor to extract the theme PaletteExtractor extractor = new PaletteExtractor(dir, weightsDir, json); String[] files = GetImageFiles(dir); String outfile = Path.Combine(dir, "optimized.tsv"); String headers = "pid\tid\timage\tcolors\tnumColors\tlog\n"; File.WriteAllText(outfile, ""); File.AppendAllText(outfile, headers); int count = 0; foreach (String f in files) { count++; String basename = new FileInfo(f).Name; //The saliency pattern "_Judd" is just an additional annotation after the image filename if it exists //i.e. if the image filename is A.png, the saliency map filename is A_Judd.png PaletteData data = extractor.HillClimbPalette(basename, "_Judd", debug); //save to file String colorString = data.ToString(); File.AppendAllText(outfile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n"); backgroundWorker.ReportProgress(100 * count / files.Count(),"Finished " + f); } }); RunWorker(); }
private void extractTheme_Click(object sender, EventArgs e) { //Use the Palette Extractor to extract the theme PaletteExtractor extractor = new PaletteExtractor(dir,weightsDir,json); String[] files = Directory.GetFiles(dir, "*.png"); String outfile = dir+"\\palettes.tsv"; String headers = "pid\tid\timage\tcolors\tnumColors\tlog\n"; File.WriteAllText(outfile, ""); File.AppendAllText(outfile, headers); int count = 0; foreach (String f in files) { count++; String basename = f.Replace(dir+"\\", ""); //The saliency pattern "_Judd" is just an additional annotation after the image filename if it exists //i.e. if the image filename is A.png, the saliency map filename is A_Judd.png PaletteData data = extractor.HillClimbPalette(basename, "_Judd", true); //save to file String colorString = data.ToString(); File.AppendAllText(outfile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n"); } }
/** * Extract k-means, c-means, model, and oracle themes for comparison */ private void extractThemesToCompare_Click(object sender, EventArgs e) { if (backgroundWorker.IsBusy) { statusBox.Text = "Please wait..."; return; } backgroundWorker.WorkerReportsProgress = true; backgroundWorker = MakeBackgroundWorker(delegate { backgroundWorker.ReportProgress(0, "Extracting themes to compare..."); PaletteExtractor extractor = new PaletteExtractor(evalDir, weightsDir, json); Dictionary<String, Dictionary<String, List<PaletteData>>> referenceP = ItemsToDictionary(reference); String[] files = GetImageFiles(evalDir); String headers = "pid\tid\timage\tcolors\tnumColors\tlog\n"; backgroundWorker.ReportProgress(0, "Extracting random themes..."); //extract random String randomFile = Path.Combine(evalDir, "random.tsv"); if (!File.Exists(randomFile) || File.ReadLines(randomFile).Count() != (40 * files.Count()) + 1) { int count = 0; File.WriteAllText(randomFile, ""); File.AppendAllText(randomFile, headers); foreach (String f in files) { String basename = new FileInfo(f).Name; PaletteData swatches = extractor.GetPaletteSwatches(basename); List<PaletteData> random = GenerateRandomPalettesNonBinned(40, swatches); foreach (PaletteData data in random) { count++; String colorString = data.ToString(); File.AppendAllText(randomFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n"); } } } else { Console.WriteLine("Random palettes file already exists"); backgroundWorker.ReportProgress(100, "Random themes file already exist...skipping"); Thread.Sleep(1000); } backgroundWorker.ReportProgress(0, "Extracting K-means themes..."); //extract kmeans String kmeansFile = Path.Combine(evalDir, "kmeans.tsv"); if (!File.Exists(kmeansFile) || File.ReadLines(kmeansFile).Count() != files.Count() + 1) { File.WriteAllText(kmeansFile, ""); File.AppendAllText(kmeansFile, headers); int count = 0; foreach (String f in files) { count++; String basename = new FileInfo(f).Name; PaletteData data = KMeansPalette(f); //save to file String colorString = data.ToString(); File.AppendAllText(kmeansFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n"); backgroundWorker.ReportProgress(100 * count / files.Count(), ""); } } else { Console.WriteLine("Kmeans palettes file already exists"); backgroundWorker.ReportProgress(100, "K-means themes already exist...skipping"); Thread.Sleep(1000); } //extract cmeans backgroundWorker.ReportProgress(0, "Extracting C-means themes..."); String cmeansFile = Path.Combine(evalDir, "cmeans.tsv"); if (!File.Exists(cmeansFile) || File.ReadLines(cmeansFile).Count() != files.Count() + 1) { File.WriteAllText(cmeansFile, ""); File.AppendAllText(cmeansFile, headers); int count = 0; foreach (String f in files) { count++; String basename = new FileInfo(f).Name; PaletteData data = CMeansPalette(f); //save to file String colorString = data.ToString(); File.AppendAllText(cmeansFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n"); backgroundWorker.ReportProgress(100 * count / files.Count(), ""); } } else { Console.WriteLine("Cmeans palettes file already exists"); backgroundWorker.ReportProgress(100, "C-means themes already exist...skipping"); Thread.Sleep(1000); } //extract model backgroundWorker.ReportProgress(0, "Extracting Optimized themes..."); String modelFile = Path.Combine(evalDir, "optimized.tsv"); if (!File.Exists(modelFile) || File.ReadLines(modelFile).Count() != files.Count() + 1) { File.WriteAllText(modelFile, ""); File.AppendAllText(modelFile, headers); int count = 0; foreach (String f in files) { count++; String basename = new FileInfo(f).Name; //The saliency pattern "_Judd" is just an additional annotation after the image filename if it exists //i.e. if the image filename is A.png, the saliency map filename is A_Judd.png PaletteData data = extractor.HillClimbPalette(basename, "_Judd", debug); //save to file String colorString = data.ToString(); File.AppendAllText(modelFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n"); backgroundWorker.ReportProgress(100 * count / files.Count(), ""); } } else { Console.WriteLine("Optimized palettes file already exists"); backgroundWorker.ReportProgress(100, "Optimized themes already exist...skipping"); Thread.Sleep(1000); } //extract oracle foreach (String name in referenceP.Keys) { backgroundWorker.ReportProgress(0, "Extracting Oracle themes for..." + name); String oracleFile = Path.Combine(evalDir, "oracle-" + name + ".tsv"); if (!File.Exists(oracleFile) || File.ReadLines(oracleFile).Count() != referenceP[name].Keys.Count() + 1) { File.WriteAllText(oracleFile, ""); File.AppendAllText(oracleFile, headers); int count = 0; foreach (String f in files) { String basename = new FileInfo(f).Name; if (!referenceP[name].ContainsKey(basename)) continue; count++; PaletteData data = GetOracleTheme(basename, referenceP[name]); //save to file String colorString = data.ToString(); File.AppendAllText(oracleFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n"); backgroundWorker.ReportProgress(100 * count / files.Count(), ""); } } else { Console.WriteLine("Oracle palettes file already exists"); backgroundWorker.ReportProgress(100, "Oracle themes already exist...skipping"); Thread.Sleep(1000); } } }); RunWorker(); }