代码在VS2008下通过,要在附加依赖项中添加:opencv_core220d.lib opencv_highgui220d.lib opencv_imgproc220d.lib。

也可以在代码里面添加以下内容:

  1. #pragma comment(lib,"opencv_core220d.lib")
  2. #pragma comment(lib,"opencv_highgui220d.lib")
  3. #pragma comment(lib,"opencv_imgproc220d.lib")
#pragma comment(lib,"opencv_core220d.lib")
#pragma comment(lib,"opencv_highgui220d.lib")
#pragma comment(lib,"opencv_imgproc220d.lib") 

具体可见这篇文章:配置环境

Chapter 3:

2、This exercise will accustom you to the idea of many functions taking matrix types.Create a two-dimensional matrix with three channels of type byte with data size 100-by-100. Set all the values to 0.
a. Draw a circle in the matrix using void cvCircle( CvArr* img, CvPoint center,
intradius, CvScalar color, int thickness=1, int line_type=8, int shift=0 ).
b. Display this image using methods described in Chapter 2.

Solution:

#include "opencv\cv.h"
#include "opencv\highgui.h"int main(){CvMat* mat = cvCreateMat(100,100,CV_8UC3);cvZero(mat);cvCircle(mat,cvPoint(50,50),30,CV_RGB(255,0,0),2,8);cvNamedWindow("xiti_ex3_2",CV_WINDOW_AUTOSIZE);cvShowImage("xiti_ex3_2",mat);cvWaitKey(0);//cvReleaseImage(&mat);cvDestroyWindow("xiti_ex3_2");
}

Test:

3、Create a two-dimensional matrix with three channels of type byte with data size 100-by-100, and set all the values to 0. Use the pointer element access function cvPtr2D to point to the middle (“green”) channel. Draw a green rectangle between (20, 5) and (40, 20).

Solution:

#include "opencv\cv.h"
#include "opencv\highgui.h"int main(){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);cvDestroyWindow("drawRectangle");
}

Test:

4、Create a three-channel RGB image of size 100-by-100. Clear it. Use pointer arithmetic to draw a green square between (20, 5) and (40, 20).

Solution:

#include "opencv\cv.h"
#include "opencv\highgui.h"int main(){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");
}

Test:

5、Practice using region of interest (ROI). Create a 210-by-210 single-channel byte image and zero it. Within the image, build a pyramid of increasing values using ROI and cvSet(). Th at is: the outer border should be 0, the next inner border should be 20, the next inner border should be 40, and so on until the fi nal innermost square is set to value 200; all borders should be 10 pixels wide. Display the image.

Solution:

#include "opencv\cv.h"
#include "opencv\highgui.h"int main(){IplImage* img = cvCreateImage(cvSize(210,210),IPL_DEPTH_8U,1);cvZero(img);int x = 0;int y = 0;int scalar = 0;int add = 0;while(add < 200){cvSetImageROI(img,cvRect(x,y,210 - add,10));cvSet(img,cvScalar(scalar));//赋值x += 10;y += 10;scalar += 20;add += 20;cvResetImageROI(img);}cvNamedWindow("Example",CV_WINDOW_AUTOSIZE);cvShowImage("Example",img);cvWaitKey(0);cvReleaseImage(&img);cvDestroyWindow("Example");
}

Test:

6、Use multiple image headers for one image. Load an image that is at least 100-by-100. Create two additional image headers and set their origin, depth, number of channels,and widthstep to be the same as the loaded image. In the new image headers,set the width at 20 and the height at 30. Finally, set their imageData pointers to point to the pixel at (5, 10) and (50, 60), respectively. Pass these new image subheaders to cvNot(). Display the loaded image, which should have two inverted rectangles
within the larger image.、

Solution:

#include "opencv\cv.h"
#include "opencv\highgui.h"int main(){IplImage* img = cvLoadImage("HUST.jpg");IplImage* img1;IplImage* img2;img1 = cvCreateImageHeader(cvSize(20,30),img ->depth,img ->nChannels);img1 ->origin = img ->origin;img1 ->widthStep = img ->widthStep;img2 = cvCreateImageHeader(cvSize(20,30),img ->depth,img ->nChannels);img2 ->origin = img ->origin;img2 ->widthStep = img ->widthStep;img1 ->imageData = img ->imageData + 10 * img ->widthStep +5 *  img ->nChannels;img2 ->imageData = img ->imageData + 60 * img ->widthStep +50 *  img ->nChannels;cvNot(img1,img1);cvNot(img2,img2);cvNamedWindow("Example",CV_WINDOW_AUTOSIZE);cvShowImage("Example",img);cvWaitKey(0);cvReleaseImage(&img1);cvReleaseImage(&img2);cvReleaseImage(&img);cvDestroyWindow("Example");
}

Test:

7、Create a mask using cvCmp(). Load a real image. Use cvSplit() to split the image into red, green, and blue images.
a. Find and display the green image.
b. Clone this green plane image twice (call these clone1 and clone2).
c. Find the green plane’s minimum and maximum value.
d. Set clone1’s values to thresh = (unsigned char)((maximum - minimum)/2.0).
e. Set clone2 to 0 and use cvCmp(green_image, clone1, clone2, CV_CMP_GE). Now clone2 will have a mask of where the value exceeds thresh in the green image.

f. Finally, use cvSubS(green_image,thresh/2, green_image, clone2) and display the results.

Solution:

#include "opencv\cv.h"
#include "opencv\highgui.h"
#include <iostream>using namespace std;int main(){IplImage* img = cvLoadImage("HUST.jpg");//显示原始图像cvNamedWindow("img_pre",CV_WINDOW_AUTOSIZE);cvShowImage("img_pre",img);//创建三通道图像IplImage* imgRed = cvCreateImage(cvSize(img ->width,img ->height),img ->depth,1);IplImage* imgGreen = cvCreateImage(cvSize(img ->width,img ->height),img ->depth,1);IplImage* imgBlue = cvCreateImage(cvSize(img ->width,img ->height),img ->depth,1);//分离出R、G、B三通道图像cvSplit(img,imgGreen,imgBlue,imgRed,NULL);//显示绿色图像cvNamedWindow("img_Green",CV_WINDOW_AUTOSIZE);cvShowImage("img_Green",imgGreen);//克隆绿色图像两次IplImage* imgGreenClone1 = cvCloneImage(imgGreen);IplImage* imgGreenClone2 = cvCloneImage(imgGreen);double MaxNum,MinNum;cvMinMaxLoc(imgGreen,&MinNum,&MaxNum);cout<<"MinNum in imgGreen:"<<MinNum<<endl;cout<<"MaxNum in imgGreen:"<<MaxNum<<endl;double scalar = (MaxNum - MinNum)/2;cvSet(imgGreenClone1,cvScalar(scalar));cvZero(imgGreenClone2);cvCmp(imgGreen,imgGreenClone1,imgGreenClone2,CV_CMP_GE);cvSubS(imgGreen,cvScalar(scalar / 2),imgGreen,imgGreenClone2);cvNamedWindow("imgGreen_after",CV_WINDOW_AUTOSIZE);cvShowImage("imgGreen_after",imgGreen);cvWaitKey(0);cvReleaseImage(&img);cvReleaseImage(&imgGreen);cvDestroyWindow("img_pre");cvDestroyWindow("img_Green");cvReleaseImage(&imgGreenClone2);cvDestroyWindow("imgGreen_after");
}

Test:

8、Create a structure of an integer, a CvPoint and a CvRect; call it “my_struct”.
Write two functions: void write_my_struct( CvFileStorage * fs, const char * name, my_struct *ms) and void read_my_struct( CvFileStorage* fs, CvFileNode* ms_node, my_struct* ms ). Use them to write and read my_struct.

Solution:

#include "opencv\cv.h"
#include "opencv\highgui.h"
#include <iostream>using namespace std;typedef struct{int i;CvPoint pt;CvRect rect;
}my_struct;void write_my_struct(CvFileStorage *fs,const char *name,my_struct *ms){assert(fs != 0);cvWriteInt(fs,"i",ms ->i);cvStartWriteStruct(fs,"pt",CV_NODE_SEQ);cvWriteInt(fs,0,ms ->pt.x);cvWriteInt(fs,0,ms ->pt.y);cvEndWriteStruct(fs);cvStartWriteStruct(fs,"rect",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){assert(fs != 0);ms ->i = cvReadIntByName(fs,0,"i",0);CvSeq* s = cvGetFileNodeByName(fs,0,"pt") ->data.seq;ms ->pt.x = cvReadInt((CvFileNode*)cvGetSeqElem(s,0)); ms ->pt.y = cvReadInt((CvFileNode*)cvGetSeqElem(s,1)); CvSeq* s1 = cvGetFileNodeByName(fs,0,"rect") ->data.seq;ms ->rect.x = cvReadInt((CvFileNode*)cvGetSeqElem(s1,0)); ms ->rect.y = cvReadInt((CvFileNode*)cvGetSeqElem(s1,1)); ms ->rect.width = cvReadInt((CvFileNode*)cvGetSeqElem(s1,2));ms ->rect.height = cvReadInt((CvFileNode*)cvGetSeqElem(s1,3));//输出读取的结果cout<<"my_struct:"<<endl;cout<<"i:"<<ms ->i<<endl;cout<<"pt:("<<ms ->pt.x<<","<<ms ->pt.y<<")"<<endl;cout<<"rect:("<<ms ->rect.x<<","<<ms ->rect.y<<","<<ms ->rect.width<<","<<ms ->rect.height<<endl;
}int main(){//往xml文件中写数据CvFileStorage* fs = cvOpenFileStorage("cfg.xml",0,CV_STORAGE_WRITE);my_struct ms;ms.i = 100;ms.pt.x = 10;ms.pt.y = 20;ms.rect.x = 30;ms.rect.y = 40;ms.rect.width = 50;ms.rect.height = 60;write_my_struct(fs,NULL,&ms);cvReleaseFileStorage(&fs);//往xml文件中读数据CvFileStorage* fs1 = cvOpenFileStorage("cfg.xml",0,CV_STORAGE_READ);my_struct ms1;read_my_struct(fs1,NULL,&ms1);cvReleaseFileStorage(&fs1);
}

Test:

《学习OpenCV》课后习题解答(第三章)(仅供参考)相关推荐

  1. 《学习OpenCV》课后习题解答(第四章)(仅供参考)(不断更新)

    代码在VS2008下通过,要在附加依赖项中添加:opencv_core220d.lib opencv_highgui220d.lib opencv_imgproc220d.lib.也可以在代码里面添加 ...

  2. 《机器学习》周志华课后习题答案——第三章 (1-7题)

    <机器学习>周志华课后习题答案--第三章 (1-7题) 文章目录 <机器学习>周志华课后习题答案--第三章 (1-7题) 一.试析在什么情形下式(3.2)中不必考虑偏置项b. ...

  3. 【考研复习】《操作系统原理》孟庆昌等编著课后习题+答案——第三章

    前言 此书在最后的附录B中,有给出部分重难点部分的参考答案.会在最后放上图片.如果想要此书习题答案,可点以下链接:为一个压缩包,以图片形式,习题图片按章节排序,答案图片按书页排序. <操作系统原 ...

  4. 周志华《机器学习》课后习题(第三章):线性模型

    作者 | 我是韩小琦 链接 | https://zhuanlan.zhihu.com/p/43270830 3.1 试分析在什么情况下,在以下式子中不比考虑偏置项b. 答: 在样本  中有某一个属性  ...

  5. Think Python读书笔记及课后习题---【前三章】

    昨天明明可以靠脸吃饭却偏偏要靠实力吃饭的班主任给我推荐了<ThinPython><ThinPython><ThinPython>这本书,于是乎我便开始了我的啃书之路 ...

  6. 微型计算机原理与接口技术(周荷琴 冯焕清)第六版 课后习题答案 第三章(部分答案)

    第三章 1.分别说明下列指令的源操作数和目的操作数各采用什么寻址方式. 源操作数  目的操作数            源操作数                    目的操作数 (1)MOV AX, ...

  7. Java语言程序设计基础篇(第十版 梁勇著)课后习题答案 - 第三章

    第三章:选择 复习题 3.1 列出 6 个关系操作符. 解: >,<,=,>=,<=,!= 3.2 假设 x 等于 1,给出下列布尔表达式的结果: (x > 0) (x ...

  8. python教材答案第四章_python核心编程课后习题解答第四章

    4–1. Python 对象.与所有Python 对象有关的三个属性是什么?请简单的描述一下. type.ID.value..(身份.类型.值) type()接受一个对象作为参数,并返回它的类型 id ...

  9. 曼昆《经济学原理》(第五版)习题解答 第三章 相互依存性与贸易的好处

    1 .在什么情况下,生产可能性曲线是直线,而不是外凸的? 答:生产可能性曲线会由于机会成本的动态变化而呈现不同形状.在机会成本不变的情况下,生产可能性曲线是一条直线;机会成本递增的时候,生产可能性曲线 ...

最新文章

  1. 使用Python和OpenCV构建图像金字塔
  2. 深大计算机与科学,陆楠 - 深圳大学 - 计算机与软件学院
  3. 激励理论在人力资源管理中的运用
  4. android 6.0 数据库权限,Android超清晰6.0权限申请AndPermission
  5. liunx的urandom生成随机字符
  6. ps读写ddr3里面的数据 zynq_Zynq:用PS控制DDR3内存读写
  7. Centos 7上启动 vsftp报错处理
  8. Windows Server 2003 R2中的DFS复制与管理
  9. oracle为表空间增加数据文件_只读数据文件损坏恢复实验记录
  10. 苹果游戏开发教程之如何使用 SpriteKit 和 GameplayKit 制作你的街机手机游戏
  11. 微信小程序图片宽高自适应
  12. {最强实用}手机衩偷了,可以用这招轻易取回来(值得学习)
  13. 布隆过滤器与布谷鸟过滤器(经典版)
  14. 信息系统项目管理师考试教程(第3版)PDF
  15. java xml 小于等于_MyBatis中xml文件中的大于 大于等于 小于 小于等于 写法
  16. java email qq邮箱 与 阿里企业邮箱/个人邮箱
  17. [python] 基于Gradio可视化部署机器学习应用
  18. 1.5-20:球弹跳高度的计算
  19. 能上QQ,不能上网的解决方法
  20. Sheldon Numbers 暴力枚举

热门文章

  1. 使用Python 正则匹配两个特定字符之间的字符方法
  2. HDFS文件系统存储机制
  3. docker 使用技巧
  4. elementUI日期选择器:仅设置可选择时间区间
  5. Thinkphp内核无限坐席在线客服系统源码
  6. HTML5博客个人日志记录网页模板
  7. 《深入理解计算机系统》(2) 信息的表示和处理
  8. FireFox不能直接调用event对象,FireFox需要通过函数参数传递事件对象
  9. 新俊飞六合一口红机完整源码
  10. THINKPAD T420(4180J4C)还是THINKPAD T420(4180PLC)好?