IplImage结构体为:
- typedef struct _IplImage
- {
- int nSize;
- int ID;
- int nChannels;
- int alphaChannel;
- int depth;
-
- char colorModel[4];
- char channelSeq[4];
- int dataOrder;
-
- int origin;
-
- int align;
- int width;
- int height;
- struct _IplROI *roi;
- struct _IplImage *maskROI;
- void *imageId;
- struct _IplTileInfo *tileInfo;
- int imageSize;
- char *imageData;
- int widthStep;
- int BorderMode[4];
- int BorderConst[4];
- char *imageDataOrigin;
- }
- IplImage;
1:重要的参数有nChannels depth origin width height widthStep<和矩阵的step相同> imageData—指针<注意图像的数据类型为uchar,imageData为char类型,没有CvMat那么复杂了>
2:彩色图像的数据排列方式
彩色图像有三个通道(B,G,R),这三个通道的值,在Opencv中的排列顺序是B0G0R0B1G1R1…BnGnRn,采用这种交叉排列的方式进行存储。
3:访问IplImage的数据 ---- 指针访问<注意图像的数据类型为uchar >
代码:
- #include <iostream>
- #include "cv.h"
- #include "cxcore.h"
- #include "highgui.h"
- using namespace std;
- int main()
- {
- IplImage *image = cvLoadImage("F:\\11.jpg",1);
- cvNamedWindow("show",0);
- for(int y = 0; y < image->height; y++)
- {
- uchar *p_image = (uchar*)(image->imageData + y * image->widthStep);
- for(int x = 0; x < image->width; x++)
- {
- *(p_image + x*3) = 0;
- }
- }
- cvShowImage("show", image);
- cvWaitKey(0);
- return 0;
- }
4:将三通道分解为三个单通道 彩色必须由三种颜色构成 单通道只能说明是该取值,不能说明其它色彩,彩色必须要有三种通道构成 用到的函数为cvCreateImage
code:
- #include <iostream>
- #include "cv.h"
- #include "cxcore.h"
- #include "highgui.h"
- using namespace std;
- int main()
- {
- IplImage *image = cvLoadImage("F:\\11.jpg",1);
- IplImage *b_img = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
- IplImage *g_img = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
- IplImage *r_img = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
- cvNamedWindow("show",0);
- cvNamedWindow("b",0);
- cvNamedWindow("g",0);
- cvNamedWindow("r",0);
- for(int y = 0; y < image->height; y++)
- {
- uchar *p_image = (uchar*)(image->imageData + y * image->widthStep);
- uchar *b_image = (uchar*)(b_img->imageData + y * b_img->widthStep);
- uchar *g_image = (uchar*)(g_img->imageData + y * g_img->widthStep);
- uchar *r_image = (uchar*)(r_img->imageData + y * r_img->widthStep);
- for(int x = 0; x < image->width; x++)
- {
- b_image[x] = *(p_image + x*3);
- g_image[x] = *(p_image + x*3 + 1);
- r_image[x] = *(p_image + x*3 + 2);
- }
- }
- cvShowImage("show", image);
- cvShowImage("b", b_img);
- cvShowImage("g", g_img);
- cvShowImage("r", r_img);
- cvWaitKey(0);
- cvReleaseImage(&image);
- cvReleaseImage(&b_img);
- cvReleaseImage(&g_img);
- cvReleaseImage(&r_img);
- cvDestroyWindow("show");
- cvDestroyWindow("b");
- cvDestroyWindow("g");
- cvDestroyWindow("r");
- return 0;
- }
5:ROI和COI的理解
ROI是Region of Interest 的缩写。表示的是在一副大图像的感兴趣区域。”感兴趣区域”,指的是再一副大图像中,我们需要做处理的一部分
COI是Channels Of Interest, 是指感兴趣的通道,彩色图像有BGR三个通道。所以,可以选择其中一个作为感兴趣通道
用到的函数为cvSetImageROI cvResetImageROI cvCopy
code(图像的一部分替换另一幅图像的一部分):
- #include <iostream>
- #include "cv.h"
- #include "cxcore.h"
- #include "highgui.h"
- using namespace std;
-
- int main()
- {
- IplImage *tongtong = cvLoadImage("F:\\tongtong.jpg",1);
- IplImage *jiale = cvLoadImage("F:\\jiale.jpg",1);
- CvRect rect,rect1;
- rect.x = 1200;
- rect.y = 300;
- rect.width = 340;
- rect.height = 400;
- rect1.x = 90;
- rect1.y= 65;
- rect1.width = 120;
- rect1.height = 150;
- cvSetImageROI(jiale,rect);
- cvSetImageROI(tongtong,rect1);
- cvResize(jiale,tongtong);
-
- cvResetImageROI(tongtong);
- cvNamedWindow("tongtong");
- cvNamedWindow("jiale");
- cvShowImage("tongtong",tongtong);
- cvShowImage("jiale",jiale);
- cvWaitKey(0);
- return 0;
- }
作者:小村长 出处:http://blog.youkuaiyun.com/lu597203933 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:小村长zack, 欢迎交流!)