[14] CUDA_使用Opencv处理图像

CUDA_使用Opencv处理图像

1. Opencv中的图像表示

  • Opencv 提供了Mat 类来存储图像,如下:
cpp 复制代码
cv::Mat img;
img=cv::imread("cameraman.tif);
  • 定义图像的示例:
cpp 复制代码
//定义单通道图像
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. 图像的读取和显示

  • 图像读取与显示实现如下:
cpp 复制代码
#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 Image
 if (img.empty()) 
 {
  cout << "Could not open an image" << endl;
  return -1;
 }
//Name of the window
 String win_name = "My First Opencv Program"; 

 // Create a window
 namedWindow(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 window
 destroyWindow(win_name); 

 return 0;
}

3. 使用Opencv 创建图像

cpp 复制代码
#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 256x256
	Mat 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 256x256
	Mat 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;
}
  • 在空白图像上绘制形状:
cpp 复制代码
#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 window

    namedWindow(win_name); // Create a window
    imshow(win_name, img); // Show our image inside the created window.
    waitKey(0); // Wait for any keystroke in the window
    destroyWindow(win_name); //destroy the created window

    return 0;
}
  • 保存图像
cpp 复制代码
bool flag=cv::imwrite("images/save_image.jpg",img);

4. 使用Opencv 处理视频

  • 处理本地视频:
cpp 复制代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
    //open the video file from PC
    VideoCapture cap("images/rhinos.avi");
    // if not success, exit program
    if (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 frame
        bool flag = cap.read(frame);

        //Breaking the while loop at the end of the video
        if (flag == false)
        {
            break;
        }
        //display the frame 
        imshow(win_name, frame);
        //Wait for 100 ms and key 'q' for exit
        if (waitKey(100) == 'q')
        {
            break;
        }
    }
    destroyWindow(win_name);
    return 0;
}
  • 处理网络相机:
cpp 复制代码
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
 //open the Webcam
 VideoCapture cap(0); 
 // if not success, exit program
 if (cap.isOpened() == false)  
 {
  cout << "Cannot open Webcam" << endl;
  return -1;
 }
 //get the frames rate of the video
 double 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 window
 while (true)
 {
  Mat frame;
  bool flag = cap.read(frame); // read a new frame from video 
  //show the frame in the created window
  imshow(win_name, frame);
  if (waitKey(1) == 'q')
  {
      break;
  }
 }
return 0;
}
  • 保存视频:
cpp 复制代码
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();
相关推荐
volcanical35 分钟前
Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena
人工智能·自然语言处理·机器翻译
大知闲闲哟36 分钟前
深度学习J6周 ResNeXt-50实战解析
人工智能·深度学习
静静AI学堂1 小时前
Yolo11改策略:卷积改进|SAC,提升模型对小目标和遮挡目标的检测性能|即插即用
人工智能·深度学习·目标跟踪
martian6652 小时前
【人工智能离散数学基础】——深入详解数理逻辑:理解基础逻辑概念,支持推理和决策系统
人工智能·数理逻辑·推理·决策系统
Schwertlilien2 小时前
图像处理-Ch7-图像金字塔和其他变换
图像处理·人工智能
凡人的AI工具箱2 小时前
每天40分玩转Django:Django类视图
数据库·人工智能·后端·python·django·sqlite
千天夜2 小时前
深度学习中的残差网络、加权残差连接(WRC)与跨阶段部分连接(CSP)详解
网络·人工智能·深度学习·神经网络·yolo·机器学习
一勺汤2 小时前
YOLOv8模型改进 第二十五讲 添加基于卷积调制(Convolution based Attention) 替换自注意力机制
深度学习·yolo·计算机视觉·模块·yolov8·yolov8改进·魔改
凡人的AI工具箱2 小时前
每天40分玩转Django:实操图片分享社区
数据库·人工智能·后端·python·django
小军军军军军军2 小时前
MLU运行Stable Diffusion WebUI Forge【flux】
人工智能·python·语言模型·stable diffusion