The Zen of Python,by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

为图像添加椒盐噪声的例子

#include <iostream> 
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <random>

using namespace std;

void salt(cv::Mat image, int n) {

  // C++11的随机数生成器
  std::default_random_engine generator;
  std::uniform_int_distribution<int>
                randomRow(0, image.rows - 1);
  std::uniform_int_distribution<int>
                randomCol(0, image.cols - 1);

  int i,j;
  for (int k=0; k<n; k++) {

    // 随机生成图形位置
    i= randomCol(generator);
    j= randomRow(generator);
    if (image.type() == CV_8UC1) { // 灰度图像
      // 单通道8位图像
      image.at<uchar>(j,i)= 255;
    } else if (image.type() == CV_8UC3) { // 彩色图像
      // 3通道图像
      image.at<cv::Vec3b>(j,i)[0]= 255;
      image.at<cv::Vec3b>(j,i)[1]= 255;
      image.at<cv::Vec3b>(j,i)[2]= 255;
    }
  }
}

int main()
{
    // 打开图像
    cv::Mat image= cv::imread("test.jpg",1);
    // 调用函数以添加噪声
    salt(image,3000);
    // 显示结果
    cv::imshow("Image",image);
    cv::waitKey(0);
}

这是OpenCV计算机视觉编程中的一个例子,在这段代码中涉及的知识。

继续阅读“为图像添加椒盐噪声的例子”

图像编程入门理论与操作

#include <iostream>
//核心功能模块
#include <opencv2/core.hpp>
// 读写图像和视频的函数以及一些用户交互函数模块
#include <opencv2/highgui.hpp>
// 包含主要的图像处理函数模块
#include <opencv2/imgproc.hpp>

using namespace std;

int main()
{
	// 创建一个空图像对象
	cv::Mat image;
	/*
	 * @param const cv:String 文件名称
	 * @param int flags 读入图像时转换成三通道彩色图像或灰度图
	 */
	image= cv::imread("test.bmp", cv::IMREAD_GRAYSCALE);
        // 等待按键
	cv::waitKey(0);

}
继续阅读“图像编程入门理论与操作”