• OpenCV图像处理基础操作


    目录

    一:读取显示图片操作

    二:图像处理   像素点操作

    2.1   雪花屏特效

    2.2   色彩反转

    2.3   暗色处理 

    2.4   毛玻璃特效

    三:图像处理   调用OpenCV库 封装函数 

    3.1   高斯模糊 [就好像 近视眼的人没有戴眼镜看事物的情境---轻度近视]

    3.2   XY轴模糊 [就好像 近视眼的人没有戴眼镜看事物的情境---重度近视]

    3.3   灰度化处理

    3.4   中值滤波  视觉上就感觉像是油画

    四:OpenCV   视频操作

    4.1   播放视频:那些年,我们一起追的女孩

    4.2   使用本机摄像头


    一:读取显示图片操作

    读取图片的路径选择,这里注意点是window下 \ 但是在ubuntu下是 / 在编写代码的时候需要修正

    读取图片代码:

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. int main(int argc, char *argv[])
    6. {
    7. Mat img = imread("D:/000imageopencv/333.jpg");//图片路径
    8. imshow("img",img);//显示图片
    9. waitKey(0);//等待按键
    10. return 0;
    11. }

    结果:

    对图片可以滚轮放大查看RGB数值,不难看出,越是深色RGB数值越是接近0,越是浅色RGB数值越是接近255

     查看分辨率 499 X 355,同时将鼠标移动至图片边缘处(看到 x=498,y=334 博主已经很尽力移到边缘啦,如果不信,你们自己操作一下,哈哈),由此可以知道图片就是由一个又一个的像素点构成的。

    二:图像处理   像素点操作

    2.1   雪花屏特效

    相关代码如下:

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. Mat imageprocess(Mat &img)
    6. {
    7. int row = img.rows;
    8. int col = img.cols * img.channels();
    9. for(int i=0;i
    10. {
    11. uchar * data = img.ptr(i);
    12. for(int j=0;j
    13. {
    14. //雪花屏特效
    15. int q = rand()%col;
    16. data[q]=155;//某些通道随机改成155
    17. }
    18. }
    19. return img;
    20. }
    21. int main(int argc, char *argv[])
    22. {
    23. Mat img = imread("D:/000imageopencv/333.jpg");//图片路径
    24. imshow("img",img);//显示图片
    25. Mat resimg = imageprocess(img);
    26. imshow("resimg",resimg);//显示接收图片
    27. waitKey(0);//等待按键
    28. return 0;
    29. }

    2.2   色彩反转

      

    相关代码如下: 

    1. Mat imageprocess(Mat &img)
    2. {
    3. int row = img.rows;
    4. int col = img.cols * img.channels();
    5. for(int i=0;i
    6. {
    7. uchar * data = img.ptr(i);
    8. for(int j=0;j
    9. {
    10. //色彩反转
    11. data[j] = data[j] - 50;
    12. }
    13. }
    14. return img;
    15. }

    2.3   暗色处理 

    相关代码如下: 

    1. Mat imageprocess(Mat &img)
    2. {
    3. int row = img.rows;
    4. int col = img.cols * img.channels();
    5. for(int i=0;i
    6. {
    7. uchar * data = img.ptr(i);
    8. for(int j=0;j
    9. {
    10. //暗色处理
    11. data[j] = data[j]/2;
    12. }
    13. }
    14. return img;
    15. }

    2.4   毛玻璃特效

    相关代码如下:

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. //毛玻璃特效
    6. Mat imageGalss(Mat &img)
    7. {
    8. RNG rng;
    9. int random = 0;
    10. int num = 5;
    11. for(int i=0;i-5;i++)
    12. {
    13. for(int j=0;j-5;j++)
    14. {
    15. random = rng.uniform(0,num);
    16. img.at(i,j)[0] = img.at(i+random,j+random)[0];
    17. img.at(i,j)[1] = img.at(i+random,j+random)[1];
    18. img.at(i,j)[2] = img.at(i+random,j+random)[2];
    19. }
    20. }
    21. return img;
    22. }
    23. int main(int argc, char *argv[])
    24. {
    25. Mat img = imread("D:/000imageopencv/333.jpg");//图片路径
    26. imshow("img",img);//显示图片
    27. Mat resimg = imageGalss(img);
    28. imshow("resimg",resimg);//显示接收图片
    29. waitKey(0);//等待按键
    30. return 0;
    31. }

    三:图像处理   调用OpenCV库 封装函数 

    3.1   高斯模糊 [就好像 近视眼的人没有戴眼镜看事物的情境---轻度近视]

    相关代码如下:

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. int main(int argc, char *argv[])
    6. {
    7. Mat img = imread("D:/000imageopencv/333.jpg");//图片路径
    8. imshow("img",img);//显示图片
    9. Mat resimg;
    10. //高斯模糊
    11. cv::GaussianBlur(img,resimg,Size(5,5),0);
    12. imshow("resimg",resimg);//显示接收图片
    13. waitKey(0);//等待按键
    14. return 0;
    15. }

    3.2   XY轴模糊 [就好像 近视眼的人没有戴眼镜看事物的情境---重度近视]

    相关代码如下:

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. int main(int argc, char *argv[])
    6. {
    7. Mat img = imread("D:/000imageopencv/333.jpg");//图片路径
    8. imshow("img",img);//显示图片
    9. Mat resimg;
    10. //XY轴模糊
    11. cv::blur(img,resimg,Size(10,10));
    12. imshow("resimg",resimg);//显示接收图片
    13. waitKey(0);//等待按键
    14. return 0;
    15. }

    3.3   灰度化处理

    相关代码如下: 

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. int main(int argc, char *argv[])
    6. {
    7. Mat img = imread("D:/000imageopencv/333.jpg");//图片路径
    8. imshow("img",img);//显示图片
    9. Mat resimg;
    10. //灰度处理
    11. cvtColor(img,resimg,CV_BGR2GRAY);
    12. imshow("resimg",resimg);//显示接收图片
    13. waitKey(0);//等待按键
    14. return 0;
    15. }

    3.4   中值滤波  视觉上就感觉像是油画

    相关代码如下:

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. int main(int argc, char *argv[])
    6. {
    7. Mat img = imread("D:/000imageopencv/333.jpg");//图片路径
    8. imshow("img",img);//显示图片
    9. Mat resimg;
    10. //中值滤波
    11. cv::medianBlur(img,resimg,5);
    12. imshow("resimg",resimg);//显示接收图片
    13. waitKey(0);//等待按键
    14. return 0;
    15. }

    四:OpenCV   视频操作

    4.1   播放视频:那些年,我们一起追的女孩

      

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. int main(int argc, char *argv[])
    6. {
    7. Mat frame;
    8. VideoCapture cap("D:/000000000000000ffmpeg/那些年,我们一起追的女孩.mp4");
    9. while (cap.read(frame))
    10. {
    11. imshow("frame",frame);
    12. waitKey(50);
    13. }
    14. return 0;
    15. }

     4.2   使用本机摄像头

    这边博主就不露脸了哈!

    1. #include
    2. #include
    3. using namespace std;
    4. using namespace cv;
    5. int main(int argc, char *argv[])
    6. {
    7. Mat frame;
    8. VideoCapture cap(0);
    9. while (cap.read(frame))
    10. {
    11. imshow("frame",frame);
    12. waitKey(50);
    13. }
    14. return 0;
    15. }

  • 相关阅读:
    【Kotlin 协程】协程底层实现 ② ( 协程调度器 | 协程任务泄漏 | 结构化并发 )
    每天5分钟复习OpenStack(八)存储虚拟化
    Qt之图片缩放并解决失真问题
    Vant UI的Sidebar侧边导航组件单独设置滚动条
    Nmap网络扫描
    神经网络建模的基本思想,神经网络建模数据分析
    第十章《日期与时间》第5节:Period与Duration
    10、学习MySQL LIKE 子句
    边端融合系统安全风险分析及评估方法
    DevOps|研发效能治理:进化史、规模化与治理复杂性
  • 原文地址:https://blog.csdn.net/m0_56051805/article/details/126044827