[OpenCV #6]split and merge

OpenCV/example 2013. 2. 20. 18:17

1.분리하고 통합

result - 원본 이미지를 RGB로 분리하고 Red와 Blue 채널을 바꿔, 통합한 결과

gray - 0.299*R + 0.587*G + 0.114*B 공식을 적용하여 gray이미지 출력

밑의 창들은 채널 분리한 결과

void split(const Mat& mtx, Vector<Mat>& mv)

mtx - the original color image

mv - the result set is 1-channel image

 

void merge(const Vector<Mat>& mv, Mat& dst)

mv - the original set of 1-channel image

dst - the resulting color image

 

2.소스코드

#include<opencv\cv.h>
#include<opencv\highgui.h>

using namespace cv;
using namespace std;
int main()
{
 Mat image = imread("sistar.jpg"); //Load image form disk
 if(!image.data) return -1; //Check image
 //Split the original image into three channels
 //channels[0],channels[1],channels[2]
 vector<Mat> channels;
 split(image,channels); //Partition image into three channel planes

 imshow("Red",channels[2]);
 imshow("Green",channels[1]);
 imshow("Blue",channels[0]);

 

 vector<Mat>channelslzm(3);
 //Changes the Red and Blue sites:
 channelslzm[0]=channels[2];
 channelslzm[1]=channels[1];
 channelslzm[2]=channels[0];
 Mat imagelzm;
 merge(channelslzm,imagelzm);
 imshow("Result",imagelzm);

 

 //Calculating the brightness according to the formula 0.299*R + 0.587*G + 0.114*B

 //(But, in fact, it's right to do woth cvtColor)
 Mat gray=0.299*channels[2]+0.587*channels[1]+0.114*channels[0];
 imshow("Gray",gray);
 waitKey(0); //Wait for keystroke
 return 0;

}


반응형

'OpenCV > example' 카테고리의 다른 글

[OpenCV]카메라 영상 출력  (0) 2013.04.04
[OpenCV #7]이진 영상 변환(threshold)  (0) 2013.03.04
[OpenCV #5]cv::Mat 클래스,복사,픽셀 접근  (0) 2013.02.19
[OpenCV #4]관심 영역  (0) 2013.02.14
[OpenCV #3]Threshold  (0) 2013.02.14