目录
Windows10+ VMware16pro + ubuntu20.04+ VSCode
一、VC Code配置
寻找路径的方法:在文件里,搜索“OpenCV4”,就可以显示出来了,我的路径是下面这个位置,每个人的不一样,和这个接近的就应该是了。
"/usr/include/opencv4"
在c_cpp_properties.json里的includepath增加路径,程序里头文件下面的波浪线就没了,说明vscode找到OpenCV库了。
二、读取图像
2.1 CMakeLists.txt
# CMake最低版本要求
cmake_minimum_required(VERSION 2.8)
# 项目信息
project(read_image)
find_package(OpenCV REQUIRED)
# 制定生成目标
add_executable (read_image read_image.cpp)
target_link_libraries(read_image ${OpenCV_LIBS} )
2.2 源代码
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc,char** argv){
cv::Mat imagepath = imread("/home/bc/图片/屏幕截图.png");
cv::imshow("input", imagepath);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
imshow只能读8位图像和浮点型图像,通过imread读进来的图像都符合8位,通道顺序为B,G,R。
三、任意比例读取图像
cv::namedWindow("输入", cv::WINDOW_FREERATIO);
四、以灰度图方式读取
cv::Mat imagepath = imread("/mnt/hgfs/H/image.jpg",IMREAD_GRAYSCALE);
五、图像色彩转换
5.1 CMakeLists.txt
# CMake最低版本要求
cmake_minimum_required(VERSION 2.8)
# 项目信息
project(read_image)
find_package(OpenCV REQUIRED)
include_directories(
include
)
aux_source_directory(. ALL_SRCS)
# 制定生成目标
add_executable (read_image read_image.cpp quickdemo.cpp)
target_link_libraries(read_image ${OpenCV_LIBS} )
5.2 源代码
#include<quickdemo.h>
void QuickDemo::colorSpace_Demo(Mat &image){
Mat gray, hsv;
cvtColor(image, hsv, COLOR_BGR2HSV);
cvtColor(image, gray, COLOR_BGR2GRAY);
imshow("HSV", hsv);
imshow("灰度图", gray);
imwrite("../image/hsv.png", hsv);
imwrite("../image/gray.png", gray);
}
六、图像赋值
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2)
Mat m3 = cv::Mat::zeros(Size(8, 8),CV_8UC3);
m3 = Scalar(0,255,0); // B,G,R
imshow("tuxiang", m3);
std::cout << "width " << m3.cols << " height: " << m3.rows << " channls:" << m3.channels() << std::endl;
clone和copyto都会产生新的内存,成为新的东西,直接=的话还是一个东西。
七、像素操作
Mat dst = image + 20;
Mat dst = image - 20;
Mat dst = image * 2;
Mat dst = image / 20;
八、滚动条操作
cv::Mat src, m, tsd;
int lightness = 50;
static void on_track(int, void *)
{
m = Scalar(lightness, lightness, lightness);
add(src, m, tsd);
imshow("bar", tsd);
}
void QuickDemo::tracking_bar_demo(Mat &image){
namedWindow("liangdutiaozheng", WINDOW_AUTOSIZE);
tsd = Mat::zeros(image.size(), image.type());
m = Mat::zeros(image.size(), image.type());
src = image;
Mat temp = image;
int max_value = 100;
createTrackbar("valuer bar", "liangdutiaozheng", &lightness, max_value,on_track);
}