/**
* @author Tiago Natel de Moura
* Software simples para visualização de cores com opencv
* Se rodá-lo sobre o console ele imprime a cor atual no formato hexadecimal RGB
*/
#include <stdio.h>
#include <string.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
/**
* Nome da janela
*/
char window[] = "Cores";
/**
* Aqui ajustamos os valores iniciais das cores
*/
static int B = 100;
static int G = 100;
static int R = 100;
/**
* No OpenCV uma imagem é um ponteiro para a estrutura IplImage
*/
IplImage* color = 0;
/**
* Protótipos
* update(int) atualiza a imagem com a nova cor ajustada
* pintando(int) ajusta a nova cor
*/
void update(int);
IplImage* pintando(IplImage*,int,int,int);
int main(int argc, char *argv[])
{
/** Criamos a imagem */
color = cvCreateImage( cvSize(500,500), 8, 3);
/** ajustamos com as cores iniciais */
color = pintando(color, B,G,R);
/** Cria a janela */
cvNamedWindow(window, CV_WINDOW_AUTOSIZE);
/** Anexa a imagem na janela */
cvShowImage(window, color);
/** Cria três barras de rolagens para o azul (B), verde (G) e vermelho (R) */
cvCreateTrackbar("BLUE", window, &B, 255, update);
cvCreateTrackbar("GREEN", window, &G, 255, update);
cvCreateTrackbar("RED", window, &R, 255, update);
cvWaitKey(0);
cvDestroyWindow(window);
return 0;
}
void update(int pos)
{
printf("Cor: %.2x%.2x%.2x\n",R,G,B);
color = pintando(color, B, G, R);
cvShowImage(window, color);
}
IplImage* pintando(IplImage* img, int B, int G, int R)
{
/** Pega as propriedades da imagem */
int height = img->height;
int width = img->width;
int step = img->widthStep;
uchar* data = (uchar*) img->imageData;
int channels= img->nChannels;
/**
* Aqui percorremos todos os pixels da imagem e ajustamos
* a nova cor
*/
for(int y=0; y < height; y++)
for(int x=0;x < width; x++)
{
data[y*step + x*channels] = B;
data[y*step + x*channels+1] = G;
data[y*step + x*channels+2] = R;
}
return img;
}
A quem interessar pode fazer o download aqui.
Até mais.