#include "stdafx.h"
#include<stdio.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
//从文件中读入图像
clock_t start,finish;
start=clock();
IplImage * img = cvLoadImage("E:\landscape\Landscape0135.jpg");
//如果读入图像失败
IplImage * b = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage * g = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage * r = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
printf("width = %d,height = %d chanels= %d depth = %d",img->width,img->height,img->nChannels,img->depth);
char * data = img->imageData;
int step = img->widthStep;
int chanel = img->nChannels;
char * bdata = b->imageData;
char * gdata = g->imageData;
char * rdata = r->imageData;
for(int i=0;i<img->height;i++)
{
for(int j=0;j<img->width;j++)//这里注意三通道的step是单通道的三倍,这里注意除以3,否则数组越界,IPL_DEPTH_8U
{
bdata[i*step/3 + j] = data[i*step + j*chanel];
gdata[i*step/3 + j] = data[i*step + j*chanel + 1];
rdata[i*step/3 + j] = data[i*step + j*chanel + 2];
}
}
for(int i=0;i<backimg->height;i++)
{
for(int j=0;j<backimg->width;j++)
{
if(*cvGet2D(back, i, j).val==255)
{
data[i*step + j*chanel] = 0;
data[i*step + j*chanel+1] = 0;
data[i*step + j*chanel+2] = 255;
}
}
}
//显示图像
cvNamedWindow("blue");
cvNamedWindow("green");
cvNamedWindow("red");
cvShowImage("blue",b);
cvShowImage("green",g);
cvShowImage("red",r);
cvNamedWindow("Window Test");
cvShowImage("Window Test",img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Window Test");
finish=clock();
cout<<(finish-start)/CLOCKS_PER_SEC<<endl;
//此函数等待按键,按键盘任意键就返回
waitKey();
return 0;
} #include "stdafx.h"
#include<stdio.h>
#i