Tag Archives: GCC

OpenCV project in Cygwin/GCC – Part 2

Part 2 – Making OpenCV project in Cygwin

As explained in Part 1, once you have prepared the environment and installed all the required packages, you are now ready to make your first OpenCV project on Cygwin.


Step 1: Write code

Let this be your first OpenCV Project: Read an Image file, convert it to Grayscale image and show it on screen.

Make a folder ‘test’. Then make a file ‘myproj.cpp’ copy the following code in it and put it under a sub folder ‘src’ (i.e. test/src/myproj.cpp)

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <cstdio>
#include <iostream>
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
char* filename = (argc >= 2 ? argv[1] : (char*)"defaultimg.jpg");
Mat imgInput = imread(filename, 1);

if( imgInput.empty() )
{
cout << "Couldn'g open image " << filename << endl;
cout << "Usage: ./myproj.exe <image_name>\n";
return 0;
}
namedWindow("Input Image",WINDOW_AUTOSIZE);
imshow("Input Image",imgInput);

Mat imgGray;

/* Convert to Grayscale */
cv::cvtColor(imgInput, imgGray, COLOR_BGR2GRAY);
namedWindow("Output Image",WINDOW_AUTOSIZE);
imshow("Output Image",imgGray);

cv::waitKey(0);
return 0;
}

Step 2: Make a ‘CMakeLists.txt’ file

This file is used to generate the ‘make’ file. Make a file named ‘CMakeLists.txt’ in the ‘test’ folder and copy the following code in it.

cmake_minimum_required(VERSION 2.8.4)
PROJECT (myproj)
find_package(OpenCV REQUIRED )

set( NAME_SRC
src/myproj.cpp
)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( myproj ${NAME_SRC} ${NAME_HEADERS} )
include_directories(/usr/include /usr/include/opencv2 /usr/include/opencv)
link_directories(/usr/lib ${CMAKE_BINARY_DIR}/bin)
target_link_libraries(myproj opencv_core opencv_highgui opencv_imgproc)

Step 3: Build

Open your Cygwin terminal, and go to the ‘test’ folder you just made. Run the following two commands one by one:

$ cmake .
$ make

This will give you an executable myproj.exe in ‘test/bin’ folder.

Step 4: Run

To run the executable we just made, we first need to run XServer using following command on your Cygwin terminal:

$ startxwin

This will start the XServer and an XServer icon will appear in your taskbar

Cyg6

Right Click on this icon and from ‘Applications’ menu, select ‘xterm’. This will start the xterm terminal.

Now in the xterm, cd to the ‘test/bin’ folder and execute your exe file with providing it with an image as an argument. (the image must be present in the ‘test/bin’ folder)

$ ./myproj.exe lena.jpg

This will show the input image as well as the grayscale output image

OpenCV project in Cygwin/GCC – Part 1

Part 1 – Preparation

I used to make my OpenCV projects in C++ using Visual Studio on my Windows Machine. However for my next project I wanted to make sure that the project can be build on Linux as well. But, since I do not have a Linux machine, I thought of using GCC Compiler on Cygwin. (If the project works fine on GCC/Cygwin, it should run fine on a Linux machine as well). But it wasn’t as easy as I thought.


 

Step 1: Install Cygwin

My other blog post explains how-to-install-cygwin-on-windows

Step 2: Install OpenCV libraries for Cygwin

Since my windows machine had Visual Studio specific OpenCV libraries, that can not be used with Cygwin/gcc, I had to install OpenCV libraries for Cygwin separately. I used Cygwin Ports for the installation.

Run the Cygwin Setup setup-x86.exe (or setup-x86_64.exe) and follow instructions upto step 8 of this post. At step 9 of choosing packages, under the “Libs” category, select “libopencv-devel” and “libopencv2.4”, click Next and install these libraries.

Cyg2

 Step 3. Install CMake utility

“CMake is cross-platform free and open-source software for managing the build process of software using a compiler-independent method” (from wiki).

Again run the Cygwin Setup setup-x86.exe (or setup-x86_64.exe) and follow instructions upto step 8 of this post. At step 9 of choosing packages, under “Devel” category, select “cmake”, click next and install.

Cyg3

Now you are ready to build your OpenCV projects using Cygwin/GCC. However if your OpenCV projects involve displaying images etc, non command prompt windows, you need to install XServer.

Step 4. Install XServer

Again run the Cygwin Setup setup-x86.exe (or setup-x86_64.exe) and follow instructions upto step 8 of this post. At step 9 of choosing packages, under “X11” category, select “xinit”, “xorg-server”, “xorg-x11-fonts-XXXXX”, click next and install.

Cyg5

Now you are ready to compile and run your OpenCV project under Cygwin. Happy Coding !!!

How to install Cygwin on Windows

In this tutorial we will install Cygwin on Windows for using gcc/g++ compiler, gdb debugger, and make utility, in order to start building C/C++ programs using Cygwin.

1. Download the Cygwin setup from https://cygwin.com/install.html (choose the 32 bit or 64 bit version, depending on your OS)

2. Run the downloaded Setup File setup-x86.exe (or setup-x86_64.exe) (prefer: Right Click -> Run as Administrator)

3.  Click Next on the First Welcome Screen

4. Select “Install from Internet” -> Click Next

Cyg0

5. Select a “Root Install Dir” (or leave the default dir e.g. C:\cygwin64) -> Click Next

6. Select a “Local Package Dir”, this is where Cygwin Setup will save downloaded files before installation. (e.g. D:\MySotwares\Cygwin)

7. Select appropriate “Internet Connection” in the next screen. If your machine is connected to internet by a proxy server, make sure to select “Internet Explorer Proxy settings” or if you know the proxy settings yourself select “Use HTTP/FTP Proxy Settings” and write down the proxy server settings (e.g. Proxy Host: 172.17.101.12 & Port: 8080). -> Click Next

8. Choose a Donwload Site. Choose any mirror site listed (Just click on it) -> Click Next

9. Next Screen is “Select Packages”,

Cyg1

Click on the + sign left of “Devel” category, it will show a list of packages that can be installed. Select the following packages (by clicking over “Skip” written)

  • gcc-g++: GNU Compiler Collection
  • gdb: The GNU debugger
  • make: The GNU verison of the ‘make’ utility

[ Warning: DO NOT SELECT the  “ALL – Default”, it will install all the packages available for Cygwin. This will take enormous amount of downloading as well as space on your disk ]

Click Next

10. Setup will look for dependencies and show you a list of packages to be installed. -> Click Next

11. When installation is finished, check “Create Icon on Desktop” -> Click Finish

Thats it, you have just installed the gcc, g++, gdb, and ‘make’ utilities, and you are ready to compile and run your first program on Cygwin. Happy Coding!