OpenCV 30讲 冲
Vs2019配置Eigen和Opencv和由于找不到opencv_world451.dll无法继续执行代码问题解决_想做一个开森的胖纸的博客-CSDN博客配置环境变量永远是最ex的!!! 上面这个博客很好
OpenCV4学习笔记 - 目录_Apple_Coco的博客-CSDN博客_opencv4学习
大佬的学习笔记
001-图像读取与显示
#include <opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
//Mat src = imread("C:/Users.jpg",IMREAD_GRAYSCALE);//显示灰度图像
Mat src = imread("C:/Users.jpg");
//Mat 是二维矩阵 src是三通道 每通道1个字节:3*8=24(位)
if (src.empty())//判断图片是否读取正确
{
printf("could not load image...\n");
return -1;
}
namedWindow("输入窗口",WINDOW_FREERATIO);//手动调整窗口大小
imshow("输入窗口", src);
//imshow("窗口名称",图像名字);
//imshow默认创建和图像大小保持一致的窗口WINDOW_AUTOSIZE
waitKey(0);//执行到这句话waitKey(1) 表示图片停顿1毫秒
destroyAllWindows();//销毁 前面创建的显示窗口
return 0;
}
002-图像色彩空间转换
#include "quickopencv.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("C:/Users//Desktop/hsv.jpg",hsv);
imwrite("C:/Users//Desktop/gray.jpg",gray);
//imwrite("保存在哪",图片对象);
}
003-图像对象的创建与赋值
void QuickDemo::mat_creation_demo(Mat& image)
{
//Mat m1, m2;
//m1 = image.clone();
//image.copyTo(m2);
//创建空白图像 8*8矩阵
Mat m3 = Mat::zeros(Size(400, 400), CV_8UC3);
//m3 = Scalar(255, 0, 0);//纯蓝色画布
//m3 = Scalar(0, 0,255); //纯红色画布
std::cout << "width: " << m3.cols << " hight: " << m3.rows << " channels: " << m3.channels() << endl;
//cout << m3 << endl;
Mat m4;
m3.copyTo(m4);
m4 = Scalar(0, 255, 255);//纯黄色
imshow("图像3", m3);
imshow("图像4", m4);
};
004-图像像素的读写操作
效果:
void QuickDemo::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) {//灰度图像
int pv = image.at<uchar>(row,col);
image.at<uchar>(row, col) = 255 - pv;
}if (dims == 3) {//彩色图像
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];
}
}
}
*/
//指针版本 更快!
for (int row = 0; row < h; row++) {
uchar* current_row = image.ptr<uchar>(row);//获取指针
for (int col = 0; col < w; col++) {
if (dims == 1) {//灰度图像
int pv = *current_row;
*current_row++ = 255 - pv;
}if (dims == 3) {//彩色图像
*current_row++ = 255 - *current_row;
*current_row++ = 255 - *current_row;
*current_row++ = 255 - *current_row;
}
}
}
imshow("像素读写演示", image);
}
005-图像像素的算术操作
void QuickDemo::operators_demo(Mat& image) {
Mat dst = Mat::zeros(image.size(), image.type());
Mat m = Mat::zeros(image.size(), image.type());
dst = image + Scalar(50, 50, 50);
//+使图片更亮 -,/ 更暗
multiply(image, m, dst);
//乘法函数 图片超级亮
//下面这一串 是这个multiply函数的操作过程
/*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++) {
Vec3b p1 = image.at<Vec3b>(row, col);
Vec3b p2 = m.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]);
//saturate_cast<u>(p)函数 p被限制在u的范围大于=u.max,小于就=u.min;
//此函数主要用于 数组的转型 int->double...
}
}
*/
//add(image, m, dst); subtract(image, m, dst); divide(image, m, dst);加 减 除法函数
imshow("加法操作", dst);
}
006-滚动条操作演示-调整图像亮度 TrackBar
Mat src, dst, m;
int lightness = 50;
static void on_track(int, void*) {
m = Scalar(lightness, lightness, lightness);
//add(src, m, dst);//亮度增加滚轮
subtract(src, m, dst);//亮度降低滚轮
imshow("亮度调整", dst);
}
void QuickDemo::tracking_bar_demo(Mat& image) {
namedWindow("亮度调整",WINDOW_AUTOSIZE);
dst = Mat::zeros(image.size(), image.type());
m = Mat::zeros(image.size(), image.type());
src = image;
int max_value = 100;
createTrackbar("Value Bar: " , "亮度调整" ,&lightness, max_value, on_track);
on_track(50, 0);
}
007-滚动条操作演示-参数传递度
static void on_lightness(int b, void* userdata) {
Mat image, dst, m;
image = *((Mat*)userdata);
dst = Mat::zeros(image.size(), image.type());
m = Mat::zeros(image.size(), image.type());
m = Scalar(b, b, b);//b 就是拉动的时候变动的数值
//addWeighted( image,alpha,m,beta,gamma,dst);//亮度增加滚轮
//融合两张图 第一张占比xx 第二张xx
addWeighted(image, 1.0, m, 0, b, dst);
//亮度降低滚轮
imshow("亮度与对比度调整", dst);
}
static void on_contrast(int b, void* userdata) {//对比度 (亮的地方更亮,暗的更暗)
Mat image, dst, m;
image = *((Mat*)userdata);
dst = Mat::zeros(image.size(), image.type());
m = Mat::zeros(image.size(), image.type());
m = Scalar(b, b, b);//b 就是拉动的时候变动的数值
double contrast = b / 100.0;
addWeighted(image, contrast, m, 0.0, 0, dst);
//亮度降低滚轮
imshow("亮度与对比度调整", dst);
}
void QuickDemo::tracking_bar_demo(Mat& image) {
namedWindow("亮度与对比度调整",WINDOW_AUTOSIZE);
int lightness = 50;
int max_value = 100;
int contrast_value = 100;
createTrackbar("Value Bar: " , "亮度与对比度调整" ,&lightness, max_value, on_lightness,(void*)(&image));
createTrackbar("Contrast Bar: ", "亮度与对比度调整", &contrast_value, 200, on_contrast, (void*)(&image));
on_lightness(50, &image);
}
008-键盘响应操作
void QuickDemo::key_demo(Mat& image) {
Mat dst = Mat::zeros(image.size(), image.type());
//设定 dst 与传进来的image size,type一样
while (true) {
char c = waitKey(100);//等待一百毫秒
if (c == 27)//退出 Key Esc
break;
if (c == 49) {//Key #1
cout << "you enter key#1" << endl;
cvtColor(image, dst, COLOR_BGR2GRAY);//灰度转换
}
if (c == 50) {//Key #2
cout << "you enter key#2" << endl;
cvtColor(image, dst, COLOR_BGR2HSV);//HSV转换
}
if (c == 51) {//Key #3
cout << "you enter key#3" << endl;
dst = Scalar(50, 50, 50);
add(image, dst, dst);//加亮度
}
imshow("键盘响应",dst);
}
}
009-OpenCV自带颜色表操作
void QuickDemo::color_style_demo(Mat& image) {
int colormap[] = {
COLORMAP_AUTUMN,
COLORMAP_BONE,
COLORMAP_JET,
COLORMAP_WINTER,
COLORMAP_RAINBOW,
COLORMAP_OCEAN,
COLORMAP_SUMMER,
COLORMAP_SPRING,
COLORMAP_COOL,
COLORMAP_PINK,
COLORMAP_HOT,
COLORMAP_PARULA,
COLORMAP_MAGMA,
COLORMAP_INFERNO,
COLORMAP_PLASMA,
COLORMAP_VIRIDIS,
COLORMAP_CIVIDIS,
COLORMAP_TWILIGHT,
COLORMAP_TWILIGHT_SHIFTED,
};
Mat dst;
int index = 0;
while (true) {
int c = waitKey(2000);
if (c == 27) {//退出
break;
}
applyColorMap(image, dst, colormap[index % 19]);
index++;
imshow("颜色风格", dst);
}
}
010-图像像素的逻辑操作
void QuickDemo::bitwise_demo(Mat& image) {
Mat m1 = Mat::zeros(Size(256, 256), CV_8UC3);
Mat m2 = Mat::zeros(Size(256, 256), CV_8UC3);
rectangle(m1, Rect(100,100,80,80),Scalar(255,255,0),2,LINE_8,0);
//rectangle( ,Rect(坐标),Scalar(颜色),thickness(线宽<0表示填充框内,>0表示绘制框线宽),周围8个像素提供资源)
rectangle(m2, Rect(150, 150, 80, 80), Scalar(0, 255, 255), -1, LINE_8, 0);
imshow("m1",m1);
imshow("m2", m2);
Mat dst;
//bitwise_and(m1, m2, dst);//图片取重复的地方
//bitwise_or(m1, m2, dst);//图片交叉
//bitwise_xor(m1, m2, dst);//图片异或
bitwise_not(image,dst);//Mat dst=~image; 直接取反 高级代码
imshow("像素位操作", dst);
}
011-通道分离与合并
void QuickDemo::channels_demo(Mat& image) {//通道分离与合并
vector<Mat>mv;//容器
split(image, mv);//得到各个通道像素表示
imshow("蓝色", mv[0]);
imshow("绿色", mv[1]);
imshow("红色", mv[2]);
Mat dst;
mv[0] = 0;
//mv[1]=0;
merge(mv, dst);//三通道变彩色
imshow("蓝色", dst);
int from_to[] = { 0,2,1,1,2,0 };
mixChannels(&image, 1,&dst, 1, from_to,3 );
imshow("通道混合", dst);
}
本文来自博客园,作者:软工菜鸡,转载请注明原文链接:https://www.cnblogs.com/SElearner/p/17676717.html