Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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();

            //'S' key pressed
            //Save region
            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();
            }

            //'Q' key pressed
            //Next frame
            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();
            }

            //'N' key pressed
            //Negative
            if (e.KeyChar == 110)
            {
                ImgCount++;

                this.ImgFrame.Image.Save(ConfigurationManager.AppSettings["ImgNegatives"].ToString() + "Negative_" + ImgCount + ".jpg");
            }

            //'Escape' key pressed
            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
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [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
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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();
            // 
            // ImgFrame
            // 
            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;
            // 
            // groupBox1
            // 
            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";
            // 
            // label4
            // 
            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";
            // 
            // label3
            // 
            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";
            // 
            // label2
            // 
            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";
            // 
            // label1
            // 
            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";
            // 
            // CropCount
            // 
            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;
            // 
            // Form1
            // 
            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!
Posted
Updated 28-Apr-11 11:53am
v2
Comments
AmarjeetAlien 28-Apr-11 18:25pm    
Just in case If I'm violating something:
The MIT License

Copyright (c) 2010 David J Barnes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Albert Holguin 28-Apr-11 18:33pm    
lol, this is the first time I've seen someone do this...
CodeHawkz 29-Apr-11 0:23am    
:D

1 solution

To complete the installation of OpenCV, you may have to restart Windows.

If not settled by restarting, copy the following files from <OpenCV install
directory>/bin to your VisualStudio project directory (bin/Debug).
cv110.dll
cxcore110.dll
cxts001.dll
ffopencv110.dll
highgui110.dll
ml110.dll
 
Share this answer
 
Comments
AmarjeetAlien 29-Apr-11 6:57am    
Thanks for ur response Rajesh!
Well,I'm using OpenCV2.2 and so I've copied following file(from:OpenCV2.2/bin/) to "bin/debug"
opencv_core220.dll
opencv_core220d.dll
opencv_ffmpeg220.dll
opencv_ffmpeg220d.dll
opencv_highgui220.dll
opencv_highgui220d.dll
opencv_ml220.dll
opencv_ml220d.dll
opencv_video220.dll
opencv_video220d.dll

And all other OpenCvSharp .dll are also present.

Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900