opencv第三章习题参考

来源:互联网 发布:折弯机编程 编辑:程序博客网 时间:2024/05/18 22:08

习题做的比较马虎,见谅。

3.2

// opencv_3_2.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "highgui.h"
#include "cv.h"

int main(int argc, char* argv[])
{
CvMat* mat = cvCreateMat(100,100,CV_8UC3);
CvPoint center = {50,50};
int radius = 50;
CvScalar color = {{100,100,100,100}};
const int thickness = 1;
const int line_type = 8;
const int shift = 0;

cvZero(mat);
cvCircle( mat, center, radius, color,
              thickness, line_type, shift );
cvNamedWindow( "example", CV_WINDOW_AUTOSIZE );
cvShowImage( "example", mat );
cvWaitKey(0);
cvReleaseMat(&mat);
cvDestroyWindow("example");
return 0;
}


3.3

// opencv_3_3.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
CvMat* mat = cvCreateMat(100,100,CV_8UC3);
cvZero(mat);
int i,j;
for(i = 20;i < 40; ++i){
for(j = 5;j < 20; ++j){
uchar* p = cvPtr2D(mat,i,j);
p[1] = 255;
}
}
cvNamedWindow("drawRectangle",CV_WINDOW_AUTOSIZE);
cvShowImage("drawRectangle",mat);
cvWaitKey(0);
cvReleaseMat(&mat);
cvDestroyWindow("drawRectangle");
return 0;
}


3.4

// opencv_3_4.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
IplImage* img = cvCreateImage(
cvSize(100,100),
8,
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 + 1] = 255;
}
}
cvNamedWindow("drawRectangle",CV_WINDOW_AUTOSIZE);
cvShowImage("drawRectangle",img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("drawRectangle");
return 0;
}


3.5

// opencv_3_5.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
int i = 0;
int value = 0;
IplImage* img = cvCreateImage(
cvSize(210, 210),
8,
1);

cvZero(img);
for(i = 0; i < 201; i += 20)
{
cvSetImageROI(img, cvRect(i/2, i/2, 210-i, 210-i));
cvSet(img, cvScalar(value), NULL);
value = i;
cvResetImageROI(img);
}
cvNamedWindow("pyramid", 1);
cvShowImage("pyramid", img);
cvWaitKey();
return 0;
}

3.6

// opencv_3_6.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
IplImage* img = cvLoadImage("C:\\WINDOWS\\Web\\Wallpaper\\flower.jpg");
IplImage* img_rec1 = cvCreateImage(cvSize(20, 30), img->depth, img->nChannels);
IplImage* img_rec2 = cvCreateImage(cvSize(20, 30), img->depth, img->nChannels);

img_rec1->origin = img->origin;
img_rec2->origin = img->origin;
img_rec1->widthStep = img->widthStep;
img_rec2->widthStep = img->widthStep;
img_rec1->imageData = img->imageData + 10*img->widthStep + 5*3;
img_rec2->imageData = img->imageData + 60*img->widthStep + 50*3;
cvNot(img_rec1, img_rec1);
cvNot(img_rec2, img_rec2);
cvNamedWindow("window", CV_WINDOW_AUTOSIZE);
cvShowImage("window", img);
cvWaitKey();
cvReleaseImage(&img);
cvDestroyWindow("window");
return 0;
}



3.7

// opencv_3_7.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include "cv.h"
#include "highgui.h"
using namespace std;

int main(int argc, char* argv[])
{
IplImage* img = cvLoadImage("C:\\WINDOWS\\Web\\Wallpaper\\flower.jpg");
IplImage* img_red = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* img_green = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* img_blue = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* img_gray = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);

cvSplit(img, img_red, img_green, img_blue, NULL);

//a
//cvNamedWindow("img_green", CV_WINDOW_AUTOSIZE);
//cvShowImage("img_green", img_green);

//b
IplImage* clone1 = cvCloneImage(img_green);
IplImage* clone2 = cvCloneImage(img_green);

//c
double max = 0.0;
double min = 0.0;
cvMinMaxLoc(img_green, &min, &max);
cout<<"min in img_green: "<<min<<endl;
cout<<"max in img_green: "<<max<<endl;

//d
double scalar = (max - min)/2;
cvSet(clone1, cvScalar(scalar));

//e
cvZero(clone2);
cvCmp(img_green, clone1, clone2, CV_CMP_GE);

//f
cvSubS(img_green, cvScalar(scalar/2), img_green, clone2);
cvNamedWindow("img_green", CV_WINDOW_AUTOSIZE);
cvShowImage("img_green", img_green);

cvWaitKey();

cvReleaseImage(&img);
cvReleaseImage(&img_red);
cvReleaseImage(&img_green);
cvReleaseImage(&img_blue);
cvReleaseImage(&img_gray);
cvReleaseImage(&clone1);
cvReleaseImage(&clone2);

cvDestroyWindow("img_green");
return 0;
}


3.8

// opencv_3_8.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include "cv.h"
#include "highgui.h"
#include <string.h>
using namespace std;

typedef struct  
{
int i;
CvPoint point;
CvRect m;
}
my_struct;

void write_my_struct(CvFileStorage* fs, const char* name, my_struct* ms)
{
cvWriteInt(fs, "i", ms->i);
cvStartWriteStruct(fs, "cvPoint", CV_NODE_SEQ);
cvWriteInt(fs, 0, ms->point.x);
cvWriteInt(fs, 0, ms->point.y);
cvEndWriteStruct(fs);
cvStartWriteStruct(fs, "cvRect", CV_NODE_SEQ);
cvWriteInt(fs, 0, ms->m.x);
cvWriteInt(fs,0,ms->m.y);
cvWriteInt(fs,0,ms->m.width);
cvWriteInt(fs,0,ms->m.height);
cvEndWriteStruct(fs);
}

void read_my_struct(CvFileStorage* fs, CvFileNode* ms_node, my_struct* ms)
{
fs = cvOpenFileStorage("my_struct.xml", 0, CV_STORAGE_READ);
ms->i = cvReadIntByName(fs, 0, "i", 5);
CvSeq* s = cvGetFileNodeByName(fs, 0, "cvPoint")->data.seq;
ms->point.x = cvReadInt((CvFileNode*)cvGetSeqElem(s,0));
ms->point.y = cvReadInt((CvFileNode*)cvGetSeqElem(s,1));

CvSeq* q = cvGetFileNodeByName(fs, 0, "cvRect")->data.seq;
ms->m.height = cvReadInt((CvFileNode*)cvGetSeqElem(q,0));
ms->m.width = cvReadInt((CvFileNode*)cvGetSeqElem(q,1));
ms->m.x = cvReadInt((CvFileNode*)cvGetSeqElem(q,2));
ms->m.y = cvReadInt((CvFileNode*)cvGetSeqElem(q,3));

//cvReleaseFileStorage(&fs);
}

int main(int argc, char* argv[])
{
my_struct arr[10];
my_struct* p = NULL;
my_struct* q = NULL;
int i = 0;

for (i = 0; i < 10; i++)
{
arr[i].i = i;
arr[i].point.x = i;
arr[i].point.y = i + 1;
arr[i].m.height = 100;
arr[i].m.width = 100;
arr[i].m.x = i;
arr[i].m.y = i + 1;
}

//往xml文件中写数据
CvFileStorage* fs_w = cvOpenFileStorage(
"cfg.xml",
0,
CV_STORAGE_WRITE);
for (i = 0; i < 10; i++)
{
p = &arr[i];
write_my_struct(fs_w, NULL, p);

}
cvReleaseFileStorage(&fs_w);

//往xml文件中读数据
CvFileStorage* fs_r = cvOpenFileStorage(
"cfg.xml",
0,
CV_STORAGE_READ);
for (i = 0; i < 10; i++)
{
q = &arr[i];
read_my_struct(fs_w, NULL, q);
}
cvReleaseFileStorage(&fs_r);

return 0;
}

结果:

<?xml version="1.0"?>
<opencv_storage>
<i>0</i>
<cvPoint>
  0 1</cvPoint>
<cvRect>
  0 1 100 100</cvRect>
<i>1</i>
<cvPoint>
  1 2</cvPoint>
<cvRect>
  1 2 100 100</cvRect>
<i>2</i>
<cvPoint>
  2 3</cvPoint>
<cvRect>
  2 3 100 100</cvRect>
<i>3</i>
<cvPoint>
  3 4</cvPoint>
<cvRect>
  3 4 100 100</cvRect>
<i>4</i>
<cvPoint>
  4 5</cvPoint>
<cvRect>
  4 5 100 100</cvRect>
<i>5</i>
<cvPoint>
  5 6</cvPoint>
<cvRect>
  5 6 100 100</cvRect>
<i>6</i>
<cvPoint>
  6 7</cvPoint>
<cvRect>
  6 7 100 100</cvRect>
<i>7</i>
<cvPoint>
  7 8</cvPoint>
<cvRect>
  7 8 100 100</cvRect>
<i>8</i>
<cvPoint>
  8 9</cvPoint>
<cvRect>
  8 9 100 100</cvRect>
<i>9</i>
<cvPoint>
  9 10</cvPoint>
<cvRect>
  9 10 100 100</cvRect>
</opencv_storage>

参考:http://www.cnblogs.com/xiajun/archive/2011/08/23/2298652.html

《学习opencv中文版》

原创粉丝点击