개발/Application Develop

2. WPF + OpenCV(dll)

huiyu 2014. 10. 11. 01:48

C++로 구현된 OpenCV를 dll로 파일로 만들어 C#으로 된 WPF에서 사용하는 예제입니다.


1. OpenCV 설치[http://huiyu.tistory.com/45]


2. C++ Win32 Console Application 생성


3. 다음 클릭 후, dll 선택. 완료


4. OpenCV 프로젝트 설정[http://huiyu.tistory.com/45]


5.아래 코드 입력

// ImageProcessAgain.cpp : Defines the exported functions for the DLL application.  
//  
 
#include "stdafx.h"  
 
#include "opencv\cv.h"  
#include "opencv\highgui.h"  
  
extern "C"  
{     
    __declspec(dllexport) int exampleImageProcessing(LPCWSTR);  
}  
  
extern int __cdecl exampleImageProcessing(LPCWSTR filename)  
{  
    IplImage* img = cvLoadImage((char*)filename);  
    return img->width;  
}


6. 빌드한다.

*빌드 시 C4996오류가 날 경우, 프로젝트 속성에서 구성속성->C/C++->전처리기 정의에 

;_CRT_SECURE_NO_WARNINGS 를 추가한다.


7. C# WPF 프로젝트를 생성 후, 프로젝트에 생성된 dll을 복사한다.


8. 복사한 dll파일을 선택 후, 파일 속성에서 '출력 디렉터리로 복사'->'변경된 내용만 복사'로 변경한다.


9. WPF 화면에 텍스트박스와 버튼을 추가한다.


10. 아래의 코드 입력

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Data;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Navigation;  
using System.Windows.Shapes;  
using System.Runtime.InteropServices;  
  
namespace UsingOpencvAgain  
{  
    /// <summary>  
    /// Interaction logic for MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow : Window  
    {  
        public MainWindow()  
        {  
            InitializeComponent();  
        }  
  
        [DllImport("ImageProcessAgain.dll", CallingConvention = CallingConvention.Cdecl)]  
        public static extern int exampleImageProcessing(string filename);  
  
        private void button1_Click(object sender, RoutedEventArgs e)  
        {  
            textBox2.Text = exampleImageProcessing(textBox1.Text).ToString();  
        }  
    }  
}

실행시키고 특정 이미지의 경로를 텍스트박스에 입력하면 그 이미지의 사이지가 출력된다.


728x90
반응형

'개발 > Application Develop' 카테고리의 다른 글

OpenCV 이미지 띄우기  (0) 2014.10.20
C++/CLI 기본 예제  (1) 2014.10.17
1. OpenCV 설치하기  (1) 2014.10.11
MFC 기초  (0) 2014.07.22
Resource 정보 code에서 불러오기  (0) 2014.06.02