Use o OpenCV para separar a imagem RGB em componentes vermelho verde e azul
Este pequeno programa OpenCV demonstra como separar uma imagem RGB em componentes R, G e B. O programa é escrito em programação de baixo nível, pois há uma função embutida no OpenCV que tornaria esse código mais eficiente. No entanto, este exemplo aprofunda o entendimento de como a imagem é dividida em uma matriz de pixels e como cada pixel pode ser manipulado separadamente.
ESCREVER CÓDIGO
#include
#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;
}
COMPILAR
g++ `pkg-config opencv --cflags --libs` \
separate-RGB.cpp -o separate-RGB
USO
./separate-RGB img.png red.png green.png blue.png