学习Opencv第3章课后习题

1.

a.abs() cvRound() cvFloor() cvCeil()

b.cvRNG() cvRandInt() cvRandReal()

c.cvPointFrom32f()

d.cvPointTo32f()

2.

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main(int argc, char** argv)
{
IplImage *img;
    img = cvCreateImage(cvSize(200,200),IPL_DEPTH_8U,3);
cvZero(img);
cvCircle(img,cvPoint(50,50),50,cvScalar(255,0,0),1);

cvNamedWindow( "Source", CV_WINDOW_AUTOSIZE );
cvShowImage( "Source", img );
cvWaitKey();
return 0;
}  

3.

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main(int argc, char** argv)
{
IplImage *img;
    img = cvCreateImage(cvSize(200,200),IPL_DEPTH_8U,3);
cvZero(img);
uchar* pData = cvPtr2D(img,1,0,NULL);
cvRectangle(img,cvPoint(20,5),cvPoint(40,20),CV_RGB(0,255,0),1);
cvNamedWindow( "Source", CV_WINDOW_AUTOSIZE );
cvShowImage( "Source", img );
cvWaitKey();
return 0;
}

4.

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main(int argc, char** argv)
{
IplImage *img;
    img = cvCreateImage(cvSize(200,200),IPL_DEPTH_8U,3);
cvZero(img);
for (int y=5;y<20;y++)
{
   uchar* ptr = (uchar*)(img->imageData + y*img->widthStep);
   for (int x=20;x<40;x++)
   {
    ptr[3*x] = 255;
    ptr[3*x + 1] = 0;
    ptr[3*x + 2] = 0;
   }
  
}
cvNamedWindow( "Source", CV_WINDOW_AUTOSIZE );
cvShowImage( "Source", img );
cvWaitKey();
return 0;
}

5.


#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main(int argc, char** argv)
{
IplImage *img;
    img = cvCreateImage(cvSize(210,210),IPL_DEPTH_8U,1);
cvZero(img);
int interset_x = 0;
int interset_y = 0;
int offset_x = img->width;
int offset_y = img->height;
int add = 0;
int res =0;
BOOL condition = TRUE;
while(add<200)
{
   cvSetImageROI(img,cvRect(interset_x,interset_y,offset_x,offset_y));
   if (condition)
   {
    cvSet(img,cvScalar(0));
    condition = FALSE;
   }
   else
   {
    cvSet(img,cvScalar(add));
    condition = TRUE;

   }  
   cvResetImageROI(img);

   add = add+20;
    interset_x = interset_x +10;
    interset_y = interset_y +10;
    offset_x = offset_x -20;
    offset_y = offset_y -20;

}

cvNamedWindow( "Source", CV_WINDOW_AUTOSIZE );
cvShowImage( "Source", img );
cvWaitKey();
return 0;
}

6.

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main(int argc, char** argv)
{
IplImage *img;
    img = cvLoadImage("lena.bmp",1);
IplImage* dst1 = cvCreateImageHeader(cvSize(20,30),img->depth,img->nChannels);
IplImage* dst2 = cvCreateImageHeader(cvSize(20,30),img->depth,img->nChannels);
dst1->origin = img->origin;
dst2->origin = img->origin;

dst1->widthStep = img->widthStep;
dst2->widthStep = img->widthStep;

dst1->imageData = img->imageData+10*img->widthStep+5*img->nChannels;
dst2->imageData = img->imageData+60*img->widthStep+50*img->nChannels;

cvNot(dst1,dst1);
cvNot(dst2,dst2);

cvNamedWindow( "Source", CV_WINDOW_AUTOSIZE );
cvShowImage( "Source", img );
cvWaitKey();
cvReleaseImageHeader(&dst1);
cvReleaseImageHeader(&dst2);
return 0;
}

7.

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main(int argc, char** argv)
{
IplImage *img;
IplImage *clone1,*clone2;
img = cvLoadImage("lena.bmp",1);
IplImage* dst = cvCreateImage(cvGetSize(img),img->depth,1);
cvSplit( img, dst, 0, 0, 0 );
    clone1 = cvCloneImage(dst);
clone2 = cvCloneImage(dst);
double max,min;
cvMinMaxLoc(dst,&min,&max);
double thresh = (max-min)/2;

cvSet(clone1,cvScalar(thresh));
cvZero(clone2);
cvCmp(dst,clone1,clone2,CV_CMP_GE);
cvSubS(dst,cvScalar(thresh/2),dst,clone2);

cvNamedWindow( "Source", CV_WINDOW_AUTOSIZE );
cvShowImage( "Source", dst );
cvWaitKey();
cvReleaseImage(&dst);

return 0;
}

8.

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

struct my_struct{
int Value;
CvPoint point;
CvRect rect;
};

void write_my_struct(CvFileStorage* fs,const char* name,my_struct* ms)
{
cvWriteInt(fs,name,ms->Value);

cvStartWriteStruct(fs,name,CV_NODE_SEQ);
cvWriteInt(fs,0,ms->point.x);
cvWriteInt(fs,0,ms->point.y);
cvEndWriteStruct(fs);

cvStartWriteStruct(fs,name,CV_NODE_SEQ);
cvWriteInt(fs,0,ms->rect.x);
cvWriteInt(fs,0,ms->rect.y);
cvWriteInt(fs,0,ms->rect.width);
cvWriteInt(fs,0,ms->rect.height);
cvEndWriteStruct(fs);

}

void read_my_struct(CvFileStorage* fs,CvFileNode* ms_node,my_struct* ms)
{
int value = cvReadIntByName(fs,0,"Value",5);

CvSeq* s = cvGetFileNodeByName(fs,0,"point")->data.seq;
int point_x = cvReadInt((CvFileNode*)cvGetSeqElem(s,0));
int point_y = cvReadInt((CvFileNode*)cvGetSeqElem(s,1));

}
int main(int argc, char** argv)
{
CvFileStorage* fs = cvOpenFileStorage(
   "cfg.xml",
   0,
   CV_STORAGE_WRITE
   );
my_struct ms = {10,cvPoint(10,20),cvRect(0,0,20,50)};

   write_my_struct(fs,"struct",&ms);
cvReleaseFileStorage(&fs);

return 0;
}

生成的cfg.xml文件

<?xml version="1.0" ?>
- <opencv_storage>
<struct>10</struct>
<struct>10 20</struct>
<struct>0 0 20 50</struct>
</opencv_storage>


转载于:http://hi.baidu.com/loop_k/blog/item/3decda576934fccfb645ae7d.html

你可能感兴趣的:(struct,null,DST)