网上已经有很多答案了,我这里只是记一下自己的,没啥创新或者参考意义:)
1. a>cvAbs和cvRound; b>cvRandInt和cvRandReal;c>cvPoint2D32f,cvPointFrom32f;d>cvPointTo32f
2.
CvMat *matrix=cvCreateMat(100,100,CV_8UC3); assert(matrix); cvZero(matrix); cvCircle(matrix,cvPoint(50,50),50,CV_RGB(255,0,0)); cvNamedWindow("ex3_2",1); cvShowImage("ex3_2",matrix); cvReleaseMat(&matrix); cvWaitKey(); cvDestroyWindow("ex3_2");
3.
CvMat *matrix=cvCreateMat(100,100,CV_8UC3); assert(matrix); cvZero(matrix); for(UINT i=20;i<=40;++i) for(UINT j=5;j<=20;++j) { uchar *ptr=(uchar *)cvPtr2D(matrix,i,j); ptr[1]=255; } cvNamedWindow("ex3_3",1); cvShowImage("ex3_3",matrix); cvWaitKey(); cvReleaseMat(&matrix); cvDestroyWindow("ex3_3");
4.
CvMat *matrix=cvCreateMat(100,100,CV_8UC3); assert(matrix); cvZero(matrix); for(size_t i=20;i<=40;++i) { uchar *ptr=matrix->data.ptr+i*matrix->step; for(size_t j=5;j<=20;++j) { ptr[j*3+1]=255; } } cvNamedWindow("ex3_4",1); cvShowImage("ex3_4",matrix); cvWaitKey(); cvReleaseMat(&matrix); cvDestroyWindow("ex3_3");
5.
IplImage *img=cvCreateImage(cvSize(210,210), IPL_DEPTH_8U,1); assert(img); cvZero(img); size_t beginPoint=0; double beginColor=70; while(beginPoint<=200) { cvSetImageROI(img,cvRect(beginPoint,beginPoint, img->width-beginPoint,img->height-beginPoint)); cvSet(img,cvScalar(beginColor)); cvResetImageROI(img); beginPoint+=20; beginColor+=10; if(beginColor>255)beginColor=70; } cvNamedWindow("ex3_5",1); cvShowImage("ex3_5",img); cvWaitKey(); cvReleaseImage(&img); cvDestroyWindow("ex3_5");
6.
IplImage *img=cvLoadImage(".\\testimage.JPG"); assert(img); IplImage *header1=cvCreateImageHeader(cvSize(20,30),img->depth, img->nChannels); IplImage *header2=cvCreateImageHeader(cvSize(20,30),img->depth, img->nChannels); assert(header1 && header2); header1->origin=header2->origin=img->origin; header1->widthStep=header2->widthStep=img->widthStep; header1->imageData=(char *)cvPtr2D(img,5,10); header2->imageData=(char *)cvPtr2D(img,50,60); cvNot(header1,header1); cvNot(header2,header2); cvNamedWindow("ex3_6",1); cvShowImage("ex3_6",img); cvWaitKey(); cvReleaseImage(&img); cvDestroyWindow("ex3_6");
7.
IplImage *img=cvLoadImage(".\\testimage.JPG"); assert(img); IplImage *red_image=cvCreateImage(cvSize(img->width,img->height), img->depth,1); IplImage *green_image=cvCreateImage(cvSize(img->width,img->height), img->depth,1); IplImage *blue_image=cvCreateImage(cvSize(img->width,img->height), img->depth,1); assert(red_image && green_image && blue_image); cvSplit(img,red_image,green_image,blue_image,0); //a cvNamedWindow("ex3_7_a",1); cvShowImage("ex3_7_a",green_image); cvWaitKey(); cvDestroyWindow("ex3_7_a"); //b IplImage *clone1=cvCloneImage(green_image); IplImage *clone2=cvCloneImage(green_image); //c double max,min; cvMinMaxLoc(green_image,&min,&max); //d double thresh=(max-min)/2.0; cvSet(clone1,cvScalar(thresh)); //e cvZero(clone2); cvCmp(green_image,clone1,clone2,CV_CMP_GE); //f cvSubS(green_image,cvScalar(thresh/2.0),green_image, clone2); cvNamedWindow("ex3_7_f",1); cvShowImage("ex3_7_f",green_image); cvWaitKey(); cvReleaseImage(&green_image); cvDestroyWindow("ex3_7_f");
8.
struct my_struct { int n; CvPoint point; CvRect Rect; }; void write_my_struct(CvFileStorage *fs, const char *name, my_struct *ms) { assert(fs && ms); cvStartWriteStruct(fs,name,CV_NODE_SEQ); cvWriteInt(fs,"n",ms->n); cvStartWriteStruct(fs,"point",CV_NODE_SEQ); cvWriteInt(fs,0,ms->point.x); cvWriteInt(fs,0,ms->point.y); cvEndWriteStruct(fs); cvStartWriteStruct(fs,"Rect",CV_NODE_SEQ); cvWriteInt(fs,0,ms->Rect.height); cvWriteInt(fs,0,ms->Rect.width); cvWriteInt(fs,0,ms->Rect.x); cvWriteInt(fs,0,ms->Rect.y); cvEndWriteStruct(fs); cvEndWriteStruct(fs); } void read_my_struct(CvFileStorage *fs, CvFileNode *ms_node, my_struct *ms) { assert(ms && fs); ms->n=cvReadIntByName(fs,ms_node,"n"); CvSeq *seq=cvGetFileNodeByName(fs,ms_node,"point")->data.seq; assert(seq); ms->point.x=cvReadInt((CvFileNode *)cvGetSeqElem(seq,0)); ms->point.y=cvReadInt((CvFileNode *)cvGetSeqElem(seq,1)); seq=cvGetFileNodeByName(fs,ms_node,"Rect")->data.seq; assert(seq); ms->Rect.height=cvReadInt((CvFileNode *)cvGetSeqElem(seq,0)); ms->Rect.width=cvReadInt((CvFileNode *)cvGetSeqElem(seq,1)); ms->Rect.x=cvReadInt((CvFileNode *)cvGetSeqElem(seq,2)); ms->Rect.y=cvReadInt((CvFileNode *)cvGetSeqElem(seq,3)); }