一、CMakeLists文件
cmake_minimum_required(VERSION
3.15)
project(imageBasics
)
set(CMAKE_CXX_STANDARD
14)
#寻找指定版本的OpenCV库
find_package(OpenCV REQUIRED
)
#添加OpenCV头文件
include_directories($
{OpenCV_INCLUDE_DIRS
})
add_executable(imageBasics main
.cpp
)
#链接OpenCV库
target_link_libraries( imageBasics $
{OpenCV_LIBS
})
二、.cpp文件
#include <iostream>
#include <chrono>
#include <opencv2/opencv.hpp>
using namespace std
;
int main(int argc
,char **argv
) {
cv
::Mat image
;
image
= cv
::imread(argv
[1]);
if (image
.data
== nullptr){
cerr
<< "文件" << argv
[1] << "不存在" << endl
;
return 0;
}
cout
<< "图像宽度为" << image
.cols
<< ",图像高度为" << image
.rows
<< endl
;
cv
::imshow("图像",image
);
cv
::waitKey(0);
if (image
.type() == CV_8UC3
){
cout
<< "图像是彩色图" << endl
;
}
else if (image
.type() == CV_8UC1
){
cout
<< "图像是灰度图" << endl
;
}
else {
cout
<< "图像不是符合要求" << endl
;
return 0;
}
chrono
::steady_clock
::time_point t1
= chrono
::steady_clock
::now();
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 c
= 0; c
<=image
.channels(); c
++){
unsigned char data
= data_ptr
[c
];
}
}
}
chrono
::steady_clock
::time_point t2
=chrono
::steady_clock
::now();
chrono
::duration
<double> time_used
= chrono
::duration_cast
<chrono
::duration
<double>> (t2
- t1
);
cout
<< "图像遍历用时" << time_used
.count() << "秒" << endl
;
cv
::Mat image_clone
= image
.clone();
image_clone(cv
::Rect(100,100,100,100)).setTo(255);
cv
::imshow("clone后原始图像",image
);
cv
::imshow("clone后clone图像",image_clone
);
cv
::waitKey(0);
cv
::Mat image_another
= image
;
image_another(cv
::Rect(100,100,100,100)).setTo(0);
cv
::imshow("直接赋值后原图像",image
);
cv
::imshow("直接赋值后赋值图像",image_another
);
cv
::waitKey(0);
cv
::destroyAllWindows();
std
::cout
<< "Hello, World!" << std
::endl
;
return 0;
}
转载请注明原文地址: https://lol.8miu.com/read-21134.html