Hi Coders!
I'm trying to make "positive image builder for haar cascade" in VSC#2010(it has not been updated by its developer at googlecode). After going through few work-around I was able to successfully build solution. But when I hit F5, it throws an exception:
An unhandled exception of type 'System.AccessViolationException' occurred in OpenCvSharp.dll
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
and "green arrow" points to line(in Form1.cs):
private CvCapture FrameCapture = Cv.CreateFileCapture(ConfigurationManager.AppSettings["VideoFile"].ToString());
Contents of project:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using OpenCvSharp;
using OpenCvSharp.UserInterface;
namespace MyPositiveImageBuilder
{
public partial class Form1 : Form
{
private CvCapture FrameCapture = Cv.CreateFileCapture(ConfigurationManager.AppSettings["VideoFile"].ToString());
private IplImage Frame, FrameBak, FrameSave;
private bool FirstClick = true;
private int x1, x2, y1, y2, width, height, ImgCount = Convert.ToInt32(ConfigurationManager.AppSettings["ImgIndexBegin"]);
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyPress += new KeyPressEventHandler(Form_KeyPress);
this.ImgFrame.MouseClick += new MouseEventHandler(Frame_DrawCrop);
this.ImgFrame.MouseMove += new MouseEventHandler(Frame_MouseMove);
}
private void Form1_Load(object sender, EventArgs e)
{
Frame = FrameCapture.QueryFrame();
FrameBak = Cv.CreateImage(Cv.GetSize(Frame), Frame.Depth, Frame.NChannels);
FrameSave = Cv.CreateImage(Cv.GetSize(Frame), Frame.Depth, Frame.NChannels);
Cv.Copy(Frame, FrameBak, null);
Cv.Copy(Frame, FrameSave, null);
this.ImgFrame.Image = Frame.ToBitmap();
}
private void WritePositive(string WriteData)
{
StreamWriter sw = File.AppendText(ConfigurationManager.AppSettings["PositivesFile"].ToString());
sw.WriteLine(WriteData);
sw.Close();
}
void Form_KeyPress(object sender, KeyPressEventArgs e)
{
string KeyPressed = e.KeyChar.ToString().ToLower();
if ((e.KeyChar == 115) && (width > 0))
{
string SaveImageAs = ConfigurationManager.AppSettings["ImgDirectory"].ToString() + ConfigurationManager.AppSettings["ImgNamePrefix"].ToString() + "_" + ImgCount + ".jpg";
FrameSave.SaveImage(SaveImageAs);
WritePositive(SaveImageAs + " 1 " + x1 + " " + y1 + " " + width + " " + height);
ImgCount++;
this.CropCount.Text = "Images saved: " + ImgCount.ToString();
Frame = FrameCapture.QueryFrame();
this.ImgFrame.Image = Frame.ToBitmap();
}
if (e.KeyChar == 113)
{
Frame = FrameCapture.QueryFrame();
FrameBak = Cv.CreateImage(Cv.GetSize(Frame), Frame.Depth, Frame.NChannels);
Cv.Copy(Frame, FrameBak, null);
Cv.Copy(Frame, FrameSave, null);
this.ImgFrame.Image = Frame.ToBitmap();
}
if (e.KeyChar == 110)
{
ImgCount++;
this.ImgFrame.Image.Save(ConfigurationManager.AppSettings["ImgNegatives"].ToString() + "Negative_" + ImgCount + ".jpg");
}
if (e.KeyChar == 27)
{
this.Close();
}
}
void Frame_DrawCrop(object sender, MouseEventArgs e)
{
if (FirstClick)
{
x1 = e.X;
y1 = e.Y;
Cv.Copy(Frame, FrameBak, null);
FirstClick = false;
}
else
{
x2 = e.X;
y2 = e.Y;
width = x2 - x1;
height = y2 - y1;
if ((x2 > x1) && (y2 > y1))
{
Cv.Copy(Frame, FrameBak, null);
this.ImgFrame.Image = Frame.ToBitmap();
FirstClick = true;
}
else
{
FirstClick = true;
MessageBox.Show("Please start in the upper left corner. Proceed to the lower left corner.");
this.ImgFrame.Image = FrameBak.ToBitmap();
}
}
}
void Frame_MouseMove(object sender, MouseEventArgs e)
{
Cv.Copy(FrameBak, Frame,null);
Cv.DrawLine(Frame, new CvPoint(0, e.Y), new CvPoint(Frame.Width, e.Y), new CvColor(0, 255, 0));
Cv.DrawLine(Frame, new CvPoint(e.X, 0), new CvPoint(e.X, Frame.Height), new CvColor(0, 255, 0));
this.ImgFrame.Image = Frame.ToBitmap();
}
}
}
Program.cs:
<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MyPositiveImageBuilder
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
App.config:
<<pre lang="xml">?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="VideoFile" value="E:\HaarTraining\pupil.avi"/>
<add key="PositivesFile" value="E:\HaarTraining\Positives.txt" />
<add key="ImgDirectory" value="E:\HaarTraining\Images\Positives\" />
<add key="ImgNamePrefix" value="Pupil"/>
<add key="ImgIndexBegin" value="00"/>
<add key="ImgNegatives" value="E:\"/>
</appSettings>
</configuration
>
Form1.Designer.cs:
namespace MyPositiveImageBuilder
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.ImgFrame = new System.Windows.Forms.PictureBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.CropCount = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.ImgFrame)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
this.ImgFrame.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.ImgFrame.Cursor = System.Windows.Forms.Cursors.Cross;
this.ImgFrame.Location = new System.Drawing.Point(12, 12);
this.ImgFrame.Name = "ImgFrame";
this.ImgFrame.Size = new System.Drawing.Size(320, 240);
this.ImgFrame.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.ImgFrame.TabIndex = 2;
this.ImgFrame.TabStop = false;
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(732, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(190, 153);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Directions";
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(15, 127);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Negative: N";
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(15, 92);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(70, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Save Crop: S";
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(15, 60);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(75, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Next Frame: Q";
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(15, 28);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Quit: ESC";
this.CropCount.AutoSize = true;
this.CropCount.Location = new System.Drawing.Point(747, 181);
this.CropCount.Name = "CropCount";
this.CropCount.Size = new System.Drawing.Size(0, 13);
this.CropCount.TabIndex = 4;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(934, 512);
this.Controls.Add(this.CropCount);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.ImgFrame);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.ImgFrame)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox ImgFrame;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label CropCount;
}
}
I'm not that good at programming to resolve this memory access.
Please help.
pupil.avi is present at "E:\HaarTraining\pupil.avi" and all other file are also present at specified locations as in "App.config".
Thanks in advance for yoour help!