【图像处理】c++使用opencv读取图片
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
学习目标:学习使用c++对图片进行读取等操作。
代码理解
using namespace cv;
解释:加入此代码,后面就不需要在函数前加入cv:: 如从cv::imread(),可以直接写成imread()
int main(int argc, char** argv)
{
return 0;
}
解释:
// argc是命令行,总的参数个数; argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数命令行后面跟的用户输入的参数
// char *argv[] 用来取得你所输入的参数
// main(int argc, char **argv)和main(int argc, char *argv[])一个意思
// int argc是输入在命令行上的参数(字符串)个数;
// *argv[]是指向字符串的指针数组,即数组元素是指向输入在命令行上的每个参数(字符串)的指针。
image = imread("./1.jpg");
解释:读取图片,其路径为相对路径,图片放在与.cpp相同路径下
imshow("meinv", image);
waitKey(0);
解释: 显示图片,加入waitKey(0)是防止图片出现之后马上自动消失。
for (size_t y = 0; y < image.rows; y++)
{
return 0;
}
解释:size_t是一种数据相关的无符号类型,它被设计得足够大以便能够存储内存中对象的大小。
unsigned char* row_ptr = image.ptr<unsigned char>(y);
解释:
获取行指针,之所以用char的原因是因为颜色值是1-256用char能放得下
ptr是pointer的缩写,是一个特殊的变量,它里面存储的数值被解释为内存里的一个地址。
全部代码
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat image;
image = imread("./1.jpg");
if (image.data == nullptr) //nullptr是c++11新出现的空指针常量
{
cout << "图片文件不存在" << endl;
}
else
{
//显示图片
imshow("meinv", image);
waitKey(0);
}
// 输出图片的基本信息
cout << "图像宽为:" << image.cols << "\t高度为:" << image.rows << "\t通道数为:" << image.channels() << endl;
// 遍历每个像素
//之所以用y这个名字表示行 是因为图片的坐标系中行号就是y
for (size_t y = 0; y < image.rows; y++)
{
unsigned char* row_ptr = image.ptr<unsigned char>(y);
for (size_t x = 0; x < image.cols; ++x) {
//这是获得像素数据数组的头指针,注意像素数据可能会有多个通道所以才需要用数组存储
unsigned char* data_ptr = &row_ptr[x * image.channels()];
//对当前像素逐个通道输出颜色值
for (int i = 0; i < image.channels(); ++i) {
cout << int(data_ptr[i])<<endl;
}
}
}
system("pause");
return 0;
}
读取结果
参考
GitHub 加速计划 / opencv31 / opencv
144
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9
Animated WebP Support #25608
related issues #24855 #22569
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2 天前
09030615
V4l default image size #25500
Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution.
Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499)
Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551)
This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin.
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [X] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2 天前
更多推荐
已为社区贡献10条内容
所有评论(0)