1.基本的开发环境是
ubuntu20.04
IDE是Clion最新版
opencv是该版本的ros自带的opencv4,或者是自己安的oepncv4.4+contrib,我忘了。
在本文最后有完整的cmakelist,和程序:
cmakelist的写法:
cmake_minimum_required(VERSION 3.19)
project(ps4_drive)
# 添加Python.h头文件路径
include_directories(/usr/include/python3.8)
find_package(OpenCV REQUIRED) ####2222222222222222222222
# 添加python动态链接库文件目录
link_directories(${OpenCV_LIBS})
#target_link_libraries(camera_driver libpython3.8.so)
set(CMAKE_CXX_STANDARD 14)
add_executable(test1 main.cpp)
target_link_libraries(test1 ${OpenCV_LIBS}) ###1111111111111111111
注意上边的1、2两处的写法就可以正确引用opencv了。
OpenCV基础1
掌握imshow();imread();
示例代码:
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/core.hpp"
#include "opencv2/opencv.hpp"
#include<opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main() {
Mat pic= imread("/home/wdb1/CLionProjects/untitled/picture/conimg2.jpg");
//只需要改正确的路径
imshow("picture",pic);
waitKey(0);
destroyAllWindows();
return 0;
}
可以加载显示出图片就算成功了。
opencv基础二
对彩色图像灰度读取:
Mat pic2= imread("../picture/left_8.jpg",IMREAD_GRAYSCALE);
//只需要改保存路径
基础三
对图片格式进行颜色转换:(可执行文件在testing里边)
挺多种格式转换的,初始是BGR
//
// Created by wdb1 on 2021/5/10.
//
#include <opencv2/core.hpp>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
//sk2: create a class named QuickDemo to change colors of a pictrue.
class QuickDemo{
public:
void colorSpace_Demo(Mat &image){
Mat gray,hsv;
cvtColor(image,hsv,COLOR_BGR2HSV);
cvtColor(image, gray,COLOR_BGR2GRAY);
imshow("HSV",hsv);
imshow("GRAY",gray);
imwrite("../picture/hsv.jpg",hsv);
imwrite("../picture/gray.jpg",gray);
};
};
int main()
{
Mat image= imread("../picture/left_8.jpg");
if(image.empty())
{
printf("could not open image corretly!");
return -1;
}
namedWindow("mdb",WINDOW_FREERATIO);
QuickDemo wdb;
wdb.colorSpace_Demo(image);
waitKey(0);
destroyAllWindows();
return 0;
}
基础四:学Mat::zeros(size(n,m),类型和通道)
创建
//
// Created by wdb1 on 2021/5/10.
//
#include <opencv2/core.hpp>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "iostream"
#include "stdio.h"
using namespace cv;
//sk2: create a class named QuickDemo to change colors of a pictrue.
class QuickDemo{
public:
void colorSpace_Demo(Mat &image){
Mat gray,hsv;
cvtColor(image,hsv,COLOR_BGR2HSV);
cvtColor(image, gray,COLOR_BGR2GRAY);
imshow("HSV",hsv);
imshow("GRAY",gray);
imwrite("../picture/hsv.jpg",hsv);
//ocun tupian
imwrite("../picture/gray.jpg",gray);
};
void mat_creation_demo(Mat &image) //tsak 3 mat::zeros(size(8,8),cv_8uc1)
{
Mat m1,m2;
m1=image.clone();
image.copyTo(m2); //yiyang
Mat m3=Mat::zeros(Size(8,8),CV_8UC3); //CV_8uc1 u_int8 dantongdao
m3= Scalar(127,127,128);//对3通道图像的三个通道统一赋值
std::cout<<" width "<<m3.cols<<" height "<<m3.rows<<" channels "<<m3.channels()<<std::endl;
std::cout<<m3<<std::endl;
Mat m4=m3.clone();//和直接等于m3效果不同
}
};
int main()
{
Mat image= imread("../picture/left_8.jpg");
if(image.empty())
{
printf("could not open image corretly!");
return -1;
}
namedWindow("image",WINDOW_FREERATIO);
QuickDemo wdb;
wdb.colorSpace_Demo(image);
wdb.mat_creation_demo(image);
waitKey(0);
destroyAllWindows();
return 0;
}
基础五:对rgb图像的像素与255作差得到一个新的图像:
把下边的函数放到之前创建的类里边
void pixel_visit_demo(Mat &image)
{
int w = image.cols;
int h = image.rows;
int dims = image.channels();
for (int row=0; row<h; row++)
{
for (int col = 0; col < w; ++col) {
if (dims==1) //gray
{
int pv = image.at<uchar>(row,col);
image.at<uchar>(row,col)=255-pv;
}
if(dims==3){ //color
Vec3b bgr=image.at<Vec3b>(row,col);
image.at<Vec3b>(row,col)[0]=255-bgr[0];
image.at<Vec3b>(row,col)[1]=255-bgr[1];
image.at<Vec3b>(row,col)[2]=255-bgr[2];
}
}
}
imshow("pixle",image);
}
基础六:RGB的像素加减乘除:
//
// Created by wdb1 on 2021/5/10.
//
#include <opencv2/core.hpp>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/highgui.hpp>
#include "iostream"
#include "stdio.h"
using namespace cv;
using namespace std;
Mat src,m,dst;
int lightness =50;
int bar=100;
Mat frame;
//sk2: create a class named QuickDemo to change colors of a pictrue.
static void my_track1(int b, void* data)
{
Mat image =*((Mat*)(data));
dst=image.clone();
m=image.clone();
m = Scalar (b, b,b);
add(src,m,dst);
imshow("adjusting",dst);
}
static void my_track2(int b, void* data)
{
Mat image =*((Mat*)(data));
dst=image.clone();
m=image.clone();
m = Scalar (b, b,b);
double cc=(double)(b/100.0);
//add(src,m,dst);
addWeighted(image,cc,m,0.0,0,dst,-1);
imshow("adjusting",dst);
}
static void on_track_add(int, void*)
{
dst=src.clone();
m=src.clone();
m = Scalar (lightness, lightness,lightness);
add(src,m,dst);
imshow("adjusting",dst);
}
static void on_track_sub(int, void*)
{
dst=src.clone();
m=src.clone();
m = Scalar (lightness, lightness,lightness);
subtract(src,m,dst);
imshow("adjusting",dst);
}
static void on_track_sub_add(int, void*)
{
dst=src.clone();
m=src.clone();
// m = Scalar (lightness, lightness,lightness);
if(lightness>=50)
{
m = Scalar (lightness-bar/2, bar-lightness-bar/2,bar-lightness-bar/2);
add(src,m,dst);
}
else
{
m = Scalar (bar/2-lightness, bar/2-lightness,bar/2-lightness);
subtract(src,m,dst);
}
imshow("adjusting",dst);
}
class QuickDemo{
public:
void colorSpace_Demo(Mat &image){
Mat gray,hsv;
cvtColor(image,hsv,COLOR_BGR2HSV);
cvtColor(image, gray,COLOR_BGR2GRAY);
imshow("HSV",hsv);
imshow("GRAY",gray);
imwrite("../picture/hsv.jpg",hsv);
//ocun tupian
imwrite("../picture/gray.jpg",gray);
};
void mat_creation_demo(Mat &image) //tsak 3 mat::zeros(size(8,8),cv_8uc1)
{
Mat m1,m2;
m1=image.clone();
image.copyTo(m2); //yiyang
// Mat m3=Mat::zeros(Size(8,8),CV_8UC3); //CV_8uc1 u_int8 dantongdao
Mat m3=Mat::zeros(Size(400,400),CV_8UC3);
m3= Scalar(127,127,12);
std::cout<<" width "<<m3.cols<<" height "<<m3.rows<<" channels "<<m3.channels()<<std::endl;
// std::cout<<m3<<std::endl;
imshow("mypicture",m3);
Mat m4=m3.clone();
m4=Scalar(12,66,78);
imshow("mypicture",m4);
}
void pixel_visit_demo(Mat &image) //sk4
{
int w = image.cols;
int h = image.rows;
int dims = image.channels();
for (int row=0; row<h; row++)
{
for (int col = 0; col < w; ++col) {
if (dims==1) //gray
{
int pv = image.at<uchar>(row,col);
image.at<uchar>(row,col)=255-pv;
}
if(dims==3){ //color
Vec3b bgr=image.at<Vec3b>(row,col);
image.at<Vec3b>(row,col)[0]=255-bgr[0];
image.at<Vec3b>(row,col)[1]=255-bgr[1];
image.at<Vec3b>(row,col)[2]=255-bgr[2];
}
}
}
imshow("pixle",image);
}
void zhizhen(Mat &image)
{
int w = image.cols;
int h = image.rows;
int dims = image.channels();
for (int row=0; row<h; row++)
{
uchar* current_row=image.ptr<uchar>(row);
for (int col = 0; col < w; ++col) {
if (dims==1) //gray
{
int pv = *current_row;
*current_row=255-pv;
}
if(dims==3){ //color
*current_row++=255-*current_row;
*current_row++=255-*current_row;
*current_row++=255-*current_row;
}
}
}
imshow("pixle",image);
}
void operator_demo(Mat &image)
{
Mat dst=Mat::zeros(image.size(),image.type());
Mat my_iamge=Mat::zeros(image.size(),image.type());
// dst=image +Scalar(50,50,50);
// dst=image -Scalar(50,50,50);
// dst=image/Scalar(2,2,2);
// dst=image*Scalar(2,2,2); //easy to yichu
my_iamge=Scalar (2,2,2);
multiply(image,my_iamge,dst);
int w = image.cols;
int h = image.rows;
int dims = image.channels();
for (int row=0; row<h; row++)
{
for (int col = 0; col < w; ++col) {
if(dims==3){ //color
Vec3b p1=image.at<Vec3b>(row,col);
Vec3b p2=my_iamge.at<Vec3b>(row,col);
dst.at<Vec3b>(row,col)[0]= saturate_cast<uchar>(p1[0]+p2[0]);
dst.at<Vec3b>(row,col)[1]= saturate_cast<uchar>(p1[1]+p2[1]);
dst.at<Vec3b>(row,col)[2]= saturate_cast<uchar>(p1[2]+p2[2]);
}
}
}
imshow("jifa",dst);
}
void tracking_bar_demo(Mat &image)
{
namedWindow("adjusting ligtness",WINDOW_FREERATIO);
Mat dst=Mat::zeros(image.size(),image.type());
Mat m=Mat::zeros(image.size(),image.type());
src=image;
int max_value=100;
// createTrackbar("Value Bar","adjusting ligtness",&lightness,max_value, on_track_add);
// on_track_add(50,0);
// createTrackbar("Value Bar","adjusting ligtness",&lightness,max_value, on_track_sub);
// on_track_sub(100,0);
createTrackbar("Lightness ","adjusting ligtness",&lightness,max_value, on_track_sub_add);
on_track_sub(bar,0);
//createTrackbar("Value Bar","mingren",&lightness,max_value, on_track);
}
void tracking_bar_demo2(Mat &image)
{
namedWindow("adjusting",WINDOW_FREERATIO);
Mat dst=Mat::zeros(image.size(),image.type());
Mat m=Mat::zeros(image.size(),image.type());
src=image;
int max_value=100;
int value_constrast=250;
int max_value_constrast=500;
// createTrackbar("Value Bar","adjusting ligtness",&lightness,max_value, on_track_add);
// on_track_add(50,0);
// createTrackbar("Value Bar","adjusting ligtness",&lightness,max_value, on_track_sub);
// on_track_sub(100,0);
createTrackbar("Lightness ","adjusting",&lightness,max_value, my_track1,&image);
createTrackbar("Contrast ","adjusting",&value_constrast,max_value_constrast, my_track2,&image);
on_track_sub(bar,0);
//createTrackbar("Value Bar","mingren",&lightness,max_value, on_track);
}
void key_demo(Mat &image)
{
Mat abc=image.clone();
while(1){
char c= waitKey(10);
cout<<c<<endl;
if(c==49) break; //1
if(c==50) //2
{
cvtColor(image,abc,COLOR_BGR2GRAY);
imshow("image",abc);
}
}
}
void resize_demo(Mat &image)
{
Mat zoomin,zoomout;
int h= image.rows;
int w= image.cols;
resize(image,zoomin,Size(w/2,h/2),0,0,INTER_LINEAR);
imshow("fangda suoxiao",zoomin);
imwrite("../picture/zoomin.jpeg",zoomin);
}
void drawing_demo(Mat &image)
{
Rect rect1;
Rect rect2;
rect1.x=150;
rect1.y=100;
rect1.width=200;
rect1.height=200;
rect2.x=130;
rect2.y=240;
rect2.width=150;
rect2.height=150;
rectangle(image,rect1,Scalar(0,0,255),2,8,0);
rectangle(image,rect2,Scalar(0,255,0),2,8,0);
imshow("drawing ",image);
}
void camera_demo()
{
VideoCapture capture(0);
while(1){
capture.read(frame);
flip(frame,frame,1); //jingxiang
if(frame.empty()){
cout<<"No camera. Please insert camera!!!"<<endl;
}
Rect rect1;
rect1.x=150;
rect1.y=100;
rect1.width=200;
rect1.height=200;
namedWindow("camera",WINDOW_FREERATIO);
rectangle(frame,rect1,Scalar(0,0,255),2,8,0);
m=frame.clone();
m = Scalar (lightness, lightness,lightness);
add(frame,m,frame);
imshow("camera",frame);
int c= waitKey(10);//27=ESC
if(c==27) break;
}
}
void camera_demo1()
{
VideoCapture capture(0);
while(1){
capture.read(frame);
flip(frame,frame,1); //jingxiang
int frame_width=capture.get(CAP_PROP_FRAME_WIDTH);
int frame_hight=capture.get(CAP_PROP_FRAME_HEIGHT);
double fps=capture.get(CAP_PROP_FPS);
if(frame.empty()){
cout<<"No camera. Please insert camera!!!"<<endl;
}
namedWindow("camera",WINDOW_FREERATIO);
// rectangle(frame,rect1,Scalar(0,0,255),2,8,0);
m=frame.clone();
m = Scalar (lightness, lightness,lightness);
cout<<fps<<endl;
add(frame,m,frame);
imshow("camera",frame);
int c= waitKey(10);//27=ESC
if(c==27) break;
}
}
void histogram_demo(Mat &image)
{
vector<Mat>bgr_plan;
split(image,bgr_plan); //fencheng R G B
const int channels[1]={0};
const int bins[1]={255};
float hranges[2]={0,255};
const float* ranges[1]={hranges};
Mat B_hist;
Mat G_hist;
Mat R_hist;
calcHist(&bgr_plan[0],1,0,Mat(),B_hist,1,bins,ranges);
calcHist(&bgr_plan[1],1,0,Mat(),G_hist,1,bins,ranges);
calcHist(&bgr_plan[2],1,0,Mat(),R_hist,1,bins,ranges);
//show map
int hist_w=image.rows;
int hist_h=image.cols;
int bin_w= cvRound((double )hist_w/bins[0]);
Mat histImage= image.clone();
histImage=Scalar (255,255,255);
//guiyihua fangzhi yichu
normalize(B_hist,B_hist,0,histImage.rows*0.6,NORM_MINMAX,-1,Mat());
normalize(G_hist,G_hist,0,histImage.rows*0.6,NORM_MINMAX,-1,Mat());
normalize(R_hist,R_hist,0,histImage.rows*0.6,NORM_MINMAX,-1,Mat());
//drawing
// line(histImage,Point(2,2),Point(5,6)),Scalar(255,0,0),2,8);
for (int i = 0; i < bins[0]; ++i) {
line(histImage,Point(bin_w*(i-1),hist_h- cvRound(B_hist.at<float>(i-1))),
Point(bin_w*(i),hist_h- cvRound(B_hist.at<float>(i))),Scalar (0,255,0),2,1);
line(histImage,Point(bin_w*(i-1),hist_h- cvRound(G_hist.at<float>(i-1))),
Point(bin_w*(i),hist_h- cvRound(G_hist.at<float>(i))),Scalar (255,0,0),2,2);
line(histImage,Point(bin_w*(i-1),hist_h- cvRound(R_hist.at<float>(i-1))),
Point(bin_w*(i),hist_h- cvRound(R_hist.at<float>(i))),Scalar (0,0,255),2,3);
}
while(1) {
namedWindow("histogram", WINDOW_FREERATIO);
imshow("histogram", histImage);
imshow("mingren", image);
int c = waitKey(10);//27=ESC
if (c == 27) break;
}
}
void histogram_eq_demo(Mat image)
{
Mat gray;
cvtColor(image,gray,COLOR_BGR2GRAY);
imshow("huidu",gray);
Mat dst;
equalizeHist(gray,dst);
imshow("show",dst);
}
void blur_demo(Mat &image)
{
Mat dst;
while(1) {
blur(image,dst,Size(6,6),Point(1,1)); //size里边的参数可以加深模糊效果
imshow("blur",dst);
imshow("mingren",image);
int c = waitKey(10);//27=ESC
if (c == 27) break;
}
}
};
int main()
{
Mat image= imread("../picture/mingren.jpeg");
Mat image1=image.clone();
if(image.empty())
{
printf("could not open image corretly!");
return -1;
}
// namedWindow("mingren",WINDOW_FREERATIO);
QuickDemo wdb;
// wdb.colorSpace_Demo(image);
// wdb.mat_creation_demo(image);
// wdb.pixel_visit_demo(image);
// wdb.zhizhen(image);
// wdb.operator_demo(image);
//imshow("mingren",image1);
// wdb.tracking_bar_demo2(image);
// wdb.resize_demo(image);
// line(image1,Point(250,330),Point(600,700),Scalar (0,255,0),4,8,0);
// wdb.key_demo(image);
//wdb.drawing_demo(image);
// wdb.camera_demo();
//wdb.histogram_demo(image);
//wdb.histogram_eq_demo(image);
//imshow("mingren",image);
wdb.blur_demo(image);
waitKey(0);
destroyAllWindows();
return 0;
}
完整的cmakelist.txt:
cmake_minimum_required(VERSION 3.19)
project(learning_opencv)
# 添加Python.h头文件路径
include_directories(/usr/include/python3.8)
find_package(OpenCV REQUIRED)
# 添加python动态链接库文件目录
link_directories(${OpenCV_LIBS})
#target_link_libraries(camera_driver libpython3.8.so)
set(CMAKE_CXX_STANDARD 14)
add_executable(task1 task1.cpp)
target_link_libraries(task1 ${OpenCV_LIBS})
add_executable(task2 task2.cpp)
target_link_libraries(task2 ${OpenCV_LIBS})