图像的深度转换,图像提取,像素级访问及时间测量

/*
图像深度转换
像素级访问
测量时间
剪裁图像
*/
#include<opencv2\opencv.hpp>
#include<cv.h>
using namespace cv;
using namespace std;

int main() {
	double t = (double)getTickCount();
	Mat img_org = imread("D://图片//5.jpg");
	Mat img_tem;
	Mat tem = Mat(300, 400, CV_8UC1);    //创建一个单通道图片
	double elapsed;

	if (img_org.empty())
		cout << "图片加载失败!" << endl;

	img_org.convertTo(img_tem, CV_32F);     //图像深度转换

	randu( tem, 0, 1000);    //产生随机值
	double maxvalue, minvalue;
	Point p1, p2;

	minMaxLoc(tem, &minvalue, &maxvalue, &p1, &p2);    //只能从单通道中找到最大最小值以及它们的位置
	cout << "min_position = " << p1 << endl;
	cout << "max_position = " << p2 << endl;
	line(tem, p1, p2, 0, 2);   //在最大最小值间画一条连线,单色
	circle(tem, p1, 155, 0, 2);   //画圆观察

	Mat result;
	img_org.copyTo(result);
	Point center(200, 100);

	Mat mask(size(img_org), CV_8UC3, Scalar(0,0,0));    //遮罩
	circle(mask, center, 150, Scalar(255,255,255), -1);    //白色显示黑色不显示,-1表示全部填充
	bitwise_and(img_org, mask, result);        //三者要有相同的大小,mask要为8-bit单通道


	for (int i = 0; i < result.rows; i++) {        //用函数ptr访问图中每个像素的值,当mask是黑色时改变result的像素值,使图片脱离出来
		Vec3b* pixrow = result.ptr<Vec3b>(i);
		Vec3b* pixrow_tem = mask.ptr<Vec3b>(i);
		for (int j = 0; j < result.cols; j++) {
			if (pixrow_tem[j][0] == 0 && pixrow_tem[j][1] == 0 && pixrow_tem[j][2] == 0) {   //当mask的三个通道全为0时,改变其颜色
				pixrow[j][0] = 66;
				pixrow[j][1] = 55; 
				pixrow[j][2] = 0;
			}
		}
	}


	imshow("show_org", img_org);
	imshow("show_tem", img_tem);
	imshow("show_randu", tem);
	imshow("show_mask", mask);
	imshow("show_result", result);

	elapsed = ((double)getTickCount() - t) / getTickFrequency();     //单位为秒
	cout << "t2 = " << elapsed << endl;

	waitKey(0);

	destroyAllWindows;
	return 0;
}

运行结果:

图像的深度转换,图像提取,像素级访问及时间测量_第1张图片

你可能感兴趣的:(图像的深度转换,图像提取,像素级访问及时间测量)