Category Archives: Uncategorized

Connect Raspberry Pi to internet, using crossover ethernet cable

I didn’t have a wifi module for Raspberry Pi (B+ Model 2), but I wanted to connect it to the internet. So i connected my RPi with my Windows 7 laptop using a simple Ethernet cable, and let the Wifi internet of connection of my laptop flow through the Eth cable to Raspberry Pi.

[Raspberry Pi] —– Ethernet Cable —– [ Laptop ] —– Wifi—- [ INet ]

Go to Control Panel -> Network and Sharing Center
Click on the Wireless Internet Connection

RPi Internet1

Click on the “Properties”

RPi Internet2

In the Properties Window go to the “Sharing” tab, and check the “Allow other users to connect to the internet using this computer’s network” box. Then from the drop down box, select “Local Area Network”.

RPi Internet3

Done !!! Your Raspberry Pi is now connected to the internet. Simple.

Troubleshooting:

Check the IP Address of your raspberry pi (using command: /sbin/ifconfig) it shoud be on the same network as your Laptop are on the same network.
The inet addr of the eth0 must have the same first three sections as that of the Laptop. For example if the RPi addr is 192.168.137.XXX, the Internet Adapter Local Area Connection IPv4 addr (use ipconfig command to get your addr) of your Laptop, should also be 192.168.137.YYY

Check if RPi is be able to ping to Laptop and vice versa.

If the network addr of the two devices differ, try to reboot the RPi (keep RPi connected to the Laptop during reboot). Upon rebooting it should get a new address from your Laptop DNS.

Integrating OpenCV and OpenGL projects

I was working with a OpenCV project then I needed to show the results of some image processing using a 3D object rendering. So I made an OpenGL project that takes result data and display it using GLUT functions.

But then I was unable to figure out a way to integrate these two independent projects, because inside the main() in my OpenGL project there was this non terminating glutMainLoop() loop, and I found it difficult to figure out where to put the video frame processing loop in main() of my OpenCV project !!

OpenCV Project :

int main()
{
    VideoCapture cap(0);
    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);
    Mat frame;
    while(1)
    {
        cap.read(frame);
        imshow("MyVideo", frame);
        // Do some image processing on "frame"
        if(waitKey(30) == 27)
            break;
    }
}

OpenGL Project:

#include<gl/glut.h>
int main()
{
	glutInit(&argc, argv);
	MyInit();
	glutDisplayFunc(Display);
	glutKeyboardFunc(Keyboard);
	glutMouseFunc(Mouse);
	glutIdleFunc(Idle);
	glutMainLoop();  // non-returning loop
}

Looking up on the internet, I found that freeglut supports a function glutMainLoopEvent() that causes freeglut to process one iteration’s worth of events in its event loop. This allows the application to control its own event loop and still use the freeglut windowing system.

Although i was using glut and not freeglut,  i didn’t have to change my code (except for include files) for freeglut, because freeglut supported all the functions of glut (or atleast it supported all the glut functions used in my project).

With this, I was able to integrate the two projects easily.

OpenCV + OpenGL Project:

#include<gl/freeglut.h>        // Add freeglut.h
int main()
{
	glutInit(&argc, argv);
	MyInit();
	glutDisplayFunc(Display);
	glutKeyboardFunc(Keyboard);
	glutMouseFunc(Mouse);
	glutIdleFunc(Idle);

	VideoCapture cap(0);
	namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);
	Mat frame;
	while(1)
	{
		cap.read(frame);
		imshow("MyVideo", frame);
		// Do some image processing on "frame"
		glutMainLoopEvent();        // One iteration only
		Display();      // Call the func used with glutDisplayFunc()
		if(waitKey(30) == 27)
			break;
	}
	glutLeaveMainLoop();
	return 0;
}

This approach may cause a delay in OpenGL rendering. So, a better approach could be to use threading. One thread for video frame processing and another for OpenGL rendering.

OpenCV : Determine orientation of ellipse/RotatedRect in FitEllipse()

In my other post, I examined the RotatedRect of minAreaRect() function. But later i found out that the ellipse/RotatedRect of minFitEllipse() function doesn’t work the same way as that of minAreaRect(). (Looks like the two developers didn’t talk to each other before developing these functions !)

So, i again examined the RotatedRect extraacted from ellipse of the minFitEllipse() function and here are the results:

  1. The angle property is the angle between vertical and the edge of 0th and 1st vertices of the rectangle.
  2. If the angle is less than 90 degrees the 0th vertex if the left most (or left-bottom if angle is 0 degrees) vertex. 1st, 2nd, 3rd vertices follow in clockwise manner.
  3. If the angle is greater than or equal to 90 degrees (but < 180), 0th vertex is the top most vertex (or top left vertex if angle == 90). 1st, 2nd, 3rd vertices follow in clockwise manner.
  4. Height is always greater than (or equal to) Width.

 

This is a code snippet i used to get and draw RotatedRect of fitellipse() function.

RotatedRect box = fitEllipse(contour);
Point2f vtx[4];
box.points(vtx);
for( int j = 0; j < 4; j++ )
    line(drawing, vtx[j], vtx[(j+1)%4], Scalar(0,255,0),2);

 

Below are test cases for various test objects (in white). Green rectangle is the RotatedRect of fitEllipse() function.

Cases :  0 <= Angle < 90

result_brickright_fitellipseresult_barright_fitellipseresult_barSqrVer_fitelliipse

 

Cases :  90 <=Angle < 180

result_brickleft_fitellipseresult_barleft_fitellipseresult_barSqrHor_fitelliipseresult_barHor_fitelliipseresult_barVer_fitellipse

 

 

 

Open CV : Determine angle of RotatedRect / minAreaRect

I was working on an OpenCV project which needed to determine Angle of a RotatedRect extracted from a contour using minAreaRect() function. Unknown of the weird and intelligent logic behind the “angle” property of the RotatedRect, my results weren’t as expected. It took me a while to understand whats going on with the determined RotatedRect properties.

Unable to finding quick info about the properties of RotatedRect in web search results. I decided to inspect it myself … and here are the results.

  1. The lowest point in a rectangle is 0th vertex, and 1st, 2nd, 3rd vertices follow clockwise.
  2. Height is distance between 0th & 1st  (or 2nd & 3rd) vertices. And width is distance between 1st  & 2nd (or 0th & 3rd) vertices.
  3. Angle is calculated from the horizontal to the first edge of rectangle, counter clockwise.
  4. Angle varies between -0 to -90 (I am unsure, what is the decisive factor of -0 or -90)

 

result_barleft result_barright result_brickrightresult_brickleftresult_barSqrHorresult_barSqrVerresult_barHorresult_barVer

For OpenCV a contour is just a pattern, it doesn’t understand how much it is rotated in real life. OpenCV will get the minAreaRect and see it only as a rectangle which rotated from the horizontal (upto 90 degrees). if your object is rotated more than (-)90 degrees, next edge of the rectangle becomes the leading edge and is used to calculate the angle from the horizontal (between -0 to -90). So you have to take care of this in your code to figure out how much the contour has rotated in real life i.e. from 0 to 359 degrees.

Tip: if your contour is of fixed shape, rotating it will make the Height & Width of the RotatedRect flip on every 90 degrees boundary.

The below code snippet can give you the rotation angle from 0 to -180.

double myContourAngle = myRotatedRect.angle;
if (myRotatedRect.size.width < myRotatedRect.size.height) {
  myContourAngle = myContourAngle - 90;
}

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!