2014년 12월 1일 월요일

OpenCV - Color image In/Out

#pragma once
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>


using namespace std;
using namespace cv;

// Write grayscale image
void WriteIm(String str, unsigned char* im, const Size& sz)
{
Mat bIm = Mat(sz.height, sz.width, CV_8UC1, im);
imwrite(str, bIm);
}

void main()
{
Mat image = imread("face.jpg");
if (image.empty()) return;

cv::Size sz = image.size();

 // hsl channel split
Mat hsl;
cvtColor(image, hsl, CV_RGB2HLS);
Mat hslChannels[3];
split(hsl, hslChannels);

unsigned char *im_l = hslChannels[1].ptr<unsigned char>();
unsigned char *im_h = hslChannels[0].ptr<unsigned char>();
unsigned char *im_s = hslChannels[2].ptr<unsigned char>();


 // rgb channel split
Mat rgbChannels[3];
split(image, rgbChannels);

unsigned char *im_r = rgbChannels[0].ptr<unsigned char>();
unsigned char *im_g = rgbChannels[1].ptr<unsigned char>();
unsigned char *im_b = rgbChannels[2].ptr<unsigned char>();

 // access image pointer
 for(int i=0; i<sz.height; i++)
 {
    for(int j=0; j<sz.width; j++)
    {
           im_r[i*sz.width+j] = 255 - im_r[i*sz.width+j];
    }
 }

  // Write image
WriteIm("inv_red.jpg", im_r, sz);
waitKey();
}

댓글 없음:

댓글 쓰기