RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Use OpenCV to separate RGB image into red green and blue components

Article Index
1. WRITE CODE
2. COMPILE
3. USAGE

This small OpenCV program demonstrates how to separate RGB image into R, G and B components. The program is written in low level programming as there are build in function in OpenCV which would make this code more efficient. However, this example deepens an understanding on how image is split into matrix of pixels and how can each pixel can be manipulated separately.

1. WRITE CODE

#include <iostream>
#include "cv.h"
#include "highgui.h"

using namespace std;

int main( int argc, char** argv )

{
//load color img specified by first argument
//IplImage *img = cvLoadImage( argv[1]);
IplImage *img = cvLoadImage
(argv[1], CV_LOAD_IMAGE_COLOR );


IplImage *red = cvCreateImage

(cvSize(img->width, img->height ),
img->depth, img->nChannels );

IplImage *green = cvCreateImage
(cvSize(img->width, img->height ),
img->depth, img->nChannels );

IplImage *blue = cvCreateImage
(cvSize(img->width, img->height ),
img->depth, img->nChannels );



// setup the pointer to access img data
uchar *pImg = ( uchar* )img->imageData;

// setup pointer to write data
uchar *pRed = ( uchar* )red->imageData;
uchar *pGreen = ( uchar* )green->imageData;
uchar *pBlue = ( uchar* )blue->imageData;



int i, j, rED, gREEN, bLUE, byte;
for( i = 0 ; i < img->height ; i++ )
{

for( j = 0 ; j < img->width ; j++ )
{
rED = pImg[i*img->widthStep + j*img->nChannels + 2];

gREEN = pImg[i*img->widthStep + j*img->nChannels + 1];
bLUE = pImg[i*img->widthStep + j*img->nChannels + 0];
// RED

pRed[i*img->widthStep + j*img->nChannels + 2] = rED;
// GREEN
pGreen[i*img->widthStep + j*img->nChannels + 1] = gREEN;

// BLUE
pBlue[i*img->widthStep + j*img->nChannels + 0] = bLUE;
}
}
// save images

cvSaveImage( argv[2], red );
cvSaveImage( argv[3], green );
cvSaveImage( argv[4], blue );

return 0;

}

2. COMPILE

g++ `pkg-config opencv --cflags --libs` \ 
separate-RGB.cpp -o separate-RGB

3. USAGE

./separate-RGB img.png red.png green.png blue.png

Share this linux post:

Submit Use OpenCV to separate RGB image into red green and blue components in Delicious Submit Use OpenCV to separate RGB image into red green and blue components in Digg Submit Use OpenCV to separate RGB image into red green and blue components in FaceBook Submit Use OpenCV to separate RGB image into red green and blue components in Google Bookmarks Submit Use OpenCV to separate RGB image into red green and blue components in Stumbleupon Submit Use OpenCV to separate RGB image into red green and blue components in Technorati Submit Use OpenCV to separate RGB image into red green and blue components in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download