学习opencv3--第七章答案

1.

#include 
#include 

using namespace std;

int main(int argc, const char** argv)
{
	cv::Mat src;
	src = cv::imread("C:/Users/duoduoda/Desktop/dog.jpg");
	cv::imshow("input",src);
	//a.
	cv::RNG rng(12345);
	float f_a = rng.uniform(0.0, 1.0);
	float f_b = rng.uniform(0.0, 1.0);
	float f_c = rng.uniform(0.0, 1.0);
	cout << "float a:" << f_a << "  float b:" << f_b << "  float c:" << f_c << endl;
	//b.
	double ff_a = rng.gaussian(1.0);
	double ff_b = rng.gaussian(1.0);
	double ff_c = rng.gaussian(1.0);
	cout << "double a:" << ff_a << "  double b:" << ff_b << "  double c:" << ff_c << endl;
	//c.
	uchar b_a = (uchar)rng;
	uchar b_b = (uchar)rng;
	uchar b_c = (uchar)rng;
	//static_cast是c++中的强制类型转换
	cout << "uchar a:" << static_cast(b_a) << "  uchar b:" << static_cast(b_b) << "  uchar c:" << static_cast(b_c) << endl;
	cv::waitKey(0);
	return 0;
}

2.


#include 
#include 

using namespace std;

int main(int argc, const char** argv)
{
	cv::Mat src;

	cv::RNG rng(12345);
	//a.
	cv::Mat m1 = cv::Mat::zeros(20, 1, CV_32F);
	rng.fill(m1, cv::RNG::UNIFORM, 0.0, 1.0);
	cout << m1 << endl;
	//b.
	cv::Mat m2 = cv::Mat::zeros(20, 1, CV_32F);
	rng.fill(m2, cv::RNG::NORMAL, 0.0, 1.0);
	cout << m2 << endl;
	//c.
	cv::Mat m3 = cv::Mat::zeros(20, 1, CV_8UC1);
	rng.fill(m3, cv::RNG::UNIFORM, 0, 255);
	cout << m3 << endl;
	//d.
	cv::Mat m4 = cv::Mat::zeros(20, 1, CV_8UC3);
	rng.fill(m4, cv::RNG::UNIFORM, 0, 255);
	cout << m4 << endl;

	cv::waitKey(0);
	return 0;
}

 

你可能感兴趣的:(opencv)