Thursday, September 12, 2013

How to convert a color image to grayscale (OpenCV)

Converting a color image ( 3-Channel image ) to grayscale image ( 1-channel ) using Opencv.

Steps:
1.0 convert_to_grayscale.cpp
    1.1. Create two Mat object ( img, gray_img ).
    1.2. Load image img using imread.
    1.3. Convert using cvtColor function .
    1.4. Save it to Gray_image.jpg
    1.5. Show it on open window.
2.0 CMakeLists.txt
3.0 Compiling and Executing

Download code from here

convert_to_grayscale.cpp

Include headers
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
1.1. Create two Mat object ( img, gray_img ).

  Mat img, gray_img;                           
  char* imgname =argv[1];

1.2. Load image img using imread. 
img = imread(argv[1],1);                    
  if(!img.data)
  {
   cout<<"Image data not found";
   return -1;
  }

1.3. Convert using cvtColor function .
cvtColor(img,gray_img,CV_BGR2GRAY);    
1.4. Save it to Gray_image.jpg
 imwrite("../convert_to_grayscale/Gray_image.jpg", gray_img);    
1.5. Show it on open window.
  namedWindow(imgname,CV_WINDOW_AUTOSIZE);
  namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
  imshow(imgname, img);                             
  imshow("Gray Image", gray_img);
  waitKey(0);
  
  return 0;
}
2.0 CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project( convert_to_grayscale )
find_package( OpenCV REQUIRED )
add_executable( convert_to_grayscale convert_to_grayscale.cpp )
target_link_libraries( convert_to_grayscale ${OpenCV_LIBS} )
3.0 Compiling and Executing
cd  /home/lokender/Desktop/convert_to_grayscale/
cmake /home/lokender/Desktop/convert_to_grayscale/
make
./convert_to_grayscale loki.jpeg
OUTPUT:














Hope you enjoy this post. Don't Forget to share this post.

Using Opencv with CMAKE

How to code OpenCV program using CMake.

Why CMake? - Because there is no need to change anything while porting between windows and linux.
Let us take an example where we need to write Opencv code for viewing an Image on screen.

Steps:-
   1. Create a file ShowImage.cpp.
   2. Create CMakeLists.txt
   3. Save both files (ShowImage.cpp, CMakeLists.txt) into  same folder named Show(say).
   4. Change your directory to  /Show.
   5. Create Executables.
   6. Run Executable.

Download the code from here

Step 1.    ShowImage.cpp
#include <stdio.h> 
#include <opencv2/opencv.hpp> 
using namespace cv; 
int main( int argc, char** argv ) 
{  
   Mat image; 
   image = imread( argv[1], 1 ); 
   if( argc != 2 || !image.data )  
   { 
     printf( "Image data not found \n" ); 
     return -1; 
   } 
  namedWindow( "Show Image", CV_WINDOW_AUTOSIZE ); 
  imshow( "Show Image", image ); 
  waitKey(0); 
  return 0; 
}
Step 2. CMakeLists.txt For More info about CMake check this Tutorial
cmake_minimum_required(VERSION 2.8) 
project( ShowImage )  
find_package( OpenCV REQUIRED ) 
add_executable( ShowImage ShowImage.cpp )  
target_link_libraries( ShowImage ${OpenCV_LIBS} )

Step 3. Save both files and image into folder "Show" .

Step 4. Change directory to Show
cd <Show_Directory>
Step 5. cmake <Show_Directory> make
Step 6. ./ShowImage loki.jpeg

OUTPUT:

 
 
 
 
 
 
 
 
 
 
 
 
 
 
Hope you enjoy this post. Don't Forget to share this post.
 

Tuesday, September 18, 2012

How to change permission of a file or directory in a linux

How to change permission of a file (or directory) in a Unix/Linux System?


This is the very basic problem which a first time unix/linux user faces very often that is how to get special privilege or permission to a file or directory. The intended audience to this post is who want to understand "Ownership and permissions" in a unix like systems from the very basic step-by-step.


Basic Concepts:-

For every file or directory there are three categories who may have access.

1:- Owner 

2:- Group

3:- Others

Brief description of each given below.

1:- Owner

Owner of a file is a person who created the file. Traditionally ownership of a file is not transferable but we can change it by using command
chown
Only SUPER-USER (User who have admin power) can use this command.
For a super-user start every command with "SU" then shell prompt asks for a password, enter password , now you become super user.

2:- Group

There might be a group of users who have group ownership and permissions to a file or directory. Use following command to see group ownership.
ls -lg

3:- Others

All users other than group and owner are known as others.

Every file or directory has three type of permission describing which type of operation would be possible with directory or file.

1:- Read(r)


User who has this permission for a file or directory allow to see the content of that file and to explore that directory.

2:- Write(w)


User who has this permission for a file or directory allow to change the content of that file or directory. With this permission user can create new files and can remove existing ones.

3:- Execute(e)

User who has this permission for a file or directory allow to use filename as a command and to change the directory ( through "cd" command ) and can copy files from from that directory .

Changing Permissions!


There are three categories of users , and three types of permissions read(r), write(w), execute(e) apply to each of them. hence there are 9 cases. These 9 permissions collectively known as "mode" of file or directory. Use following command to change the mode of file or directory.
chmod
each file has some kind of permissions. To know which file has what kind of permissions use the given command .
ls -l
The first column looks like this.

drwxrwx--- or
drwxr-xr-x or
-rwxrwxr--

The ten lettered word denote permission.
first letter tell you about nature of the file. if it is "d" the file is actually a directory. If it is "-" file is an ordinary file. remaining 9 letters split into three words of three letter each, the first word tells about owner, the second about group and the third about the others.

There are total of 8 possibilities which are denoted by numbers between 0 and 7. These numbers are generated by Let us consider one of them assigning the values 0 to 1, 2 to w and 4 to r, and adding them as shown in figure


This helps us to replace a word of 9 letters by a three letter digit number. for example

rw-r-xrwx

can be splited into three groups as

rw- r-x rwx

and hence would be replaced by 657.

In this, the first digit denotes permission for the owner whether he/she can read, write and execute the file. The second digits denotes what members of the group can do. The third one denotes what any other user can do.

the command to change permission (mode) is very simple as shown below.
chmod 744 filename

so that filename gets "744" permission..

Friday, August 10, 2012

How Does a Unix/Linux Computer Run?

Let us briefly describe what happens when you try to use computer. Following diagram explains various steps involved




Explaination:-

1:-

Initially system is put on by system administrator. If you are running unix/linux , you yourself are the system administrator. For a large configuration with more than one terminal attached to main system, a person called system administrator does this for you.

2:-

It may happen that all terminals attached to the system may not be on after the booting is done. The system starts a program "getty" for each such terminal. This program periodically checks whether you have put on the terminal. This period may be fraction of second. After the terminal is put on, a new program "login" takes over from "getty". This "login" program prompts the message, as described earlier. it reads the user name and password and matches the password with the one stored in the system

3:-

After the user login, this "login" program is taken over by another program called "sh" (for "shell"). This "shell" is a command interpreter program that reads each command from the terminal and performs accordingly.

4:-

After user finished his/her work, he/she log out. There might be another user who want to use the system. In this case the terminal is not put off by the first user, consequently, the "sh" is taken over by "login again to enable the next user to log in.

5:-

On the other hand, after logging out, the user might put the terminal off. As soon as it put off, the "login" program is taken over by "getty".

6:-

At the end, before the system is put off, the system administrator puts all the "getty" properly off, and then shut the system down.