October 5, 2015

Using OpenCV in GTK+ applications

Although OpenCV comes with it's own windowing system for displaying any OpenCV images, for fully functional applications with user interface elements such as buttons, menus, and radio buttons, maybe you would find it useful to use widget libraries such as GTK+ or QT. Drawing OpenCV image surfaces in GTK+ applications is not much of a difficult task than adding a couple of extra lines in your existing GTK+ applications. And again there are plenty of ways to accomplish the same results, but here I will show how I have achieved it. The sample program here is written using GTK+ C++ binding GTKmm, but the technique and functions should be same across all the GTK+ bindings.

As usual we create a GTK+ top level window by creating a MainWindow object. MainWindow, which is itself a GtkWindow widget contains one GtkFrame and one GtkDrawingArea widgets. The class definition of MainWindow is shown below:

class MainWindow : public Gtk::Window
        Gtk::Frame video_frame;
        VideoArea video_area;
        MainWindow ();
        virtual ~MainWindow();

In the above code, VideoArea is the GtkDrawingArea widget object defined as follows:

class VideoArea : public Gtk::DrawingArea
        cv::VideoCapture cv_cap;
        bool cv_opened;
        virtual bool on_draw (const Cairo::RefPtr<Cairo::Context> &cr);
        bool on_timeout ();
        VideoArea ();
        virtual ~VideoArea();  

We are going to use GtkDrawingArea widget for the purpose of displaying OpenCV image surface. To do that, we have to put OpenCV related functions in the GtkDrawingArea on_draw function. This on_draw function will be called every time draw signal is emitted from GtkDrawingArea widget. As can be seen from the VideoArea object definition, we also have a timer function on_timeout to call on_draw function in a regular manner by invalidating GtkDrawingArea widget. So when a VideoArea object is created in the MainWindow object OpenCV is initialized like shown in the code below:

VideoArea::VideoArea() : cv_opened(false)
    if (cv_cap.isOpened() == true) {
        cv_opened = true;
        Glib::signal_timeout().connect(sigc::mem_fun(*this, &VideoArea::on_timeout), 50);

Here, we connect a glib timeout signal, which will call on_timeout function every 50 milliseconds interval. The functions for on_timeout and on_draw are shown below:

bool VideoArea::on_timeout()
    Glib::RefPtr<Gdk::Window> win = get_window();
    if (win)
        Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
        win->invalidate_rect(r, false);
    return true;

bool VideoArea::on_draw(const Cairo::RefPtr<Cairo::Context> &cr)
    if (!cv_opened) return false;
    cv::Mat cv_frame, cv_frame1;

    if (cv_frame.empty()) return false;
    cv::cvtColor (cv_frame, cv_frame1, CV_BGR2RGB);
    Gdk::Cairo::set_source_pixbuf (cr, Gdk::Pixbuf::create_from_data(cv_frame1.data, Gdk::COLORSPACE_RGB, false, 8, cv_frame1.cols, cv_frame1.rows, cv_frame1.step));
    return true;

In the on_draw function, we convert OpenCV surface to RGB channel by using cvtColor, since GTK widgets uses RGB format. Finally, we use Gdk::Pixbuf::create_from_data to fill Cairo surface with OpenCV surface pixel buffers.

Download the complete program source code and test it by yourself!


To compile the files in gtkcv.zip, you need to have OpenCV and gtkmm development files installed on your operating system. On Ubuntu just follow the instructions below:

Installing OpenCV:

Downloading OpenCV and creating compile directory,

From http://opencv.org/ 
grab opencv-3.0.0 and extract the opencv-3.0 archive file. Then, create a folder named build inside the opencv-3.0.0 folder. Then, open a terminal into the build folder. Then, in the terminal put the following commands:

# setup compile environment for OpenCV


# compile sources in 4 threads

make -j 4 

# install OpenCV

sudo make install

Installing gtkmm:

sudo apt-get install libgtkmm-3.0-dev

that's it!, now extract the gtkcv.zip file and open a terminal in the gtkcv folder and execute the following command:

g++ -o gtkcv main.cpp MainWindow.cpp VideoArea.cpp `pkg-config --cflags --libs gtkmm-3.0 opencv`

this will produce gtkcv executable file.

July 11, 2015

imamLL linked list library store struct elements

As imamLL (imamLL-1.3.tar.gz) is very versatile in storing any type of data into a list, you can store an struct as an element like shown in the following code example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "imamll.h"

struct Person {
    char name[24];
    double age;
    double weight;
    double height;

struct imamLL *People_list = NULL;          /* Pointer to hold list */
struct imamLL_element *person = NULL;       /* Pointer to hold individual element */
unsigned long c;

int main (int argc, char* argv[])
    printf ("Allocating memory for list\n");
    People_list = imamLL_list_create();

    if (People_list == NULL) {
        printf ("Can not create the list\n");
        exit (1);
    if ((person = imamLL_element_add (People_list, sizeof(struct Person), AT_END)) == NULL) {
        printf ("Can not add element");
        printf ("Freed list, returned %d\n", imamLL_list_destroy (People_list));
        exit (1);
    strcpy (((struct Person *)person->data)->name, "Md Imam Hossain");
    ((struct Person *)person->data)->age = 27.0;
    ((struct Person *)person->data)->height = 180.0;
    ((struct Person *)person->data)->weight = 67.0;

    printf ("Allocated: %lu Bytes\n", People_list->size);
    if ((person = imamLL_element_add (People_list, sizeof(struct Person), AT_END)) == NULL) {
        printf ("Can not add element");
        printf ("Freed list, returned %d\n", imamLL_list_destroy (People_list));
        exit (1);
    strcpy (((struct Person *)person->data)->name, "Md Salim Hossain");
    ((struct Person *)person->data)->age = 22.0;
    ((struct Person *)person->data)->height = 182.0;
    ((struct Person *)person->data)->weight = 75.0;

    printf ("Allocated: %lu Bytes\n", People_list->size);
    while (1) {
        person = imamLL_element_get_next (People_list);
        if (person == NULL) break;
        printf ("*Person*\n");
        printf ("Name: %s\n", ((struct Person *)person->data)->name);
        printf ("Age: %lf\n", ((struct Person *)person->data)->age);
        printf ("Height: %lf\n", ((struct Person *)person->data)->height);
        printf ("Weight: %lf\n", ((struct Person *)person->data)->weight);
    printf ("Freed: %d elements\n", imamLL_list_free (People_list));
    printf ("Freed list, returned %d\n", imamLL_list_destroy (People_list));
    return (EXIT_SUCCESS);

July 9, 2015

Fix Ubuntu shutdown and suspend problem on HP 250 G3

If you have a HP 250 G3 notebook and use Ubuntu based GNU/Linux then you may be experiencing problems such as the system will halt at shutdown and will hang when you try to suspend. All these problems are related to each other. You can easily fix these problems by just changing some settings in your BIOS.

First thing you can try if you have Windows operating system, then you can try installing the latest vendor BIOS program and see if the problems persist. After installing latest BIOS if the problems still occur then just go into your BIOS setup screen and look for USB 3 configuration in pre-OS option and then change the USB 3 configuration in pre-OS state to be in enabled. BIOS is the operating system neutral system settings program you can get into when the computer starts. To enter BIOS just try to press Esc key on the keyboard and press one of the F1-F12 key to enter BIOS settings. And if you have done everything correctly, hopefully, you will no longer experience any of the above problems anymore :)

July 5, 2015

imamLL a simple C linked list library

C programming language does not have a linked list implementation like other programming languages such as java. Since C program gives more control to it's users, it is up to users how they implement their own linked list for their programs. imamLL is a linked list implementation library for C designed to be efficient and flexible. Some of the features of imamLL library are
  • Dynamically allocate data at any given point in the program runtime 
  • Add elements of arbitrary sizes into the lists 
  • Navigate through the elements in both forward and backward 
  • Add, remove, modify and get elements from the lists
In this tutorial, we are going to build a simple program using imamLL library.

First, we download imamLL library from the following link:


After extracting the file we run ./build.sh in the terminal to install the library into the system.

Now, we create a blank text file and name it num_list.c

The content of the file is shown below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <imamll.h>        /*header for imamLL */

struct imamLL *num_list = NULL;
struct imamLL_element *element = NULL;

int main (int argc, char** argv)
    num_list = imamLL_list_create();
    if ( num_list == NULL) {
        printf ("Can not create the list\n");
        exit (EXIT_FAILURE);
    element = imamLL_element_add (num_list, sizeof (int), 0);

    if (element == NULL) printf ("Error allocating memory for an integer element\n");
    else *((int *)element->data) = 10;
    element = imamLL_element_add (num_list, sizeof (int), 0);

    if (element == NULL) printf ("Error allocating memory for an integer element\n");
    else *((int *)element->data) = 20;

    element = imamLL_element_add (num_list, sizeof (int), 0);

    if (element == NULL) printf ("Error allocating memory for an integer element\n");
    else *((int *)element->data) = 30;
    imamLL_list_rewind (num_list);

    while ((element = imamLL_element_get_next(num_list)) != NULL) {
        printf ("%d\n", *((int *)element->data));
    imamLL_list_destroy (num_list);

    return 0;

Now to compile the num_list.c, in the terminal:

gcc -Wall -o num_list num_list.c -limamll

The program above program can be easily understood by reading the Intro.html file found in the imamLL directory. There are also few more example programs located in the examples directory of imamLL.

June 29, 2015

Linux C/C++ fork () Example

If you ever wanted to do multiple tasks at the same time in your C/C++ program then Linux fork () system call function is for you. When your C/C++ compiled binary program begins execution, it creates a main process in which all the program instructions are executed. In Linux every program you run will create one or more processes. Inside each process the program instructions will be executed sequentially. Which means inside one process without completing one task the program will not be able to do another task and that is where fork () function comes in handy. The fork () function will let you create a new process which is a exact copy of the main process but completely independent and run alongside the main process. For example, if you are copying a file in the main process then the new process created by fork () can be used to update the user interface for the copy operation. 

Things to remember about processes are:

  • Processes are one of the building blocks for multitasking in Linux
  • Each process has it's own unique id provided by the Linux kernel by which the process can be tracked for different purposes
  • Processes can run independently to each other, therefore closing or terminating one process does not effect the other processes
  • Any process will have their own memory space, therefore variables from stack and heap from one process are not accessible by other processes
Now, lets build a simple C program to see how fork () can be utilized

In the program we call the main process as parent process    , and the new process created from inside main process as child process. This naming convention for processes is very typical, since one process can create another new process by using fork().

We build a simple program where, parent process counts to 10 while at the same time child process counts to 20. After finishing counting the parent process waits for the child process to end.


#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main (int argc, char *argv[])
    pid_t child_id;
    child_id = fork ();
    if (child_id == -1) {
        printf ("Creating new process by fork() failed!\n");
        return 1;
    else if (child_id == 0) {
        /* code block for child process */
        int count;
        for (count = 1; count < 10; count++) {
            printf ("Child counting: %d\n", count);
            sleep (1);
        _exit (0);
    else {
        /* code block for parent process */
        int child_exit_status;
        int count;
        printf ("Child process id: %d\n", child_id);
        for (count = 1; count < 5; count++) {
            printf ("Parent counting: %d\n", count);
            sleep (1);
        printf ("Waiting for child to end\n");
        wait (&child_exit_status);
        printf ("Child exited with %d\n", child_exit_status);

    return EXIT_SUCCESS;

the above code should print the following output in the terminal:

Child process id: 7225
Child counting: 1
Parent counting: 1
Parent counting: 2
Child counting: 2
Child counting: 3
Parent counting: 3
Child counting: 4
Parent counting: 4
Child counting: 5
Waiting for child to end
Child counting: 6
Child counting: 7
Child counting: 8
Child counting: 9
Child exited with 0

Now, let's go through the most important elements in the code:

We need to include unistd.h for the fork function. The other two header files types.h and wait.h are required by the wait () function. First, we create a variable of type pid_t for storing child process id which will be returned by the fork () function. The fork function returns -1 if the function can not create a new process and if successful fork() returns 0 to the child process and the process id of created process (child) to the parent process (main process). Therefore, inside the child process the value for child_id would be 0 and it would be child process id inside parent (main) process. Inside the child process block the _exit function is used to terminate the child process. The sleep function inside the for loop blocks will suspend the respective execution for specified amount of seconds given in the parameters, in our case it is one second. And finally the wait function will suspend the parent (main) process until state of one of the child processes changes, in our case when the child process exits. And finally, the exit status of the child process is retrieved by using the wait function which is stored in the child_exit_status variable.

Hopefully, this tutorial helped you learn basic about processes.

Download the fork() example source file and test it by yourself:


June 20, 2015

Install Ralink RT3290 Wi-Fi driver on Ubuntu based distributions

Ralink. RT3290 is a PCIe device which combines 802.11bgn Wi-Fi and Bluetooth 3.0 devices in a single chip. Currently, Linux kernel has experimental support for RT3290 wireless through the module named rt2800pci. The state of this module is such that in many circumstances you will have very weak wireless signals.

Ubuntu 15.04 comes with Linux kernel version 3.19 and the problem of weak RT3290 wireless signals is still prevalent since by default Ubuntu 15.04 will be using Linux kernel rt2800pci module. However, there is a official driver available for Linux from MediaTek for Ralink RT3290 STA wireless device. This driver will fix weak signal and many other problems on Ubuntu 15.04 based distributions. To install this driver simply download the following file and after extracting the file in the Home directory of your Ubuntu, follow the instructions in the ReadMe file inside the extracted RT3290 folder (You may need to shutdown computer and boot again to get it working). 

RT3290 Linux Driver (Ubuntu 15.04)

After installing the driver hopefully you will have better wireless experience in your Ubuntu system :)

For fedora users, please try the following:

RT3290 Linux Driver

*Updated package for latest Ubuntu  based distributions (tested against Ubuntu 17.04). Also suitable for Ubuntu 15.04 based distributions:

RT3290 Linux Driver (Ubuntu 16.04 or latest)

*If you update Linux kernel, you will have to run the following commands from the RT3290_u16 directory to compile and install the driver for new kernel:


sudo ./install.sh

*If you do not see Wi-Fi network, you may need to activate it manually by executing the following command from RT3290_u16 folder or reboot computer:

sudo ./activate-net-rt

April 22, 2015

Setup Objective-C programming environment on Ubuntu Distributions

Objective-C is a programming language which expands C programming language by including Smalltalk object orientated programming language. It has robustness like C programming language as well as it is feature rich like C++ and Java. It fully supports object oriented programming including Encapsulation, Data hiding, Inheritance and Polymorphism. It is the main programming language for OS X and iOS application development.

GNU Compiler Collection (GCC) provides compiling support for Objective-C source files through GNUstep framework. It is very easy to setup Objective-C development environment on Ubuntu based distributions. We just have to install few packages on to our Ubuntu System.

From your favourite package manager just install the following packages:



Or in the terminal put the following command:

sudo apt-get install gobjc gnustep gnustep-make gnustep-common gnustep-core-devel gnustep-devel

Once all the packages are installed properly, you should be able to compile any Objective-C source file by entering the following command in the terminal:

gcc source.m `gnustep-config --objc-flags` -lobjc -lgnustep-base

This should produce a.out binary in the current directory.

For Example, put the following codes into a text editor and save it as Hello.m

#import <Foundation/Foundation.h>

int main(int argc, char *argv[])

    NSLog(@"Hello, World!\n");
   return 0;

Now, compile the source file Hello.m using following command in the terminal:

gcc Hello.m `gnustep-config --objc-flags` -lobjc -lgnustep-base

Now, run the executable file by putting following command in the terminal:


You should see something like below in the terminal,

2015-04-22 19:24:26.483 a.out[31686] Hello, World!

April 12, 2015

Do Programming on Ubuntu/Kubuntu Linux

Programming on Linux
Linux in general is great development platform where the operating system's kernel itself is free and Open Source. So there is virtually no limit of what you want to do with your Linux computer. Kubuntu, Ubuntu and Gnome Ubuntu are all great Linux Desktop distributions with bleeding edge desktops and touch pad user interfaces. You pick your Linux distribution flavor and start working on your projects in no time. All Ubuntu variant distributions have common package names for development and application support.

Programming Language support

Ubuntu Linux by default comes with GNU Compiler Collection and you will find almost all of the software build for the system uses GNU Compiler Collection (GCC). With GCC you can program for C, C++, Objective-C, Objective-C++, Fortran, Java, Ada, Go and many other programming languages. One good thing about GCC is that it is available for many different operating systems for example Microsoft Windows, Apple Mac OS. Furthermore, GCC is supported on all major CPU architectures.

Programming in C and C++ languages

GCC will give you gcc compiler and g++ compiler for C and C++ source files respectively. Both gcc and g++ compiler support almost all existing C standards for example ANSI C, ISO C, C99 and C11.

Installation of required packages

To get minimal development environment with C and C++ compilers, install the following packages under Ubuntu distributions:



in the terminal:

sudo apt-get install build-essential autoconf automake flex bison libtool gcc-multilib g++-multilib

Then, install manual pages which will help you understand function definitions.

in the terminal:

sudo apt-get install manpages-dev manpages-posix

Now, you can start writing your programs in the text editor of your choice  and compile your source files easily from the terminal by putting the following command:

For C program:

gcc source_file.c

For C++ program

g++ source_file.cpp

This will produce executable file named a.out in the current directory.

If you want to read manual of a function just type the following in the terminal:

man name_of_the_function

For example:

man printf

to exit from manual press q keyboard key. Use Up and Down arrow key to navigate through the manual.

If you prefer Integrated Development Environment (IDE) then check out the following IDEs. Here I put my experiences with different IDEs:


Build on Java, solid user interface, recommended for all kind of developments


Uses native user interface, simple and good for experimentation and start-up projects


Build on Java, Extensible in nature


Native KDE development environment, good for C++ and QT projects


Lightweight native IDE, good for simple projects

Finally, if you ask me which IDE I would prefer, I would definitely say Netbeans :)

February 4, 2015

Get Open Sound System (OSS) applications and games working in Ubuntu/Kubuntu Linux


Open Sound System (OSS) is an obsolete sound system for GNU/Linux used by many old games and applications. In recent modern Linux distributions such as Ubuntu/Kubuntu 14.10 the support for OSS in the kernel can not be found. OSS can have both kernel level drivers for actual digital sound processors aka sound cards or can be user space library. OSS provides an interface for applications to make use of sound devices.

Getting OSS support in Ubuntu based distributions

Instead of rebuilding the Linux kernel with OSS support, we can easily make OSS working by just installing few packages from standard Ubuntu repository.

Open up the terminal and install the osspd and osspd-pulseaudio packages by putting the following command:

sudo apt-get install osspd osspd-pulseaudio

Finish installing packages. Now you can reboot the computer or start the service that provides OSS support by putting the following command in the terminal:

sudo service osspd start

check weather OSS service running by the following command:

sudo service osspd status

You should find  * osspd is running output in the terminal.

And finally, now check weather OSS is actually working by the following command:

cat /dev/urandom > /dev/dsp

You should hear white noise. Stop the noise by pressing Ctrl + C keys together in the terminal.

Now, you can hear sound from many old applications which require OSS for sound.

January 8, 2015

Enrich your Kubuntu 14.10 desktop with plasma widgets


Kubuntu operating system uses KDE desktop environment for it's default desktop shell. There are many things you can do with your KDE desktop environment. One of the cool feature of KDE is it's Plasma widgets. Moreover, KDE desktop shell is just a combination of different Plasma widgets and panels. For example, your main application launcher Kickoff is one plasma widget, System tray is another plasma widget containing icons for Wi-Fi, battery, volume etc, Task manager is another plasma widget shows the list of currently running applications. You can easily elaborate your KDE desktop shell by adding additional Plasma widgets to your desktops and panels.

To see and add a widget, right click on the desktop and select Add Widgets. This will bring up widget collection interface where can search for a particular widget as well as see widgets that are currently running on the desktop.

To add a widget to the desktop just drag the particular widget and drop it in the desktop. If you double click on any widget it will be added to the current panel.

KDE widget collection interface

Widgets are adjustable to your needs. If you place your mouse pointer over the widgets then it will bring up a control bar from where you can resize, rotate and change settings of the widgets.

Here is a selection of exciting Plasma widgets for your desktop to try out:

Folder [Installed by default]

As the name suggests, this widget will show the contents of a particular folder right on your desktop. One cool feature of this widget is, it will let you dig into different sub-folders easily just by hovering over them.

Notes [Installed by default]

This is the desktop sticky notes you have been waiting for long time!. It has all the bells and whistles you expect from a general purpose text editor including spell checking. If you constantly take notes while you are working then you will find this widget very helpful.

Analog Clock [Installed by default]

This one handy full featured clock will let you chose different Calender systems from around the world as well as different Time Zones of your interest. Once setup, you can easily see the time from different Time Zones just by scrolling your mouse over the widget.


You will not be disappointed by this weather widget. This widget is fancy and interactive. It has all the features from an ideal weather widget including option for showing forecast in different units and changing widget theme.

install yaWP by putting the following command in the terminal:

sudo apt-get install plasma-widget-yawp

Unit Converter [Installed by default]

If you are a person who needs to convert between units regularly then this widget is for you!


SearchMoid is for searching web right from your desktop. Basically, you type the web address such as www.google.com in SearchMoid and it will open the address in a web browser

install SearchMoid by putting the following command in the terminal:

sudo apt-get install plasma-widget-searchmoid

Shelf [Installed by default]

This widget will let you open different things such as applications, system devices, system tools, etc of your desktop all from one place. This widget is definitely a must have if you want to increase your desktop productivity.

There are many more useful widgets for your comfort installed by default and easily install-able by putting the following command in the terminal:

sudo apt-get install plasma-widget-*