public static void CropAndNormalizeObjects() { List <PSM4TxSample> samples = LoadSamples(@"\users\jie\projects\Intel\data\PSM4-Tx\20160722\Original"); for (int i = 0; i < samples.Count; i++) { PSM4TxSample sample = samples[i]; Emgu.CV.Image <Gray, byte> image = new Emgu.CV.Image <Gray, byte>(sample.imageFile); //{ //Aperture // //Extends in X direction to make width 240 pixels // double diff_x = APERTURE_ORIGINAL_WIDTH - sample.apertureW; // //Extends in Y direction to make 2000 pixels // double diff_y = APERTURE_ORIGINAL_HEIGHT - sample.apertureH; // Rectangle rect = new Rectangle((int)(sample.apertureX - diff_x / 2 + 0.5), (int)(sample.apertureY - diff_y / 2 + 0.5), APERTURE_ORIGINAL_WIDTH, APERTURE_ORIGINAL_HEIGHT); // image.ROI = rect; // Emgu.CV.Image<Gray, byte> normalized = image.Resize(APERTURE_HOG_WIDTH, APERTURE_HOG_HEIGHT, Inter.Linear); // string cropped_file = sample.imageFile.Replace("original", "Aperture"); // normalized.Save(cropped_file); //} //{ //arrayblock // //Extends in X direction to make width 1200 pixels // double diff_x = ARRAYBLOCK_ORIGINAL_WIDTH - sample.arrayblockW; // //Extends in Y direction to make 1600 pixels // double diff_y = ARRAYBLOCK_ORIGINAL_HEIGHT - sample.arrayblockH; // Rectangle rect = new Rectangle((int)(sample.arrayblockX - diff_x / 2 + 0.5), (int)(sample.arrayblockY - diff_y / 2 + 0.5), ARRAYBLOCK_ORIGINAL_WIDTH, ARRAYBLOCK_ORIGINAL_HEIGHT); // image.ROI = rect; // Emgu.CV.Image<Gray, byte> normalized = image.Resize(ARRAYBLOCK_HOG_WIDTH, ARRAYBLOCK_HOG_HEIGHT, Inter.Linear); // string cropped_file = sample.imageFile.Replace("original", "Arrayblock"); // normalized.Save(cropped_file); //} { //Isolator //Extends in X direction to make width 1000 pixels double diff_x = ISOLATOR_ORIGINAL_WIDTH - sample.isolatorW; //Extends in Y direction to make 1600 pixels double diff_y = ISOLATOR_ORIGINAL_HEIGHT - sample.isolatorH; Rectangle rect = new Rectangle((int)(sample.isolatorX - diff_x / 2 + 0.5), (int)(sample.isolatorY - diff_y / 2 + 0.5), ISOLATOR_ORIGINAL_WIDTH, ISOLATOR_ORIGINAL_HEIGHT); image.ROI = rect; Emgu.CV.Image <Gray, byte> normalized = image.Resize(ISOLATOR_HOG_WIDTH, ISOLATOR_HOG_HEIGHT, Inter.Linear); string cropped_file = sample.imageFile.Replace("original", "Isolator"); normalized.Save(cropped_file); } } }
public static void NegativeIsolatorPatches() { List <PSM4TxSample> samples = LoadSamples(@"\users\jie\projects\Intel\data\PSM4-Tx\20160722\Original"); for (int i = 0; i < samples.Count; i++) { PSM4TxSample sample = samples[i]; Emgu.CV.Image <Gray, byte> image = new Emgu.CV.Image <Gray, byte>(sample.imageFile); int image_width = image.Width, image_height = image.Height; Random rand = new Random(); for (int k = 0; k < 2; k++) { int x, y, w = ISOLATOR_ORIGINAL_WIDTH, h = ISOLATOR_ORIGINAL_HEIGHT; x = rand.Next(0, image_width); y = rand.Next(0, image_height); if (x + w >= image_width || y + h >= image_height) { k--; continue; } Rectangle rect = new Rectangle(x, y, w, h); image.ROI = rect; Emgu.CV.Image <Gray, byte> neg_image = image.Resize(ISOLATOR_HOG_WIDTH, ISOLATOR_HOG_HEIGHT, Inter.Linear); string negative_image_file = sample.imageFile.Replace("original", @"IsolatorNegative"); negative_image_file = negative_image_file.Insert(negative_image_file.LastIndexOf('.'), "." + k.ToString()); string folder = Path.GetDirectoryName(negative_image_file); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } neg_image.Save(negative_image_file); } } }
//static int nLevels = 64; //static Size testPadding = new Size(32, 32); //static double hitThreshold = 0; //static int groupThreshold = 2; //static double scaleStep = 1.05; //static bool useMeanShiftGrouping = false; public static List <PSM4TxSample> LoadSamples(string folder) { //Load all samples from all the folders List <PSM4TxSample> samples = new List <PSM4TxSample>(); List <string> missing_xml_samples = new List <string>(); string[] files = Directory.EnumerateFiles(folder) .Where(file => file.ToLower().EndsWith(".bmp") || file.ToLower().EndsWith(".jpg")) .ToArray(); for (int k = 0; k < files.Length; k++) { string img_file = files[k].ToLower(); string xml_file = img_file.EndsWith("bmp") ? img_file.Replace(".bmp", "_data.xml") : img_file.Replace(".jpg", "_data.xml"); if (!File.Exists(xml_file)) { missing_xml_samples.Add(img_file); continue; } PSM4TxSample sample = new PSM4TxSample(img_file); // .// Means descendants, which includes children of children (and so forth). // ./ Means direct children. //If a XPath starts with a / it becomes relative to the root of the document; //to make it relative to your own node start it with ./. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.Load(xml_file); HtmlAgilityPack.HtmlNodeCollection shape_nodes = doc.DocumentNode.SelectNodes("//shape"); foreach (HtmlAgilityPack.HtmlNode shape_node in shape_nodes) { HtmlAgilityPack.HtmlNode blocktext_node = shape_node.SelectSingleNode(".//blocktext"); HtmlAgilityPack.HtmlNode text_node = blocktext_node.SelectSingleNode("./text"); HtmlAgilityPack.HtmlNode data_node = shape_node.SelectSingleNode(".//data"); HtmlAgilityPack.HtmlNode extent_node = data_node.SelectSingleNode("./extent"); string s_x = extent_node.GetAttributeValue("X", ""); string s_y = extent_node.GetAttributeValue("Y", ""); string s_w = extent_node.GetAttributeValue("Width", ""); string s_h = extent_node.GetAttributeValue("Height", ""); if (text_node.InnerText.ToLower() == "isolator") { sample.SetIsolator(s_x, s_y, s_w, s_h); //(787.18896484375, 1370.0) } else if (text_node.InnerText.ToLower() == "arrayblock") { sample.SetArrayBlock(s_x, s_y, s_w, s_h); //(1052.86828613281, 1201.8359375) } else if (text_node.InnerText.ToLower() == "aperture") { sample.SetAperture(s_x, s_y, s_w, s_h); //(209.156982421875, 1885.03271484375) } } samples.Add(sample); } using (StreamWriter sw = new StreamWriter("missing_xml.txt")) { for (int i = 0; i < missing_xml_samples.Count; i++) { sw.WriteLine(missing_xml_samples[i]); } } return(samples); }