ImageToBGR_彩色图像三通道转化成BGR3幅单通道图像
ImageToBGR.c 采用指针偏移
#include "stdafx.h" #include "cv.h" #include "highgui.h" #include "cxcore.h" int main() { IplImage* img; img = cvLoadImage("E:\\TempDataForDebug\\tomato.jpg"); IplImage* b_img; IplImage* g_img; IplImage* r_img; b_img = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); g_img = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); r_img = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); int y,x; for (y = 0; y<img->height; y++) { unsigned char* prow = (unsigned char*)(img->imageData+y*img->widthStep); unsigned char* brow = (unsigned char*)(b_img->imageData+y*b_img->widthStep); unsigned char* grow = (unsigned char*)(g_img->imageData+y*g_img->widthStep); unsigned char* rrow = (unsigned char*)(r_img->imageData+y*r_img->widthStep); for (x = 0; x<img->width; x++) { brow[x] = prow[3*x + 0]; grow[x] = prow[3*x + 1]; rrow[x] = prow[3*x + 2]; } } cvNamedWindow("1"); cvShowImage("1",img); cvNamedWindow("b"); cvShowImage("b",b_img); cvNamedWindow("g"); cvShowImage("g",g_img); cvNamedWindow("r"); cvShowImage("r",r_img); cvWaitKey(0); return 0; }