[14] CUDA_使用Opencv处理图像

2024-06-19 10:20
文章标签 使用 图像 opencv 处理 14 cuda

本文主要是介绍[14] CUDA_使用Opencv处理图像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

CUDA_使用Opencv处理图像

1. Opencv中的图像表示

  • Opencv 提供了Mat 类来存储图像,如下:
cv::Mat img;
img=cv::imread("cameraman.tif);
  • 定义图像的示例:
//定义单通道图像
cv::Mat img(6,6,CV_8UC1);
//32位浮点型
Mat img2(256,256,CV_32FC1);
Mat img3(1960,1024,CV_64FC3);
  • 图像的分辨率和大小决定该图像保存到磁盘上的空间,假设有3个通道、大小为1024X1024的彩色图像,则需要 3X1024X1024 bytes = 3MB 空间来存放这个图像。

2. 图像的读取和显示

  • 图像读取与显示实现如下:
#include <opencv2/opencv.hpp>
#include <iostream>using namespace cv;
using namespace std;int main(int argc, char** argv)
{// Read the image Mat img = imread("images/cameraman.tif",0);// Check for failure in reading an Imageif (img.empty()) {cout << "Could not open an image" << endl;return -1;}
//Name of the windowString win_name = "My First Opencv Program"; // Create a windownamedWindow(win_name); // Show our image inside the created window.
imshow(win_name, img); // Wait for any keystroke in the window 
waitKey(0); //destroy the created windowdestroyWindow(win_name); return 0;
}

3. 使用Opencv 创建图像

#include <opencv2/opencv.hpp>
#include <iostream>using namespace cv;
using namespace std;int main(int argc, char** argv)
{//Create blanck black color Image with seze 256x256Mat img1(256, 256, CV_8UC1, Scalar(0));String win_name1 = "Blank Image";namedWindow(win_name1,0);imshow(win_name1, img1);//Create blank blue color Image with size 256x256Mat img(256, 256, CV_8UC3, Scalar(255, 0, 0));String win_name = "Blank Blue Color Image";namedWindow(win_name,0);imshow(win_name, img);waitKey(0);destroyWindow(win_name1);destroyWindow(win_name);return 0;
}

在这里插入图片描述

  • 在空白图像上绘制形状:
#include <opencv2/opencv.hpp>
#include <iostream>using namespace cv;
using namespace std;int main(int argc, char** argv)
{//create a new image which consists of //3 channels //image depth of 8 bits //800 x 600 of resolution (800 wide and 600 high)//each pixels initialized to the value of (100, 250, 30) for Blue, Green and Red planes respectively.Mat img(512, 512, CV_8UC3, Scalar(0, 0, 0));//画线line(img, Point(0, 0), Point(511, 511), Scalar(0, 255, 0), 7);//矩形rectangle(img, Point(384, 0), Point(510, 128), Scalar(255, 255, 0), 5);//画圆circle(img, Point(447, 63), 63, Scalar(0, 0, 255), -1);//椭圆ellipse(img, Point(256, 256), Point(100, 100), 0, 0, 180, 255, -1);//添加文字putText(img, "OpenCV!", Point(10, 500), FONT_HERSHEY_SIMPLEX, 3,Scalar(255, 255, 255), 5, 8);String win_name = "Blank Blue Color Image"; //Name of the windownamedWindow(win_name); // Create a windowimshow(win_name, img); // Show our image inside the created window.waitKey(0); // Wait for any keystroke in the windowdestroyWindow(win_name); //destroy the created windowreturn 0;
}

在这里插入图片描述

  • 保存图像
bool flag=cv::imwrite("images/save_image.jpg",img);

4. 使用Opencv 处理视频

  • 处理本地视频:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{//open the video file from PCVideoCapture cap("images/rhinos.avi");// if not success, exit programif (cap.isOpened() == false){cout << "Cannot open the video file" << endl;return -1;}cout << "Press Q to Quit" << endl;String win_name = "First Video";namedWindow(win_name);while (true){Mat frame;// read a framebool flag = cap.read(frame);//Breaking the while loop at the end of the videoif (flag == false){break;}//display the frame imshow(win_name, frame);//Wait for 100 ms and key 'q' for exitif (waitKey(100) == 'q'){break;}}destroyWindow(win_name);return 0;
}
  • 处理网络相机:
#include <opencv2/opencv.hpp>
#include <iostream>using namespace cv;
using namespace std;int main(int argc, char* argv[])
{//open the WebcamVideoCapture cap(0); // if not success, exit programif (cap.isOpened() == false)  {cout << "Cannot open Webcam" << endl;return -1;}//get the frames rate of the videodouble fps = cap.get(CAP_PROP_FPS); cout << "Frames per seconds : " << fps << endl;
cout<<"Press Q to Quit" <<endl;String win_name = "Webcam Video";namedWindow(win_name); //create a windowwhile (true){Mat frame;bool flag = cap.read(frame); // read a new frame from video //show the frame in the created windowimshow(win_name, frame);if (waitKey(1) == 'q'){break;}}
return 0;
}
  • 保存视频:
size frame_size(640,640);
int frame_per_second = 30;
Videowriter v_writer("image/video.avi",Videowriter::fourcc('M','J','P','G'),frames_per_second,frame_size,true)
v_writer.write(frame);
v_writer.release();

这篇关于[14] CUDA_使用Opencv处理图像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1074759

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java