Opencv基本应用(读、显示、保存图像,读视频、摄像机,获取像素值)

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

//Opencv includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
int test0(){
	//Read images
	Mat color = imread("./Opencv4_images/lena.jpg");
	Mat gray = imread("./Opencv4_images/lena.jpg",IMREAD_GRAYSCALE);

	if (!color.data) //Check for invalid input
	{
		cout << "Could not open or find the image" << endl;
		return -1;
	}

	// Write images
	imwrite("lenaGray.jpg", gray);
	//Get same pixel with opencv function
	int myRow = color.cols - 1;
	int myCol = color.rows - 1;
	//使用at方法是一个模板函数,模板参数T定义了返回值类型,能获取到像素点。
	//Vec3b类型,是一个3个uchar组成的动态数组。
	Vec3b pixel = color.at<Vec3b>(myRow, myCol);
	cout << "Pixel value (B,G,R):( " << (int)pixel[0] << "," <<
		(int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
	//Show images
	imshow("Lena BGR", color);
	imshow("Lena Gray", gray);
	// Wait for any key press
	waitKey(0);
	return 0;
}
int test1(){
VideoCapture cap(0);//open the default camera
	if (!cap.isOpened())  //check if we succeeded
	{
		return -1;
	}
	namedWindow("VIdeo",1);
	for (;;)
	{
		Mat frame;
		cap >> frame; // get a new frame from camera
		if (frame.empty())
		{
			return 0;
		}
		imshow("Video",frame);
		if (waitKey(30) >= 0)
			break;
	}
	// Relese the camera or video cap
	cap.release();
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值