Friday, 27 August 2010

Using a dylib file in XCode

main.dylib is the exported library. shownow is the exported method in main.dylib.

int main (int argc, const char * argv[]) {
char* lib_name = "./main.dylib";
void* lib_handle = dlopen(lib_name, RTLD_NOW);
if(lib_handle)
{
void (*shownow)(void) = dlsym(lib_handle, "shownow");
if(shownow)
{
shownow();
}
}
return 0;
}

Creating a dylib file in XCode

!--main.c file

#define EXPORT __attribute__((visibility("default")))



__attribute__((constructor))
static void initializer(void)
{
printf("%s",__FILE__);
}
__attribute__((destructor))
static void finalizer(void)
{
printf("%s",__FILE__);
}

EXPORT

void shownow (void) {
// insert code here...
printf("Hello, World!\n");
}

Use EXPORT before any funtion that you would like to export in dylib file. After you have written the file. You can create a dylib in terminal by typing the command below.
Locate the main.c file and type


gcc -dynamiclib -std=gnu99 "FileName" -current_version 1.0 -compatibility_version 1.0 -fvisibility=hidden -o "LibFileName"

Example:
"FileName" - main.c
"LibFileName"- main.dylib

C++ code to make your application a trial version

{
time_t rawtime, end;
struct tm * endinfo;
double dif;
time ( &rawtime );
endinfo = gmtime ( &rawtime );
endinfo->tm_year = year-1900;
endinfo->tm_mon = Month-1;
endinfo->tm_mday = Day;
end = mktime ( endinfo );
dif = difftime(end, rawtime);
if (dif less than 0)
printf("\nTrial Version Expired \n");
}
Year, Month and Day are all integers. Enter your Date on which you want the application to be expired.

OpenCV setup in MAC OSX

Install macports - > http://www.macports.org/install.php

Open terminal type sudo port -v selfupdate

type sudo port install opencv

Once the installation is successful. Include OpenCV in XCode by following the steps below.

These instructions were written for Xcode 3.1.x
Create a new XCode project using the Command Line Utility/Standard Tool template
Select Project -> Edit Project Settings
Select the Build tab
Set Configuration to All Configurations
In the Architectures section, double-click Valid Architectures and remove all the PPC architectures
In the Search Paths section set Header Search Paths to /usr/local/include/opencv
Close the Project Info window
Select Project -> New Group and create a group called OpenCV Frameworks
With the new group selected, select Project -> Add to Project…
Press the "/" key to get the Go to the folder prompt
Enter /usr/local/lib
Select libcxcore.dylib, libcvaux.dylib, libcv.dylib, libhighgui.dylib, and libml.dylib.
Click Add
Uncheck Copy Items… and click Add
Now you should be able to include the OpenCV libraries, compile, and run your project

Wednesday, 7 July 2010

opencv installation and setup in MS visual studio


Installing OpenCV and Compiling OpenCV programs in VC++ express edition
1. Download the OpenCV installer OpenCV_1.1pre1a.exe from:
http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16937&release_id=633499
You can also Google “OpenCV”, the last version available is 1.1pre1
2. Just run OpenCV_1.1pre1a.exe and reset your computer
Check the check box “Add <…> \OpenCV\bin to the system PATH”
3. Verify if OpenCV was correctly installed running some examples from
C:\Program Files\OpenCV\samples\c
Try to run contours.exe, you will see something like this
If you get some error with “cxcore110.dll was not found” is because you didn’t reset your computer or the
PATH was incorrectly set. Try reinstalling OpenCV.
If you have already installed Visual C++ express edition (or similar Microsoft SDK) skip step 4.
4. Download VC++ express edition from http://www.microsoft.com/express/vc/
Install it as indicate in that page.
5. Setup VC++ to compile OpenCV programs
Open VC++ and go to Tools->Options, select “VC++ Directories” and choose include files
VLL ‐ March 2009 | Geovany A. Ramirez 2
Add the directories in the list box that are surrounded by a red line in the follow image
Now choose Library files and add “C:\Program Files\OpenCV\lib” as shown in the following image
VLL ‐ March 2009 | Geovany A. Ramirez 3
Now choose Source files and add the directories that are surrounded by a red line in the following image
Now just click OK to close the dialog.
6. You will need new .dll files for debugging, therefore you have to compile the OpenCV VC++ project. In VC++
select File->open->Project/Solution and open “C:\Program Files\OpenCV\_make\opencv.vs2005.no_openmp.sln”
and follow the instruction.
Then just select Build->Build Solution to compile the entire project. In the output window you will see something
like the following image
Now we are ready to compile OpenCV programs
7. Open a new instance of VC++ and select File->New->Project, choose Win32 and make sure that Win32 Console
Application is selected. For testing we will create a project called “show_image” that just will display and image.
Write the name and click OK, after that click Finish.
VLL ‐ March 2009 | Geovany A. Ramirez 4
Copy and paste the following code in show_image.cpp
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *image = 0;
image = cvLoadImage( "C:/Program Files/OpenCV/samples/c/fruits.jpg", 1 );
if( image )
{
cvNamedWindow( "Show", 1 );
cvShowImage( "Show", image );
printf( "Press any key to exit\n");
cvWaitKey(0);
cvDestroyWindow("result");
}
else
fprintf( stderr, "Error reading image\n" );
return 0;
}
NOTE: All your programs must include the #include "stdafx.h" in the fist line.
If you try to build the above code you will get some linking errors like
VLL ‐ March 2009 | Geovany A. Ramirez 5
For each program you have add the dependencies to VC++. Select Project->”program name” Properties and go to
Configuration Properties->Linker->Input choose Additional Dependencies and add
cvd.lib
cxcored.lib
highguid.lib
cvauxd.lib
Click Ok and build again the program using Build->Build Solution (F7). Then Run the program using the play
button (F5). You will get something like this
VLL ‐ March 2009 | Geovany A. Ramirez 6
8. You can get some help about basic data structures and basic function in
http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_BasicFuncs.htm
http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_ImageProcessing.htm
Also check the OpenCV documentation in C:/Program Files/OpenCV/docs/index.htm

Thursday, 24 June 2010

Common DLL errors

System.DllNotFoundException: Unable to load DLL 'IVL_GR.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Make sure if you have the DLL in the specified location in the application.


How to create a C# form application and use a DLL

1. Open Visual Studio
2. File->New Project ->Other Languages -> C# -> Windows Application
3. Create your GUI using the toolbox and double click the form to view its code.
4. Add using System.Runtime.InteropServices; in the header
5. Import your DLL using
[DllImport("DLL Name")] in the C# class
above it constructor definition.
3. Declare the exported function(s) of the DLL with keywords public static extern
4. Call the exported function anywhere in the program in the usual manner.
Example:
using System;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
[DllImport("cppfile.dll")] // the whole path has to be inserted within the quotes
public static extern int abc(); // this should be the function we exported when we created //the DLL

}
call the function abc() anywhere in the usual way you handle a function call.
5. click F5 to run the application.

How to create DLL from CPP file in MS Visual Studio

The process of creating a DLL from a CPP file using visual studio is easy and it requires the following steps.

1. Open visual studio. Go to File->new project -> Visual c++ -> win32 project
2. Give a project name and click ok.
3. Click Application settings and select DLL and click finish.
4. Open the cpp file attached to the project and append your main cpp file to it and add the other source files and header files to the project.
5. Change the name of the main() function in your CPP file to a name you want to use later on while calling the DLL file.
6. If you are using command line arguments and you want to have arguments to be passed to the main() function, load the input pararmeters for the function.
3. Use extern "C" _declspec (dllexport) before the specification of return type of the function.

Example:
Intitially:

int main()
{
//code
}

After following the 3 steps:
extern "C" _declspec (dllexport) int abc(optional input parameters)
{
//code
}
4. Clean up the solution and build it in release mode. You will find your DLL file in the name of the project name in Release folder.