- Allocate image space:
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.
Note that the data is cross-access. The color image data is arranged as B0 G0 R0 B1 G1 R1...
Example:
// Allocate a single-channel byte Image
Iplimage * img1 = cvcreateimage (cvsize (640,480), ipl_depth_8u, 1 );
// Allocate a three-channel floating point image
Iplimage * img2 = cvcreateimage (cvsize (640,480), ipl_depth_32f, 3 );
- Release image space:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
cvReleaseImage(&img);
- Copy image:
IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
IplImage* img2;
img2=cvCloneImage(img1);
- Set/get interest areas:
Void cvsetimageroi (iplimage * image, cvrect rect );
Void cvresetimageroi (iplimage * image );
Vrect cvgetimageroi (const iplimage * image );
Most opencv Functions Support ROI.
- Set/get interest channels:
Void cvsetimagecoi (iplimage * image, int COI); // 0 = all
Int cvgetimagecoi (const iplimage * image );
Most opencv functions do not currently support COI.
- Load images from files:
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
By default, the loaded image is converted to a 3-channel color image. If not, the flag must be added:
IMG = cvloadimage (filename, flag );
Flag:> 0 loaded image to three-channel Color Image
= 0 load the image into a single channel grayscale image
<0: Do not convert and load the image (the number of channels is the same as the number of image files ).
- Image Storage is an image file:
if(!cvSaveImage(outFileName,img)) printf("Could not save: %s/n",outFileName);
The format of the input file is determined by the file extension.
- Convert to grayscale or color byte 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)
- Convert a color image to a grayscale image:
Use the opencv conversion function:
cvCvtColor(cimg,gimg,CV_BGR2GRAY); // cimg -> gimg
Direct conversion:
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);
- Color Space conversion:
cvCvtColor(src,dst,code); // src -> dst
code = CV_<X>2<Y>
<X>/<Y> = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS
e.g.: CV_BGR2GRAY, CV_BGR2HSV, CV_BGR2Lab
- Draw a cube:
// Draw a cube between (100,100) and (200,200) with a red line with a width of 1.
Cvrectangle (IMG, cvpoint (100,100), cvpoint (200,200), cvscalar (, 0), 1 );
- Circle:
// Draw a circle with a radius of 20 at (100,100) and use a green line with a width of 1
Cvcircle (IMG, cvpoint (100,100), 20, cvscalar (0,255, 0), 1 );
- Draw line segments:
// Draw a green line between (100,100) and (200,200) with a width of 1
Cvline (IMG, cvpoint (100,100), cvpoint (200,200), cvscalar (0,255, 0), 1 );
- Draw a group of line segments:
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);
- Filled polygon in the painting:
cvFillPoly(img,curveArr,nCurvePts,nCurves,cvScalar(0,255,255));
- Add text:
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,