Category Archives: Visual Studio

WORKING WITH OPENCV IN WINDOWS

Hello Friends,

Till today I was working with OpenCV on ubuntu platform. But we should also have knowledge about using the OpenCV libraries with Visual studio in Windows. Here I will be explaining how to integrate OpenCV with Windows 32-bit and 64-bit versions.

Step-I:

Download OpenCV from here: http://opencv.org/downloads.html

Double click on the downloaded exe file and when it is is being extracted select the folder name as “opencv” and extract it in your C drive.

Step-II:

Now we need to add the path of the extracted libraries to Environment variables.
Go to “Control Panel” >> “System and Security” >> “System” and click on “advanced system properties” and a window will appear(the one inside the red rectangle):
Screenshot (18)

Now click on the “environment variable”, in the “system variables” select “Path” and edit it by adding these lines to it:
64-Bit users
C:\opencv\build\x64\vc10\bin;C:\opencv\build\common\tbb\intel64\vc10;

32-Bit users
C:\opencv\build\x86\vc10\bin;

Assuming that you are using Visual Studio 2010
You are basically adding path to bin in the environment variables, so make sure the path is correct and make appropriate changes if necessary.

Click on “OK” in every window that has been opened to make the changes.

Step-III:
Open Visual studio 2010 and create a new visual c++ win32 console application project. Name it something and create it.

Now in that select the “View” menu and click on “Property Manager”.

Step-IV:
Only for 64-Bit version users.

Select the “Project” Menu and click on “Properties”.
Screenshot (19)

Click on the “Configuration Manager”
Select the “Active Solution Configuration” as “Release”
Select the “Active Solution Platform” and click on “”
Screenshot (20)

And in that Select the new platform as “X64”, click on “ok” and close the configuration manager.

Step-V:
Now in the properties window,
Select the “Configuration Properties” >> “C/C++” >> “General” and in that edit the “Additional Include libraries” by adding these two lines to it:
Screenshot (21)

C:\opencv\build\include\opencv;C:\opencv\build\include
Here we are adding path to include folders,make sure the path is correct as per your computer.

Select the “Configuration Properties” >> “C/C++” >> “Preprocessor” and in that select the “Preprocessor definition”,edit it by adding this to it :
_CRT_SECURE_NO_WARNINGS

Step-VI:
Now in the properties window,
Select the “Configuration Properties” >> “Linker” >> “General” and select and edit the “Additional Library Directories”
Screenshot (22)

64-Bit version users add this line to it:
C:\opencv\build\x64\vc10\lib;

32-Bit version users add this line to it:
C:\opencv\build\x86\vc10\lib;

Make sure the path to lib is correct as per your settings.

Select the “Configuration Properties” >> “Linker” >> “Input” and click on “Additional dependencies” and edit it:
Screenshot (23)

opencv_core246.lib
opencv_imgproc246.lib
opencv_highgui246.lib
opencv_ml246.lib
opencv_video246.lib
opencv_features2d246.lib
opencv_calib3d246.lib
opencv_objdetect246.lib
opencv_contrib246.lib
opencv_legacy246.lib
opencv_flann246.lib

Note: In …..246.lib,246 is the version of opencv,for me its OpenCV-2.4.6 ,So make appropriate changes according to the version you have downloaded.

Click on “apply” and “ok”

Now in the code DELETE everything and copy the test code from here: https://github.com/abhi-kumar/OPENCV_MISC/blob/master/tracker.cpp
Note:on the top of the code add this line: #include “stdafx.h”

Keep the mode as release and run it:
Screenshot (24)

Get the details of the code from : https://abhishek4273.wordpress.com/2014/07/05/track-the-region-of-interest/

So, now you have integrated OpenCV with Windows Visual Studio

Thanks 🙂

Advertisements

SERIAL PORT AND VISUAL STUDIO

Hello everyone,

Getting a little off-track from our image processing,in my internship I had to integrate Serial Port with Visual Studio(C++).In that process I got stuck in retreiving data from the serial port as the event handler errors were coming up.After a very intensive research I tried to compile all my knowledge to implement it and “KA-BOOM”….. it worked 🙂

So I would like to share with you the entire process.

First open a windows c++ form application.

Then,in the form the very first thing you do is add a serial port from tool-box,in its properties set the BaudRate,Parity,StopBits,Number of bits to be transferred according to the hardware.

Now,select “button” from tool-box and drop it down on the form,set Text = Transfer and Name = btnTransfer.
Select another button from tool-box and drop it down on the form.set Text = Open and Name = btnOpen.
Select another button from tool-box and drop it down on the form.set Text = Close and Name = btnClose.

Select a comboBox from tool-box and drop it down on the form.Change its DropDown Style poperty from DropDown to DropDownList.

Select a label and place it in front of the comboBox and name it “Port”.

Select “TextBox” from tool-box and drop it down on the form,set Name = txtTransfer.

Double click anywhere on the form to go to the form1.h file.

Firstly in the namespace add:
using namespace System::IO::Ports;

Then after the following part of the code:
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();

Add This: findPorts();

Just before the “#pragma region” add:
String^ Data;
String^ data1;

Now inside the function named void “InitializeComponent(void)” add:
this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived_1);

After the “#pragma endregion” add:

private: void findPorts(void)
{
// get port names
array<Object^>^ objectArray = SerialPort::GetPortNames();
// add string array to combobox
this->comboBox1->Items->AddRange( objectArray );
}

Come to the form(GUI) and select the serial port,in the properties window,select the bar which is like lightning flashing and click on the DataReceieved and write its value as “serialPort1_DataReceived_1”.

Now in the code add this:

private: System::Void SetTextCallback(System::Object^ sender, System::EventArgs^ e)
{
this->txtTransfer->AppendText(data1);
Data = this->txtTransfer->Text;
}

private: System::Void serialPort1_DataReceived_1(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
data1 = serialPort1->ReadExisting();
this->Invoke(gcnew EventHandler(this, &Form1::SetTextCallback));
}

Now go to the form and double click on “Open” button and paste the code inside the function created:

if(this->comboBox1->Text==String::Empty)
this->txtTransfer->Text=”Please Select Port Settings”;
else {
try{
// make sure port isn’t open
if(!this->serialPort1->IsOpen){
this->serialPort1->PortName=this->comboBox1->Text;

//open serial port
this->serialPort1->Open();
}
else
this->txtTransfer->Text=”Port isn’t openned”;
}
catch(UnauthorizedAccessException^){
this->txtTransfer->Text=”UnauthorizedAccess”;
}
}

Now go to the form and double click on “Transfer” button and paste the code inside the function created:

this->serialPort1->Write(“THE INVOKING STRING GOES HERE”);

Now go to the form and double click on “Close” button and paste the code inside the function created:

this->serialPort1->Close();

Compile and run it.
And as soon as the string reaches the port you will receive the data immediately in the textBox we created.

Thank you 🙂