图像处理
单通道图像灰度取值
IplImage * pSrcImage = cvLoadImage("Sunset.jpg",CV_LOAD_IMAGE_GRAYSCALE);
uchar * pSrcData = (uchar *)pSrcImage->imageData;
for (int i = 0; i < pSrcImage->height; i++)
{
for (int j = 0; j < pSrcImage->width; j++)
{
int n = i * pSrcImage->width + j;
char a = pSrcData[n];// 这个即是像素值
}
}
cvReleaseImage(&pSrcImage);
分配与释放图像空间
- 分配图像空间:IplImage* cvCreateImage(CvSize size, int depth, int channels);
size: cvSize(width,height);
depth: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U,
IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F, IPL_DEPTH_64F
channels: 1, 2, 3 or 4.
注意数据为交叉存取.彩色图像的数据编排为b0 g0 r0 b1 g1 r1 ...举例:
// 分配一个单通道字节图像
IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
// 分配一个三通道浮点图像
IplImage* img2=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); - 释放图像空间:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
cvReleaseImage(&img); - 复制图像:IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
IplImage* img2;
img2=cvCloneImage(img1); - 设定/获取兴趣区域:void cvSetImageROI(IplImage* image, CvRect rect);
void cvResetImageROI(IplImage* image);
vRect cvGetImageROI(const IplImage* image);
大部分OpenCV函数都支持ROI. - 设定/获取兴趣通道:void cvSetImageCOI(IplImage* image, int coi); // 0=all
int cvGetImageCOI(const IplImage* image);
大部分OpenCV函数暂不支持COI.
读取存储图像
- 从文件中载入图像: IplImage* img=0;
img=cvLoadImage(fileName);
if(!img) printf("Could not load image file: %s\n",fileName);
Supported image formats: BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM,
SR, RAS, TIFF, TIF载入图像默认转为3通道彩色图像. 如果不是,则需加flag:
img=cvLoadImage(fileName,flag);
flag: >0 载入图像转为三通道彩色图像
=0 载入图像转为单通道灰度图像
<0 不转换载入图像(通道数与图像文件相同). - 图像存储为图像文件: if(!cvSaveImage(outFileName,img)) printf("Could not save: %s\n",outFileName);
输入文件格式由文件扩展名决定.
存取图像元素
- 假设需要读取在i行j列像点的第k通道. 其中, 行数i的范围为[0, height-1], 列数j的范围为[0, width-1], 通道k的范围为[0, nchannels-1].
- 间接存取:(比较通用, 但效率低, 可读取任一类型图像数据)
- 对单通道字节图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
CvScalar s;
s=cvGet2D(img,i,j); // get the (i,j) pixel value
printf("intensity=%f\n",s.val[0]);
s.val[0]=111;
cvSet2D(img,i,j,s); // set the (i,j) pixel value - 对多通道浮点或字节图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
CvScalar s;
s=cvGet2D(img,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
s.val[0]=111;
s.val[1]=111;
s.val[2]=111;
cvSet2D(img,i,j,s); // set the (i,j) pixel value
- 对单通道字节图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
- 直接存取:(效率高, 但容易出错)
- 对单通道字节图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
((uchar *)(img->imageData + i*img->widthStep))[j]=111; - 对多通道字节图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R - 对多通道浮点图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B
((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G
((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R
- 对单通道字节图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
- 用指针直接存取 :(在某些情况下简单高效)
- 对单通道字节图像:IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
int height = img->height;
int width = img->width;
int step = img->widthStep/sizeof(uchar);
uchar* data = (uchar *)img->imageData;
data[i*step+j] = 111; - 对多通道字节图像:IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
int height = img->height;
int width = img->width;
int step = img->widthStep/sizeof(uchar);
int channels = img->nChannels;
uchar* data = (uchar *)img->imageData;
data[i*step+j*channels+k] = 111; - 对单通道浮点图像(假设用4字节调整):IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
int height = img->height;
int width = img->width;
int step = img->widthStep/sizeof(float);
int channels = img->nChannels;
float * data = (float *)img->imageData;
data[i*step+j*channels+k] = 111;
- 对单通道字节图像:IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
- 使用 c++ wrapper 进行直接存取:(简单高效)
- 对单/多通道字节图像,多通道浮点图像定义一个 c++ wrapper:template<class T> class Image
{
private:
IplImage* imgp;
public:
Image(IplImage* img=0) {imgp=img;}
~Image(){imgp=0;}
void operator=(IplImage* img) {imgp=img;}
inline T* operator[](const int rowIndx) {
return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));}
};
typedef struct{
unsigned char b,g,r;
} RgbPixel;
typedef struct{
float b,g,r;
} RgbPixelFloat;
typedef Image<RgbPixel> RgbImage;
typedef Image<RgbPixelFloat> RgbImageFloat;
typedef Image<unsigned char> BwImage;
typedef Image<float> BwImageFloat; - 单通道字节图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
BwImage imgA(img);
imgA[i][j] = 111; - 多通道字节图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
RgbImage imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111; - 多通道浮点图像:IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
RgbImageFloat imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;
- 对单/多通道字节图像,多通道浮点图像定义一个 c++ wrapper:template<class T> class Image
图像转换
- 转为灰度或彩色字节图像:cvConvertImage(src, dst, flags=0);
src = float/byte grayscale/color image
dst = byte grayscale/color image
flags = CV_CVTIMG_FLIP (flip vertically)
CV_CVTIMG_SWAP_RB (swap the R and B channels) - 转换彩色图像为灰度图像:
cvCvtColor(cimg,gimg,CV_BGR2GRAY); // cimg -> gimg
使用OpenCV转换函数:
for(i=0;i<cimg->height;i++) for(j=0;j<cimg->width;j++)
直接转换:
gimgA[i][j]= (uchar)(cimgA[i][j].b*0.114 +
cimgA[i][j].g*0.587 +
cimgA[i][j].r*0.299); - 颜色空间转换:
code = CV_<X>2<Y>
<X>/<Y> = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS
e.g.: CV_BGR2GRAY, CV_BGR2HSV, CV_BGR2Lab
绘图命令
- 画长方体:// 用宽度为1的红线在(100,100)与(200,200)之间画一长方体
cvRectangle(img, cvPoint(100,100), cvPoint(200,200), cvScalar(255,0,0), 1); - 画圆:// 在(100,100)处画一半径为20的圆,使用宽度为1的绿线
cvCircle(img, cvPoint(100,100), 20, cvScalar(0,255,0), 1); - 画线段:// 在(100,100)与(200,200)之间画绿色线段,宽度为1
cvLine(img, cvPoint(100,100), cvPoint(200,200), cvScalar(0,255,0), 1); - 画一组线段:CvPoint curve1[]={10,10, 10,100, 100,100, 100,10};
CvPoint curve2[]={30,30, 30,130, 130,130, 130,30, 150,10};
CvPoint* curveArr[2]={curve1, curve2};
int nCurvePts[2]={4,5};
int nCurves=2;
int isCurveClosed=1;
int lineWidth=1;
cvPolyLine(img,curveArr,nCurvePts,nCurves,isCurveClosed,cvScalar(0,255,255),lineWidth); - 画内填充色的多边形:cvFillPoly(img,curveArr,nCurvePts,nCurves,cvScalar(0,255,255));
- 添加文本:CvFont font;
double hScale=1.0;
double vScale=1.0;
int lineWidth=1;
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);
cvPutText (img,"My comment",cvPoint(200,400), &font, cvScalar(255,255,0));Other possible fonts:
CV_FONT_HERSHEY_SIMPLEX, CV_FONT_HERSHEY_PLAIN,
CV_FONT_HERSHEY_DUPLEX, CV_FONT_HERSHEY_COMPLEX,
CV_FONT_HERSHEY_TRIPLEX, CV_FONT_HERSHEY_COMPLEX_SMALL,
CV_FONT_HERSHEY_SCRIPT_SIMPLEX, CV_FONT_HERSHEY_SCRIPT_COMPLEX,