void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font,CvScalar color );
typedef struct _IplImage
{
int nSize; // IplImage大小,=sizeof(IplImage)
int ID; // 版本 (=0)
int nChannels; // 大多数OPENCV函数支持1,2,3 或 4 个通道
int alphaChannel; /* 被OpenCV忽略 */
int depth; /* 像素的位深度: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U,
IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F 可支持 */
char colorModel[4]; /* 被OpenCV忽略 */
char channelSeq[4]; /* 被OpenCV忽略 */
int dataOrder; /* 0 - 交叉存取颜色通道,对三通道RGB图像,像素存储顺序为BGR BGR BGR ... BGR;
1 - 分开的颜色通道,对三通道RGB图像,像素存储顺序为RRR...R GGG...G BBB...B。
cvCreateImage只能创建交叉存取图像 */
int origin; /* 0 - 顶—左结构,
1 - 底—左结构 (Windows bitmaps 风格) */
int align; /* 图像行排列 (4 or 8). OpenCV 忽略它,使用 widthStep 代替 */
int width; /* 图像宽像素数 */
int height; /* 图像高像素数*/
struct _IplROI *roi;/* 图像感兴趣区域. 当该值非空只对该区域进行处理 */
struct _IplImage *maskROI; /* 在 OpenCV中必须置NULL */
void *imageId; /* 同上*/
struct _IplTileInfo *tileInfo; /*同上*/
int imageSize; /* 图像数据大小(在交叉存取格式下imageSize=image->height*image->widthStep),单位字节*/
char *imageData; /* 指向排列的图像数据 */
int widthStep; /* 排列的图像行大小,以字节为单位 */
int BorderMode[4]; /* 边际结束模式, 被OpenCV忽略 */
int BorderConst[4]; /* 同上 */
char *imageDataOrigin; /* 指针指向一个不同的图像数据结构(不是必须排列的),是为了纠正图像内存分配准备的 */
}
IplImage
通过IplImage读取像素值方法如下:
1、通过cvGet2D(*img,x,y).val[x];可以读取R,G,B
#include "cv.h"
#include "highgui.h"
#include
#include
IplImage *src = NULL;
CvFont font;
static void cvMouseCallBack(int event, int x, int y, int flags, void* param)
{
CvPoint pt;
int r,g,b;
char *rgb = malloc(200);
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
pt.x = x;
pt.y = y;
b = (int)(cvGet2D(src,x,y).val[0]);
g = (int)cvGet2D(src,x,y).val[1];
r = (int)cvGet2D(src,x,y).val[2];
sprintf(rgb,"RGB:%d:%d:%d",r,g,b);
cvPutText(src,rgb,pt,&font,cvScalar(0,100,20,0));
cvShowImage("Practice",src);
free(rgb);
break;
default:break;
}
}
int main(int argv,char **argc)
{
if((argv==2)&&(src=cvLoadImage(argc[1],1)))
{
cvNamedWindow("Practice",1);
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,1,1,0,1,8);
cvSetMouseCallback("Practice",cvMouseCallBack,NULL);
cvShowImage("Practice",src);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&src);
}
return 0;
}
2、通过imageData,widthStep为行大小
uchar *cdata = (uchar*)xxx->imageData;
uchar step = widthStep/(sizeof(uchar))
r = cdata[x*step+y];
#include "cv.h"
#include "highgui.h"
#include
#include
IplImage *src = NULL;
CvFont font;
static void cvMouseCallBack(int event, int x, int y, int flags, void* param)
{
CvPoint pt;
int r,g,b;
unsigned char *cdata = (unsigned char *)src->imageData;
int step = src->widthStep/(sizeof(unsigned char));
unsigned char* tmp = malloc(307200);
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
pt.x = x;
pt.y = y;
b = cdata[x*step+y*src->nChannels+0];
g = cdata[x*step+y*src->nChannels+1];
r = cdata[x*step+y*src->nChannels+2];
sprintf(tmp,"RGB:%d:%d:%d",r,g,b);
cvPutText(src,tmp,pt,&font,cvScalar(0,255,255,0));
cvShowImage("Practice",src);
free(tmp);
break;
default:break;
}
}
int main(int argv,char **argc)
{
if((argv==2)&&(src=cvLoadImage(argc[1],1)))
{
cvNamedWindow("Practice",1);
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,1,1,0,1,8);
cvSetMouseCallback("Practice",cvMouseCallBack,NULL);
cvShowImage("Practice",src);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&src);
}
return 0;
}