Developersland

Teknoloji ve Yazılım Blogu

  • Yazıtipi boyutunu arttır
  • Varsayılan yazıtipi boyutu
  • Yazıtipi boyutunu azaltır

First Tutorial in OpenCV Using OpenCVsharp

Firstly, we need to OpenCVsharp library to use and we will use OpenCV-2.3.0 version(we will use C# as code language). 

1. So download the setup file from this link.

2.Run the file as an administrator and when it is asked where to extract, "Extract to" "C:/Program Files"(it will create a folder OpenCV2.3 in Program Files directory).

3. After extraction finishes, right-click Computer icon, click Properties pop-up window. Click "Advanced system settings" and click "Environment Variables". It will open a new window headered with "System Variables". In "System Variables" part(it's second grid, upper one is "User Variables"), double-click "Path", it will open window headered "Edit System Variable". Add ";C:\Program Files\OpenCV2.3\build\bin\;C:\Program Files\OpenCV2.3\build\x64\vc10\bin\;C:\Program Files\OpenCV2.3\build\x64\vc10\lib;C:\Program Files\OpenCV2.3\build\x64\vc10\staticlib" (without quote marks) to the end of second textbox(labeled "Variable value").


Note: if your computer uses X86, use this string to add ";C:\Program Files\OpenCV2.3\build\bin\;C:\Program Files\OpenCV2.3\build\x86\vc10\bin\;C:\Program Files\OpenCV2.3\build\x86\vc10\lib;C:\Program Files\OpenCV2.3\build\x86\vc10\staticlib"

4. Restart.

Now, it is time to start our first project in OpenCV:

1.Open Visual Studio (I will say acoording the version I'm using-2010).

File-> New Project (or with shortcut CTRL+Shift+N)

2.Select Visual C#, and click WPF Application, name it "myFirstOpenCVProject".

3. From References-> Add reference and browse to "C:\Program Files\OpenCV2.3\build\x86\vc10\bin\" and select all .dll files except that started with OpenCVsharp except for "OpenCVsharpExtern.dll".

4. Add to namespaces this line:

using OpenCvSharp;

5.From toolbox, drag drop button to window and an image also and declare global variable:

 IplImage ipl1;//IplImage means image processing language image


6.Change first button content into "Select Image" and double click it. inside click function add these lines:



Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog(); openFileDialog.Multiselect = false; openFileDialog.Filter = "TIFF (*.tif,*.tiff)|*.tif;*.tiff| JPG (*.jpg,*.jpeg)|*.jpg *.jpeg"; try { if ((bool)openFileDialog.ShowDialog()) { if (openFileDialog.OpenFile() != null) { ipl1 = new IplImage(openFileDialog.FileName, LoadMode.GrayScale); CvSize size = Cv.GetSize(ipl1); BitmapImage bm = new BitmapImage(new Uri(openFileDialog.FileName, UriKind.Absolute)); image1.Source = bm; } } else { System.Windows.MessageBox.Show("The file was not chosen. Try again."); } } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message); }

7.Start without debugging (CTRL+F5) to see if it works.

8. Now drag drop 4 other buttons to the window with contents "show 1st part", "show 2nd part","show 3rd part" and "show 4th part".

9. Double-click "show 1st part" button(button2), and add these lines inside function:




if (ipl1 == null)

            {

                System.Windows.MessageBox.Show("The file was not chosen. Select image firstly.");

                return;

            }

            try

            {

                ipl1.ResetROI();

                CvSize s1 = new CvSize();

                s1.Width=ipl1.Width/2;

                s1.Height=ipl1.Height/2;

                ipl1.SetROI(new OpenCvSharp.CvRect(new OpenCvSharp.CvPoint(0, 0), s1));

                CvSize size = new CvSize(600, 450);

                IplImage tmpsize = Cv.CreateImage(size, ipl1.Depth, 0);

                Cv.Resize(ipl1, tmpsize);

                Cv.ShowImage("1st part", tmpsize);

            }

            catch { }



10. Double-click "show 2nd part" button(button3), and add these lines inside function:




if (ipl1 == null)

            {

                System.Windows.MessageBox.Show("The file was not chosen. Select image firstly.");

                return;

            }

            try

            {

                ipl1.ResetROI();

                CvSize s1 = new CvSize();

                s1.Width=ipl1.Width/2;

                s1.Height=ipl1.Height/2;

                ipl1.SetROI(new OpenCvSharp.CvRect(new OpenCvSharp.CvPoint(ipl1.Width / 2, 0), s1));

                CvSize size = new CvSize(600, 450);

                IplImage tmpsize = Cv.CreateImage(size, ipl1.Depth, 0);

                Cv.Resize(ipl1, tmpsize);

                Cv.ShowImage("2nd part", tmpsize);

            }

            catch { }



11. Double-click "show 3rd part" button(button4), and add these lines inside function:







if (ipl1 == null)

            {

                System.Windows.MessageBox.Show("The file was not chosen. Select image firstly.");

                return;

            }

            try

            {

                ipl1.ResetROI();

                CvSize s1 = new CvSize();

                s1.Width=ipl1.Width/2;

                s1.Height=ipl1.Height/2;

                ipl1.SetROI(new OpenCvSharp.CvRect(new OpenCvSharp.CvPoint(0, ipl1.Height/2), s1));

                CvSize size = new CvSize(600, 450);

                IplImage tmpsize = Cv.CreateImage(size, ipl1.Depth, 0);

                Cv.Resize(ipl1, tmpsize);

                Cv.ShowImage("3rd part", tmpsize);

            }

            catch { }

12.  Double-click "show 4th part" button(button5), and add these lines inside function:

if (ipl1 == null)

            {

                System.Windows.MessageBox.Show("The file was not chosen. Select image firstly.");

                return;

            }

            try

            {

                ipl1.ResetROI();

                CvSize s1 = new CvSize();

                s1.Width=ipl1.Width/2;

                s1.Height=ipl1.Height/2;

                ipl1.SetROI(new OpenCvSharp.CvRect(new OpenCvSharp.CvPoint(ipl1.Weight/2, ipl1.Height/2), s1));

                CvSize size = new CvSize(600, 450);

                IplImage tmpsize = Cv.CreateImage(size, ipl1.Depth, 0);

                Cv.Resize(ipl1, tmpsize);

                Cv.ShowImage("4th part", tmpsize);

            }

            catch { }

 

Yorumlar  

 
+7 #1 Yasho 09-07-2012 07:33
Hi, I have a problem with STEP 3. When I try to add the OpenCV2.3 dlls to WPF, it says that the dlls are not a valid COM file. What should I do?
Alıntı
 

Yorum ekle


Güvenlik kodu
Yenile