//Exercises at end of Chapter 5,《learning OpenCV3》
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void help(const char **argv) {
    cout << "\n\n"
        << "This program solves the Exercises at the end of Chapter 5 \n"
        << "Call:\n"
        << argv[0] << " <path/image_name>\n\n"
        << "For example: " << argv[0] << " / test.jpg\n"
        << endl;
}
int main( int argc, const char** argv )
{
    help(argv);
    if(argc < 2) {
        cout << "\nERROR: You had too few parameters.\n" << endl;
        return -1;
    }
    /************************************************************************/
    /*5.1. Drawing practice: load or create and display a color image. Draw one example of
    every shape and line that OpenCV can draw.                           */
    /************************************************************************/
    Mat src = imread("e:/template/lena.jpg");
    cv::circle(src,Point(100,100),100,Scalar(255,255,255),2); //circle
    cv::rectangle(src,Point(0,0),Point(300,300),Scalar(255,255,255),2);//rectangle
    cv::line(src,Point(0,0),Point(300,300),Scalar(255,255,255),2);//line
    cv::ellipse(src,cv::Point(100,100),Size(100,100),45,0,180,Scalar(255,0,0),2);//ellipse
    /************************************************************************/
    /*5.2. Grayscale: load and display a color image.
    a. Turn it into three-channel grayscale (it is still an BGR image, but it looks gray
    to the user).   
    b. Draw color text onto the image.*/
    /************************************************************************/
    //a
    Mat tmp;
    cvtColor(src,tmp,COLOR_BGR2GRAY);
    cvtColor(tmp,src,COLOR_GRAY2BGR);
    //b
    putText(src,"puttext",Point(50,30),CV_FONT_HERSHEY_DUPLEX,1.0f,Scalar(0,255,0));
    /************************************************************************/
    /*5.5. Use cv::LineIterator to count pixels on different line segments in, say, a 300 × 300 image.
    a. At what angles do you get the same number of pixels for 4-connected and
    8-connected lines?
    b. For line segment angles other than the above, which counts more pixels:
    4-connected or 8-connected lines?
    c. For a given line segment, explain the difference in the length of the line compared 
    to the number of pixels you count iterating along the line for
    both 4-connected and 8-connected? Which connectedness is closer to the true
    line length?
    /************************************************************************/
    //a、
    LineIterator it_4_x(src, Point(0,0), Point(0,100), 4);
    LineIterator it_8_x(src, Point(0,0), Point(0,100), 4);
    LineIterator it_4_y(src, Point(0,0), Point(100,0), 4);
    LineIterator it_8_y(src, Point(0,0), Point(100,0), 4);
    cout << "it_4_x " <<it_4_x.count<<" it_8_x "<<it_8_x.count<<endl;
    cout << "it_4_y " <<it_4_y.count<<" it_8_y "<<it_8_y.count<<endl;
    //b the answer is: 4-connected counts more pixels than 8-connected counts
    LineIterator it_4(src, Point(0,0), Point(100,100), 4);
    LineIterator it_8(src, Point(0,0), Point(100,100), 8);
    cout << "it_4 " <<it_4.count<<" large than it_8 "<<it_8.count<<endl;
    //c 
    // the difference is the same as the difference between 4-connected and 8-connected
    // I veleve the 8-connected is closer to the true line length. 
    waitKey();
    return 0;
}

《学习OpenCV3》第6章课后习题的更多相关文章

  1. 20145330《Java学习笔记》第一章课后练习8知识总结以及IDEA初次尝试

    20145330<Java学习笔记>第一章课后练习8知识总结以及IDEA初次尝试 题目: 如果C:\workspace\Hello\src中有Main.java如下: package cc ...

  2. 视觉slam十四讲第七章课后习题6

    版权声明:本文为博主原创文章,转载请注明出处: http://www.cnblogs.com/newneul/p/8545450.html 6.在PnP优化中,将第一个相机的观测也考虑进来,程序应如何 ...

  3. 视觉slam十四讲第七章课后习题7

    版权声明:本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/newneul/p/8544369.html  7.题目要求:在ICP程序中,将空间点也作为优化变量考虑进来 ...

  4. 关于《Java读书笔记》第六章课后习题选择题总结与疑问

    课后习题 选择题 3 题 代码: class Some{ String ToString(){ return "Some instance"; } } public class M ...

  5. C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂

    C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...

  6. 视觉slam十四讲第8章课后习题3+稀疏直接法程序注释

    版权声明:本文为博主原创文章,转载请注明出处: http://www.cnblogs.com/newneul/p/8571653.html 3.题目回顾:在稀疏直接法中,假设单个像素周围小块的光度也不 ...

  7. 《学习OpenCV3》第14章课后习题

    1.在一条含有 N 个点的封闭轮廓中,我们可以通过比较每个点与其它点的距离,找出最外层的点.(这个翻译有问题,而且这个问题是实际问题) a.这样一个算法的复杂度是多少? b.怎样用更快的速度完成这个任 ...

  8. Perl 中级教程 第5章课后习题

    5. 9. 1. 练习1 [5 分钟] 先不要运行程序, 看看你能否判断出这程序的哪部份出了问题?如果你看不出 来, 就可以运行一相程序, 得到些暗示, 来看是否能修改好: my %passenger ...

  9. 中级Perl 第三章课后习题

    3. 10. 1. 练习1 [25 分钟] 读当前目录的文件列表并转换成全路径.不能用shell 命令或外部程序读当前目 录.Perl 的File::Spec 和Cwd 两个模块对这个程序有帮助.每个 ...

随机推荐

  1. eclipse的调试模式以及断点运行

    先在代码序号处打个点. 点击debug调试模式 然后再点击 如果有打断点,则会在断点处结束,如果没有,会一直一步一步向下直至结束.

  2. ES6之字符串扩展

    ES6字符串新增的常用方法: 1. includes(): 字符串中是否包含某个字符或字符串, 包含的两个字符必须是相连的 let str = 'hello, world' str.includes( ...

  3. C#什么时候需要使用构造函数

    只要使用 new 运算符实例化对象,并且不为 new 提供任何参数,就需要调用默认构造函数.除非类是 static 的,否则 C# 编译器将为无构造函数的类提供一个公共的默认构造函数,以便该类可以实例 ...

  4. VS2013打包程序步骤

    VS自带的打包程序默认是没有安装的,如果有打包的需要,需要自己去下载一个安装程序  1.右击解决方案,选择添加项目,在打开的对话框中找到[已安装]-[模板]-[其他项目]-[安装和部署],如图示.第一 ...

  5. 32网络通信之Poll模型

    多路复用并发模型  -- poll #include<poll.h> int  poll(struct pollfd *fds,  unsigned int nfds, int timeo ...

  6. map 的用法

    #include<iostream> #include<map> #include<string> #define s second #define f first ...

  7. poj3074 DLX精确覆盖

    题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...

  8. Qt 事件机制

    [1]事件 事件是可以被控件识别的操作.如按下确定按钮.选择某个单选按钮或复选框. 每种控件有自己可识别的事件,如窗体的加载.单击.双击等事件,编辑框(文本框)的文本改变事件等等. 事件就是用户对窗口 ...

  9. linux 远程执行命令

    命令: ssh 命令参数: -l 指定登入用户 -p 设置端口号 -f 后台运行,并推荐加上 -n 参数 -n 将标准输入重定向到 /dev/null,防止读取标准输入 -N 不执行远程命令,只做端口 ...

  10. linux 开始

    3306 -- mysql 8000--django默认 服务由端口控制 https -- 443 http -- 80 linux发行版:1.centos 免费版的redhat2.ubuntu 乌版 ...