برق، الکترونیک، الکتروتکنیک، مکاترونیک، پزشکی، کشاورزی

برق، الکترونیک، الکتروتکنیک، مکاترونیک، پزشکی، کشاورزی و

برق، الکترونیک، الکتروتکنیک، مکاترونیک، پزشکی، کشاورزی

برق، الکترونیک، الکتروتکنیک، مکاترونیک، پزشکی، کشاورزی و

داده هایی در مورد برق، الکترونیک، الکتروتکنیک، مکاترونیک، پزشکی، کشاورزی و

تبلیغات
آخرین نظرات

۳ مطلب با کلمه‌ی کلیدی «اپن سی وی» ثبت شده است

اپن سی وی+ایکلیپس+ویندوز+سی میک+مینگ جی سی سی

ShahBaz | سه شنبه, ۳۰ شهریور ۱۳۹۵، ۰۶:۰۸ ب.ظ

In order to get a working OpenCV environment on Windows, we'll need the following tools:
  1. MinGW
  2. OpenCV
  3. Cmake
  4. Eclipse IDE
The following procedure was tested with these versions of the aforementioned tools, but should also work with other versions close to these:
- OS: Windows 10 Pro 
- MinGW (mingw-get version 0.6.2)
- OpenCV for Windows (v. 2.4.11)
- CMake for Windows (v. 3.3.1)
- Eclipse for C/C++ Developers (version: Mars Release 4.5.0)
 
Download, Install and Setup MinGW

 
 Download
Browse the official website for MinGW at mingw.org and click on "Download Installer" on the right. You'll be redirected on a sourceforge page where the executable file should begin downloading automatically.The file mingw-get-setup.exe is downloaded.
 
 Install
Run mingw-get-setup.exe and press "Install". 
Better leave the default Installation Directory to "C:\MinGW". Also better leave checked the following checkboxes as they are by default:
Then press "Continue". On the next screen just wait for it to download a few things and when it's done press "Continue".
A new window will appear. Select "Basic Setup" on the left side (probably selected by default) and mark for installation the packages "mingw32-base" and "mingw32-gcc-g++". These should be enough. 
Then click "Installation | Apply Changes". On the next screen press "Apply" again. Wait until it's done and then close all windows.
 
As a last step, we need to add the "C:\MinGW\bin" path to the PATH environment variable.
Go to "System | Advanced system settings | Environment Variables...", select the Path system variable and press "Edit...". At the end of the path add ";C:\MinGW\bin" (do not forget the semi-colon in front of C in order to separate it from the other paths). It should look something like this:
 
Then press "OK" on all windows opened, and you're done.
 

Download and Install OpenCV


 Download
Browse OpenCV's website at opencv.org/downloads.html and download "OpenCV for Windows". Here, we'll be using OpenCV version 2.4.11 as an example. The downloaded file should be opencv-2.4.11.exe (for the 2.4.11 version). 

 Install
Run opencv-2.4.11.exe. It will ask for a folder to extract OpenCV. I chose to extract under C:\. Select your extraction folder and click "Extract". 

After extraction "C:\opencv" contains the extracted files. Browse there with Windows Explorer. Inside there should be two folders named build and sources. Create another folder and name in "MinGW" (or anything else you want). We're gonna use this empty folder on a later step.

Download and Install CMake


 Download
Browse Cmake's website at www.cmake.org/download and click on the latest stable link. Look at Binary distributions and select the file that corresponds to Windows (Win32 Installer). As of now the file is cmake-3.3.1-win32-x86.exe.

 Install
Run the downloaded file and install it using all the default options selected.

 Setup
After installation run CMake (cmake-gui). Press the Browse Source button and navigate to "C:\opencv\sources" (if you installed OpenCV on a different path instead of right under C:\ as I did previously, then browse to that path and select the sources folder). 
Next, press Browse Build and select the folder we created under C:\opencv, which I named MinGW. So the path entered should be "C:\opencv\MinGW". 
Then, press Configure. This brings up a new dialog where you're asked to specify the generator for this project. I just select here "MinGW Makefiles" and leave the "Use default native compilers" selected. Then click on Finish. When it's done, press Generate. You'll see something like this, if succeed, and close cmake:

Open Command Prompt and go to "C:\opencv\MinGW". Then write: mingw32-make.
Wait until it reaches 100% and then close the prompt.

*Note: You may experience an error message during mingw32-make, like this:
If that happens, open Windows Explorer, go to "C:\MinGW\include" folder and find and open the file "commctrl.h". Find the lines on top of the file (probably lines 13-15) that look like this:
1
2
3
#if 0
#define _WIN32_IE 0x0300
#endif
Change these lines to:
1
2
3
#if 1
#define _WIN32_IE 0x0500
#endif
Save the file, and try again mingw32-make. This time it should work.

Finally add to the PATH environment variable the path "C:\opencv\MinGW\bin". Go to "System | Advanced system settings | Environment Variables...", select the Path system variable and press "Edit...". At the end of the path add ";C:\opencv\MinGW\bin" (do not forget the semi-colon in front of C in order to separate it from the other paths). It should look something like this now:

Download and Install Eclipse for C++ Developers

 Download
Browse to the download page of Eclipse's website eclipse.org/downloads and download Eclipse IDE for C/C++ Developers. Then extract the downloaded archive.
Note: To run Eclipse you need to have Java installed (Java JRE or JDK). Don't mix 32-bit versions of one tool with 64-bit versions of another, or errors may occur. Here, we've used the 32-bit versions of Java, Eclipse, MinGW, and CMake.

 Setup CDT Tools
Open eclipse and if it's the first time, specify a workspace folder where the projects will reside. Go to "Help | Install New Software..." and click on "Available Software Sites". A new dialog appears and make sure that the checkbox in front of CDT is checked and press OK. Then under Work with select "CDT  - http://download.eclipse.org/tools/cdt/releases/8.7". Check the "CDT Main Features" and press Next, then Next again, then accept the license agreements and press Finish.
Restart eclipse.


--
Next, we'll see how to setup a functioning OpenCV project with all the necessary dependencies, both in C++ and Java, using eclipse.
  • ShahBaz

نصب ماژول ها در اپن سی وی

ShahBaz | يكشنبه, ۳۱ مرداد ۱۳۹۵، ۰۷:۵۶ ب.ظ

sudo su
cd /home/elinux/opencv-3.1.0/build

cmake -DOPENCV_EXTRA_MODULES_PATH=/home/elinux/opencv_contrib-master/modules -DBUILD_opencv_legacy=OFF /home/elinux/opencv-3.1.0

  • ShahBaz

تشخیص حرکت و کلیک موس در اپن سی وی

ShahBaz | جمعه, ۲۹ مرداد ۱۳۹۵، ۰۳:۵۴ ب.ظ

How to Detect Mouse Clicks and Moves

OpenCV supports for detecting mouse events. Mouse events include mouse clicks and movements over an attached OpenCV window.


OpenCV Example Code


It is very simple to do that. All you have do is to define a callback function in the OpenCV C++ code attaching to the OpenCV window. That callback function will be called every time, mouse events occur. That callback function will also give the coordinates of the mouse events. (e.g - (x, y) coordinate of a mouse click).

Here is the simple OpenCV code to detect left, right and middle mouse clicks and mouse movements with its coordinates

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if  ( event == EVENT_LBUTTONDOWN )
{
cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if  ( event == EVENT_RBUTTONDOWN )
{
cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if  ( event == EVENT_MBUTTONDOWN )
{
cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
     else if ( event == EVENT_MOUSEMOVE )
     {
          cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;

     }
}

int main(int argc, char** argv)
{
// Read image from file 
Mat img = imread("MyPic.JPG");

//if fail to read the image
if ( img.empty() ) 

cout << "Error loading the image" << endl;
return -1; 
}

//Create a window
namedWindow("My Window", 1);

//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);

//show the image
imshow("My Window", img);

// Wait until user press some key
waitKey(0);

return 0;

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

You can download this OpenCV visual C++ project from here

Detect Mouse Clicks and Moves Over the OpenCV Window
Detect Mouse Clicks and Moves Over the OpenCV Window


Summary

In the above OpenCV sample code,  "CallbackFunc" function will be called on any mouse event (Moving a mouse over the attached OpenCV window is also a mouse event). By using suitable if - else blocks, I printed only left, right and middle mouse clicks and mouse movements over the window.

Here are new OpenCV functions, found in the above example code. If you are not familiar with the other OpenCV functions as well, please go through the other lessons in this tutorial.


  • void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0)
This function sets a callback function to be called every time any mouse events occurs in the specified window. Here is the detailed explanation of the each parameters of the above OpenCV function.
    • winname - Name of the OpenCV window. All mouse events related to this window will be registered
    • onMouse - Name of the callback function. Whenever mouse events related to the above window occur, this callback function will be called. This function should have the signature like the following
      • void FunctionName(int event, int x, int y, int flags, void* userdata)
        • event - Type of the mouse event. These are the entire list of mouse events
          • EVENT_MOUSEMOVE
          • EVENT_LBUTTONDOWN
          • EVENT_RBUTTONDOWN
          • EVENT_MBUTTONDOWN
          • EVENT_LBUTTONUP
          • EVENT_RBUTTONUP
          • EVENT_MBUTTONUP
          • EVENT_LBUTTONDBLCLK
          • EVENT_RBUTTONDBLCLK
          • EVENT_MBUTTONDBLCLK
        • x - x coordinate of the mouse event
        • y - y coordinate of the mouse event
        • flags - Specific condition whenever a mouse event occurs. See the next OpenCV example code for the usage of this parameter. Here is the entire list of enum values which will be possesed by "flags"
          • EVENT_FLAG_LBUTTON
          • EVENT_FLAG_RBUTTON
          • EVENT_FLAG_MBUTTON
          • EVENT_FLAG_CTRLKEY
          • EVENT_FLAG_SHIFTKEY
          • EVENT_FLAG_ALTKEY
        • userdata - Any pointer passes to the "setMouseCallback" function as the 3rd parameter (see below)
    • userdata - This pointer will be passed to the callback function

OpenCV Example to Detect Mouse Clicks While Pressing a Key

I am going to explain you how to detect a mouse event while pressing a key of the keyboard. 
The following OpenCV example code will detect left mouse clicks while pressing the "CTRL" key , right mouse clicks while pressing the "SHIFT" key and movements of the mouse over the OpenCV window while pressing the "ALT" key.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if ( flags == (EVENT_FLAG_CTRLKEY + EVENT_FLAG_LBUTTON) )
{
cout << "Left mouse button is clicked while pressing CTRL key - position (" << x << ", " << y << ")" << endl;
}
else if ( flags == (EVENT_FLAG_RBUTTON + EVENT_FLAG_SHIFTKEY) )
{
cout << "Right mouse button is clicked while pressing SHIFT key - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_MOUSEMOVE && flags == EVENT_FLAG_ALTKEY)
{
cout << "Mouse is moved over the window while pressing ALT key - position (" << x << ", " << y << ")" << endl;
}
}

int main(int argc, char** argv)
{
// Read image from file 
Mat img = imread("MyPic.JPG");

//if fail to read the image
if ( img.empty() ) 

cout << "Error loading the image" << endl;
return -1; 
}

//Create a window
namedWindow("My Window", 1);

//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);

//show the image
imshow("My Window", img);

// Wait until user press some key
waitKey(0);

  return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

You can download this visual C++ OpenCV project from here.

No new OpenCV functions in the above example. If you have some doubt about any OpenCV functions in the above example, please refer to previous lessons.


Next Tutorial : Rotate Image & Video

Previous Tutorial : How to Add Trackbar

  

  • ShahBaz