// Constructor public OpticalFlow(String filename, int label) { //fs = new StreamWriter("C:\\Users\\Antivirus\\Desktop\\of\\FeaturesFile.csv", append: true); /*fs.WriteLine("velx_r1," + "vely_r1," + "degrees_r1," + "distance_r1," + "velx_r2," + "vely_r2," + "degrees_r2," + "distance_r2," + "velx_r3," + "vely_r3," + "degrees_r3," + "distance_r3," + "velx_r4," + "vely_r4," + "degrees_r4," + "distance_r4," + "velx_r5," + "vely_r5," + "degrees_r5," + "distance_r5," + "velx_r6," + "vely_r6," + "degrees_r6," + "distance_r6," + "velx_r7," + "vely_r7," + "degrees_r7," + "distance_r7," + "velx_r8," + "vely_r8," + "degrees_r8," + "distance_r8," + "velx_r9," + "vely_r9," + "degrees_r9," + "distance_r9," + "activity"); + fs.Close();*/ class_label = label; fileName = filename; top_left = new Rectangle(); top_middle = new Rectangle(); top_right = new Rectangle(); middle_left = new Rectangle(); middle_middle = new Rectangle(); middle_right = new Rectangle(); bottom_left = new Rectangle(); bottom_middle = new Rectangle(); bottom_right = new Rectangle(); top_left_lines = new List <FeatureVectorOpticalFlow>(); top_middle_lines = new List <FeatureVectorOpticalFlow>(); top_right_lines = new List <FeatureVectorOpticalFlow>(); middle_left_lines = new List <FeatureVectorOpticalFlow>(); middle_middle_lines = new List <FeatureVectorOpticalFlow>(); middle_right_lines = new List <FeatureVectorOpticalFlow>(); bottom_left_lines = new List <FeatureVectorOpticalFlow>(); bottom_middle_lines = new List <FeatureVectorOpticalFlow>(); bottom_right_lines = new List <FeatureVectorOpticalFlow>(); top_left_line = new FeatureVectorOpticalFlow(); top_middle_line = new FeatureVectorOpticalFlow(); top_right_line = new FeatureVectorOpticalFlow(); middle_left_line = new FeatureVectorOpticalFlow(); middle_middle_line = new FeatureVectorOpticalFlow(); middle_right_line = new FeatureVectorOpticalFlow(); bottom_left_line = new FeatureVectorOpticalFlow(); bottom_middle_line = new FeatureVectorOpticalFlow(); bottom_right_line = new FeatureVectorOpticalFlow(); all_lines = new List <FeatureVectorOpticalFlow>(); }
// get the line with maximum length private FeatureVectorOpticalFlow getBigLine(List <FeatureVectorOpticalFlow> lines) { if (lines.Count == 0) { return(new FeatureVectorOpticalFlow()); } FeatureVectorOpticalFlow bigLine = new FeatureVectorOpticalFlow(); bigLine = lines[0]; foreach (FeatureVectorOpticalFlow line in lines) { if (bigLine.line.Length < line.line.Length) { bigLine = line; } } return(bigLine); }
// Calculate Sections, Assign Lines for Each Section and Draw on Image public Image <Hsv, byte> CalculateSections(Image <Hsv, byte> frameImg, int frameNumber = 0) { // resultant Image Image <Hsv, byte> img = new Image <Hsv, byte>(frameImg.Width, frameImg.Height); int w = frameImg.Width; // total width of image int h = frameImg.Height; // total height of image int section_width = w / 3; // width for one section int section_height = h / 3; // height for one section // width and height of all rectangles will be same top_left.Width = top_middle.Width = top_right.Width = section_width; middle_left.Width = middle_middle.Width = middle_right.Width = section_width; bottom_left.Width = bottom_middle.Width = bottom_right.Width = section_width; top_left.Height = top_middle.Height = top_right.Height = section_height; middle_left.Height = middle_middle.Height = middle_right.Height = section_height; bottom_left.Height = bottom_middle.Height = bottom_right.Height = section_height; // only starting point of each section will be different top_left.Y = top_middle.Y = top_right.Y = 0; // same Y position for first row sections middle_left.Y = middle_middle.Y = middle_right.Y = section_height; bottom_left.Y = bottom_middle.Y = bottom_right.Y = 2 * section_height; top_left.X = middle_left.X = bottom_left.X = 0; top_middle.X = middle_middle.X = bottom_middle.X = section_width; top_right.X = middle_right.X = bottom_right.X = 2 * section_width; // drawing the rectangles on the Image CvInvoke.Rectangle(img, top_left, new Bgr(Color.Green).MCvScalar, 2); CvInvoke.Rectangle(img, top_middle, new Bgr(Color.Blue).MCvScalar, 2); CvInvoke.Rectangle(img, top_right, new Bgr(Color.Green).MCvScalar, 2); CvInvoke.Rectangle(img, middle_left, new Bgr(Color.Green).MCvScalar, 2); CvInvoke.Rectangle(img, middle_middle, new Bgr(Color.Blue).MCvScalar, 2); CvInvoke.Rectangle(img, middle_right, new Bgr(Color.Green).MCvScalar, 2); CvInvoke.Rectangle(img, bottom_left, new Bgr(Color.Green).MCvScalar, 2); CvInvoke.Rectangle(img, bottom_middle, new Bgr(Color.Blue).MCvScalar, 2); CvInvoke.Rectangle(img, bottom_right, new Bgr(Color.Green).MCvScalar, 2); // assigning the lines to their respective section list foreach (FeatureVectorOpticalFlow fv in all_lines) { // first row sections if (fv.line.P1.X < section_width && fv.line.P2.X < section_width && fv.line.P1.Y < section_height && fv.line.P2.Y < section_height) { top_left_lines.Add(fv); } else if (fv.line.P1.X < 2 * section_width && fv.line.P2.X < 2 * section_width && fv.line.P1.Y < section_height && fv.line.P2.Y < section_height) { top_middle_lines.Add(fv); } else if (fv.line.P1.X < w && fv.line.P2.X < w && fv.line.P1.Y < section_height && fv.line.P2.Y < section_height) { top_right_lines.Add(fv); } // middle row sections else if (fv.line.P1.X < section_width && fv.line.P2.X < section_width && fv.line.P1.Y < 2 * section_height && fv.line.P2.Y < 2 * section_height) { middle_left_lines.Add(fv); } else if (fv.line.P1.X < 2 * section_width && fv.line.P2.X < 2 * section_width && fv.line.P1.Y < 2 * section_height && fv.line.P2.Y < 2 * section_height) { middle_middle_lines.Add(fv); } else if (fv.line.P1.X < w && fv.line.P2.X < w && fv.line.P1.Y < 2 * section_height && fv.line.P2.Y < 2 * section_height) { middle_right_lines.Add(fv); } // third row sections else if (fv.line.P1.X < section_width && fv.line.P2.X < section_width && fv.line.P1.Y < h && fv.line.P2.Y < h) { bottom_left_lines.Add(fv); } else if (fv.line.P1.X < 2 * section_width && fv.line.P2.X < 2 * section_width && fv.line.P1.Y < h && fv.line.P2.Y < h) { bottom_middle_lines.Add(fv); } else if (fv.line.P1.X < w && fv.line.P2.X < w && fv.line.P1.Y < h && fv.line.P2.Y < h) { bottom_right_lines.Add(fv); } } // get line with maximum Length from each section list of lines // And Draw the line on the image if (top_left_lines.Count > 0) { top_left_line = getBigLine(top_left_lines); CvInvoke.ArrowedLine(img, top_left_line.line.P1, top_left_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } if (top_middle_lines.Count > 0) { top_middle_line = getBigLine(top_middle_lines); CvInvoke.ArrowedLine(img, top_middle_line.line.P1, top_middle_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } if (top_right_lines.Count > 0) { top_right_line = getBigLine(top_right_lines); CvInvoke.ArrowedLine(img, top_right_line.line.P1, top_right_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } if (middle_left_lines.Count > 0) { middle_left_line = getBigLine(middle_left_lines); CvInvoke.ArrowedLine(img, middle_left_line.line.P1, middle_left_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } if (middle_middle_lines.Count > 0) { middle_middle_line = getBigLine(middle_middle_lines); CvInvoke.ArrowedLine(img, middle_middle_line.line.P1, middle_middle_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } if (middle_right_lines.Count > 0) { middle_right_line = getBigLine(middle_right_lines); CvInvoke.ArrowedLine(img, middle_right_line.line.P1, middle_right_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } if (bottom_left_lines.Count > 0) { bottom_left_line = getBigLine(bottom_left_lines); CvInvoke.ArrowedLine(img, bottom_left_line.line.P1, bottom_left_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } if (bottom_middle_lines.Count > 0) { bottom_middle_line = getBigLine(bottom_middle_lines); CvInvoke.ArrowedLine(img, bottom_middle_line.line.P1, bottom_middle_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } if (bottom_right_lines.Count > 0) { bottom_right_line = getBigLine(bottom_right_lines); CvInvoke.ArrowedLine(img, bottom_right_line.line.P1, bottom_right_line.line.P2, new Bgr(Color.White).MCvScalar, 1); } //WriteFeatureToCSV(); //CvInvoke.NamedWindow("Motion Tracking"); // CvInvoke.Imshow("Motion Tracking", img.Resize(200,200,Inter.Cubic)); //CvInvoke.Imwrite("C:\\Users\\Antivirus\\Desktop\\of\\Frames.png",img); //CvInvoke.Imwrite("C:\\Users\\Antivirus\\Desktop\\of\\opticalflow" + (frameNumber - 1) + "-" + (frameNumber) + ".png", img); return(img); }