〇、实战内容
- OpenCV helloworld
- Image的基本操作
- OpenCV 基本数据类型
- 遍历图片,读取图片的像素
- 图片反色
- 矩阵基本操作
1 OpenCV helloworld
1.1 文件结构类型
assign_1
build [cmake build所用]
assign_1.cpp
CMakeLists.txt
img.webp
图片地址
1.2 CMakeList.txt
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
project(assign1)
find_package(OpenCV 3 REQUIRED HINTS /usr/local/opt/opencv@3)
add_executable(assign1 assign_1.cpp)
target_link_libraries(assign1 ${OpenCV_LIBS})
- cmake 3.10版本
- 使用C++ 11
- project 名字为assign1
- find_package寻找opencv@3库
1.3 Helloworld
assign_1.cpp
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
Mat image = imread("/Users/..../computerphotography/course_zhengjiangdaxue/opencv-logo.png"); // 载入名为 "opencv-logo.png" 的图片
namedWindow("hello"); // 创建一个标题为 "hello" 的窗口
imshow("hello", result); // 在窗口 "hello" 中显示图片
waitKey(0); // 等待用户按下键盘
destroyWindow("hello"); // 销毁窗口 "hello"
return 0;
}
2. Image的基本操作
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
Mat image = imread("/Users/..../computerphotography/course_zhengjiangdaxue/opencv-logo.png"); // 载入名为 "opencv-logo.png" 的图片
cout << "image size 1: " << image.size() << endl;
cout << "image 行数: " << image.rows << endl;
cout << "image 列数: " << image.cols << endl;
cout << "image 通道数: " << image.channels() << endl;
cout << "image type: " << image.type() << endl;
return 0;
}
输出结果
image size 1: [200 x 200]
image 行数: 200
image 列数: 200
image 通道数: 3
image type: 16
3. OpenCV 基本数据类型
int main(int argc, char *argv[])
{
cout << "CV_8UC1:" << CV_8UC1 << endl;
cout << "CV_8UC2:" << CV_8UC2 << endl;
cout << "CV_8UC3:" << CV_8UC3 << endl;
cout << "CV_8UC4:" << CV_8UC4 << endl;
cout << "CV_8UC5:" << CV_8UC(5) << endl;
cout << "CV_8SC1:" << CV_8SC1 << endl;
cout << "CV_8SC2:" << CV_8SC2 << endl;
cout << "CV_8SC3:" << CV_8SC3 << endl;
cout << "CV_8SC4:" << CV_8SC4 << endl;
cout << "CV_8SC5:" << CV_8SC(5) << endl;
cout << "CV_16UC1:" << CV_16UC1 << endl;
cout << "CV_16UC2:" << CV_16UC2 << endl;
cout << "CV_16UC3:" << CV_16UC3 << endl;
cout << "CV_16UC4:" << CV_16UC4 << endl;
cout << "CV_16UC5:" << CV_16UC(5) << endl;
cout << "CV_16SC1:" << CV_16SC1 << endl;
cout << "CV_32SC1:" << CV_32SC1 << endl;
cout << "CV_32FC1:" << CV_32FC1 << endl;
cout << "CV_64FC1:" << CV_64FC1 << endl;
}
输出结果
CV_8UC1:0
CV_8UC2:8
CV_8UC3:16
CV_8UC4:24
CV_8UC5:32
CV_8SC1:1
CV_8SC2:9
CV_8SC3:17
CV_8SC4:25
CV_8SC5:33
CV_16UC1:2
CV_16UC2:10
CV_16UC3:18
CV_16UC4:26
CV_16UC5:34
CV_16SC1:3
CV_32SC1:4
CV_32FC1:5
CV_64FC1:6
- CV_8UC1 8字节无符号类型,通道为1
- CV_8UC3 8字节无符号类型,通道为3 即一个长度为3的数据例如[255,255,255] 三通道基本代表R, G, B
- image.type() == 16 == CV_8UC3 即改图片是3通道
- 单通道,增加一通道,值增加8
CV_8UC1->0 -> uchar
CV_8SC1->1 -> char
CV_16UC1->2 -> ushort
CV_16SC1->3 -> short
CV_32SC1->4 -> int
CV_32FC1->5 -> float
CV_64FC1->6 -> double
4. 读取图片的像素 & 遍历图片
4.1 获取制定像素
int main(int argc, char *argv[])
{
// 3. 获取某一个像素值
cout << "image at 0: " << image.at<Vec3b>(0) << endl;
cout << "image at 10000000: " << image.at<Vec3b>(10000000) << endl;
cout << "image at 39999: " << image.at<Vec3b>(39999) << endl;
cout << "image at 199,199: " << image.at<Vec3b>(199, 199) << endl;
}
输出:文章来源:https://www.toymoban.com/news/detail-669001.html
image at 0: [255, 255, 255]
image at 10000000: [0, 0, 0]
image at 39999: [255, 255, 255]
image at 199,199: [255, 255, 255]
- at方法
a. 需要制定对应的类型,单通道见Section3 说明;二通道Vec2b Vec2i Vec2f Vec2d
b. 参数可为1个,200 * 200 即 0<=index <=39999;参数为2个,则对应的行和列- 超出索引也可获取值
4.2 遍历图片
int main(int argc, char *argv[])
{
// //5. 遍历图片像素,方法1,便利,判断是白色,赋值为黑色
for(int i = 0;i<image.rows;i++){
for(int j=0;j<image.cols;j++){
if(image.at<Vec3b>(i,j) == white){
image.at<Vec3b>(i,j) = black;
}
}
}
}
5. 图片反色
5.1 方法1 :遍历
int main(int argc, char *argv[])
{
Vec3b white(255, 255, 255);
for(int i = 0;i<image.rows;i++){
for(int j=0;j<image.cols;j++){
image.at<Vec3b>(i,j) = white - image.at<Vec3b>(i,j);
}
}
}
- 定义白色Vec3b white(255, 255, 255);
- 遍历图片用white减去每个像素颜色
5.2 方法2 :矩阵减法
Mat m(image.rows,image.cols,CV_8UC3,Scalar(255,255,255));
image = m-image;
- Mat 代表opencv里的矩阵
- 初始化的时候传入行数,列数,每个像素的数据格式,以及初始值
a. 如果CV_8UC1 就是Scalar(255)
b. 如果CV_8UC2 就是Scalar(255, 255)- 初始化了一个CV_8UC3, 和原始图片一样大的矩阵,然后做减法
6. 矩阵基本运算
int main(){
Mat origin(10, 10, CV_32FC1, Scalar(0));
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (i == j)
{
cout << "i=" << i << "j=" << j << endl;
origin.at<float>(i, j) = 2.0;
}
else if ((i == j - 1) || (i == j + 1))
{
origin.at<float>(i, j) = -1.0;
}
}
}
// 矩阵 的逆
Mat invert = origin.inv();
cout << "origin mat:"<<endl;
print(origin);
cout << endl<<"invert mat:"<<endl;
print(invert);
//矩阵加法
cout << endl<< "add mat:"<<endl;
origin = origin+invert;
print(origin);
//矩阵乘法
cout << endl<< "multiply mat:"<<endl;
origin = origin*invert;
print(origin);
//初始化对角线
cout << endl<< "eye mat:"<<endl;
Mat eye = Mat::eye(10,10,CV_32FC1);
print(eye);
cout << endl<< "normalize mat:"<<endl;
Mat result;
//归一化,最大的位白色,最小的为黑色
normalize(invert, result, 1.0, 0.0, CV_MINMAX);
// 现实窗口逻辑
print(result);
cout << endl;
return 0;
}
输出结果:文章来源地址https://www.toymoban.com/news/detail-669001.html
origin mat:
[2, -1, 0, 0, 0, 0, 0, 0, 0, 0;
-1, 2, -1, 0, 0, 0, 0, 0, 0, 0;
0, -1, 2, -1, 0, 0, 0, 0, 0, 0;
0, 0, -1, 2, -1, 0, 0, 0, 0, 0;
0, 0, 0, -1, 2, -1, 0, 0, 0, 0;
0, 0, 0, 0, -1, 2, -1, 0, 0, 0;
0, 0, 0, 0, 0, -1, 2, -1, 0, 0;
0, 0, 0, 0, 0, 0, -1, 2, -1, 0;
0, 0, 0, 0, 0, 0, 0, -1, 2, -1;
0, 0, 0, 0, 0, 0, 0, 0, -1, 2]
invert mat:
[0.90909088, 0.81818181, 0.72727281, 0.63636357, 0.54545444, 0.45454538, 0.36363626, 0.27272728, 0.18181814, 0.090909071;
0.81818181, 1.6363636, 1.4545456, 1.2727271, 1.0909089, 0.90909076, 0.72727251, 0.54545456, 0.36363629, 0.18181814;
0.72727281, 1.4545456, 2.1818185, 1.9090908, 1.6363634, 1.3636361, 1.0909088, 0.81818181, 0.54545444, 0.27272722;
0.63636369, 1.2727274, 1.909091, 2.5454543, 2.1818178, 1.8181814, 1.4545449, 1.090909, 0.72727257, 0.36363629;
0.54545456, 1.0909091, 1.6363636, 2.1818178, 2.7272723, 2.2727268, 1.8181812, 1.3636363, 0.9090907, 0.45454535;
0.45454544, 0.90909088, 1.3636363, 1.8181814, 2.2727268, 2.7272723, 2.1818175, 1.6363635, 1.0909089, 0.54545444;
0.36363637, 0.72727275, 1.090909, 1.4545451, 1.8181815, 2.1818178, 2.545454, 1.9090909, 1.2727271, 0.63636357;
0.27272728, 0.54545456, 0.81818181, 1.0909089, 1.3636363, 1.6363634, 1.9090906, 2.1818182, 1.4545454, 0.72727269;
0.18181817, 0.36363634, 0.54545456, 0.72727257, 0.90909082, 1.0909089, 1.2727271, 1.4545454, 1.6363635, 0.81818175;
0.090909094, 0.18181819, 0.27272728, 0.36363631, 0.45454541, 0.54545444, 0.63636357, 0.72727275, 0.81818175, 0.90909088]
add mat:
[2.909091, -0.18181819, 0.72727281, 0.63636357, 0.54545444, 0.45454538, 0.36363626, 0.27272728, 0.18181814, 0.090909071;
-0.18181819, 3.6363635, 0.45454562, 1.2727271, 1.0909089, 0.90909076, 0.72727251, 0.54545456, 0.36363629, 0.18181814;
0.72727281, 0.45454562, 4.1818185, 0.90909076, 1.6363634, 1.3636361, 1.0909088, 0.81818181, 0.54545444, 0.27272722;
0.63636369, 1.2727274, 0.909091, 4.545454, 1.1818178, 1.8181814, 1.4545449, 1.090909, 0.72727257, 0.36363629;
0.54545456, 1.0909091, 1.6363636, 1.1818178, 4.727272, 1.2727268, 1.8181812, 1.3636363, 0.9090907, 0.45454535;
0.45454544, 0.90909088, 1.3636363, 1.8181814, 1.2727268, 4.727272, 1.1818175, 1.6363635, 1.0909089, 0.54545444;
0.36363637, 0.72727275, 1.090909, 1.4545451, 1.8181815, 1.1818178, 4.545454, 0.90909088, 1.2727271, 0.63636357;
0.27272728, 0.54545456, 0.81818181, 1.0909089, 1.3636363, 1.6363634, 0.90909064, 4.181818, 0.45454538, 0.72727269;
0.18181817, 0.36363634, 0.54545456, 0.72727257, 0.90909082, 1.0909089, 1.2727271, 0.45454538, 3.6363635, -0.18181825;
0.090909094, 0.18181819, 0.27272728, 0.36363631, 0.45454541, 0.54545444, 0.63636357, 0.72727275, -0.18181825, 2.909091]
multiply mat:
[4.181818, 5.4545455, 6.909091, 7.6363621, 7.7272706, 7.2727251, 6.3636341, 5.0909085, 3.5454535, 1.8181813;
5.454545, 11.090909, 13.090909, 14.63636, 14.909087, 14.090905, 12.363631, 9.9090891, 6.9090891, 3.5454535;
6.9090915, 13.09091, 18.818182, 20.363632, 20.999994, 19.999994, 17.636356, 14.181816, 9.9090881, 5.0909076;
7.636363, 14.636362, 20.363634, 25.18181, 25.454536, 24.545444, 21.818171, 17.63636, 12.363632, 6.3636341;
7.727272, 14.909089, 20.999996, 25.454536, 28.727262, 27.272717, 24.545443, 19.999994, 14.090904, 7.2727246;
7.2727261, 14.090907, 19.999996, 24.545444, 27.272717, 28.727262, 25.454533, 20.999994, 14.909085, 7.7272701;
6.3636355, 12.363635, 17.636362, 21.818174, 24.545446, 25.454536, 25.181808, 20.363632, 14.63636, 7.6363616;
5.090909, 9.90909, 14.181817, 17.636358, 19.999994, 20.999994, 20.36363, 18.81818, 13.090906, 6.9090896;
3.5454543, 6.9090905, 9.90909, 12.363633, 14.090905, 14.909086, 14.636359, 13.090907, 11.090907, 5.4545441;
1.8181818, 3.5454545, 5.090909, 6.3636351, 7.2727256, 7.7272706, 7.6363616, 6.9090905, 5.4545445, 4.181818]
eye mat:
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 1, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 1, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 1, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 1, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 1, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 1, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
normalize mat:
[0.31034487, 0.27586213, 0.24137938, 0.20689656, 0.17241378, 0.13793103, 0.10344826, 0.068965539, 0.034482758, 1.3546499e-09;
0.27586213, 0.58620697, 0.51724154, 0.44827589, 0.37931034, 0.31034482, 0.24137928, 0.17241383, 0.10344827, 0.034482758;
0.24137938, 0.51724154, 0.79310369, 0.68965524, 0.58620691, 0.48275861, 0.37931028, 0.27586213, 0.17241378, 0.068965517;
0.2068966, 0.44827598, 0.6896553, 0.93103451, 0.7931034, 0.65517235, 0.51724124, 0.37931037, 0.24137929, 0.10344827;
0.17241383, 0.37931043, 0.58620697, 0.7931034, 1, 0.82758617, 0.65517229, 0.48275867, 0.31034482, 0.13793102;
0.13793106, 0.31034487, 0.48275867, 0.65517235, 0.82758617, 1, 0.79310334, 0.58620691, 0.37931034, 0.17241378;
0.1034483, 0.24137937, 0.37931037, 0.51724136, 0.65517241, 0.7931034, 0.93103445, 0.68965524, 0.44827589, 0.20689656;
0.068965539, 0.17241383, 0.27586213, 0.37931034, 0.48275867, 0.58620691, 0.68965518, 0.79310358, 0.51724142, 0.24137934;
0.03448277, 0.10344829, 0.17241383, 0.24137929, 0.31034485, 0.37931034, 0.44827589, 0.51724142, 0.58620691, 0.2758621;
9.8328981e-09, 0.034482773, 0.068965539, 0.10344828, 0.13793105, 0.17241378, 0.20689656, 0.24137937, 0.2758621, 0.31034487]
a123456@lucky build %
到了这里,关于【OpenCV实战】2.OpenCV基本数据类型实战的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!