使用Opencv_CUDA 模块实现基本计算机视觉程序

  • CUDA提供了出色的接口,发挥GPU的并行计算能力来加速复杂的计算应用程序
  • 利用CUDA和Opencv的功能实现计算机视觉应用

1. 对图像的算术和逻辑运算

  • 两个图像相加
#include <iostream>
#include "opencv2/opencv.hpp"
#include<cudaarithm.hpp>

int main(int argc, char* argv[])
{
    //Read Two Images 
    cv::Mat h_img1 = cv::imread("images/cameraman.tif");
    cv::Mat h_img2 = cv::imread("images/circles.png");
    //Create Memory for storing Images on device
    cv::cuda::GpuMat d_result1, d_img1, d_img2;
    cv::Mat h_result1;
    //Upload Images to device     
    d_img1.upload(h_img1);
    d_img2.upload(h_img2);

    cv::cuda::add(d_img1, d_img2, d_result1);
    //Download Result back to host
    d_result1.download(h_result1);
    cv::imshow("Image1 ", h_img1);
    cv::imshow("Image2 ", h_img2);
    cv::imshow("Result addition ", h_result1);
    cv::imwrite("images/result_add.png", h_result1);
    cv::waitKey();
    return 0;
}
  • 图像相减:
    //2.图像相减
    cv::cuda::subtract(d_img1, d_img2, d_result1);

[15] 使用Opencv_CUDA 模块实现基本计算机视觉程序-LMLPHP

  • 图像合成:
    //3.图像合成
    cv::cuda::addWeighted(d_img1, 0.7, d_img2, 0.3, 0, d_result1);

[15] 使用Opencv_CUDA 模块实现基本计算机视觉程序-LMLPHP

  • 图像反转:
    //4.图像反转
    cv::cuda::bitwise_not(d_img1, d_result1);
     //others:
    cv::cuda::bitwise_and(d_img1, d_img2, d_result1);
    cv::cuda::bitwise_or(d_img1, d_img2, d_result1);
    cv::cuda::bitwise_xor(d_img1, d_img2, d_result1);

[15] 使用Opencv_CUDA 模块实现基本计算机视觉程序-LMLPHP

2. 更改图像的色彩空间

  • 颜色空间(BGR)、HSV(色调、饱和度、亮度)
  • Opencv支持许多其他可用的颜色空间,例如 XYZ、HLS、Lab等
  • 转换代码如下:
#include <iostream>
#include "opencv2/opencv.hpp"
#include<opencv2/cudaimgproc.hpp>

int main(int argc, char* argv[])
{
    cv::Mat h_img1 = cv::imread("images/autumn.tif");
    //Define device variables
    cv::cuda::GpuMat d_result1, d_result2, d_result3, d_result4, d_img1;
    //Upload Image to device
    d_img1.upload(h_img1);

    //Convert image to different color spaces
    cv::cuda::cvtColor(d_img1, d_result1, cv::COLOR_BGR2GRAY);
    cv::cuda::cvtColor(d_img1, d_result2, cv::COLOR_BGR2RGB);
    cv::cuda::cvtColor(d_img1, d_result3, cv::COLOR_BGR2HSV);
    cv::cuda::cvtColor(d_img1, d_result4, cv::COLOR_BGR2YCrCb);

    cv::Mat h_result1, h_result2, h_result3, h_result4;
    //Download results back to host
    d_result1.download(h_result1);
    d_result2.download(h_result2);
    d_result3.download(h_result3);
    d_result4.download(h_result4);

    cv::imshow("Src image ", h_img1);
    cv::imshow("Result in Gray ", h_result1);
    cv::imshow("Result in RGB", h_result2);
    cv::imshow("Result in HSV ", h_result3);
    cv::imshow("Result in YCrCb ", h_result4);

    cv::waitKey();
    return 0;
}

[15] 使用Opencv_CUDA 模块实现基本计算机视觉程序-LMLPHP

3. 图像阈值处理

  • 函数:cv::cuda::threshold
  • 常用阈值处理类型:
  • 实现代码如下:
#include <iostream>
#include "opencv2/opencv.hpp"
#include<opencv2/cudaarithm.hpp>

int main(int argc, char* argv[])
{
    cv::Mat h_img1 = cv::imread("images/cameraman.tif", 0);
    //Define device variables
    cv::cuda::GpuMat d_result1, d_result2, d_result3, d_result4, d_result5, d_img1;
    //Upload image on device
    d_img1.upload(h_img1);

    //Perform different thresholding techniques on device
    cv::cuda::threshold(d_img1, d_result1, 128.0, 255.0, cv::THRESH_BINARY);
    cv::cuda::threshold(d_img1, d_result2, 128.0, 255.0, cv::THRESH_BINARY_INV);
    cv::cuda::threshold(d_img1, d_result3, 128.0, 255.0, cv::THRESH_TRUNC);
    cv::cuda::threshold(d_img1, d_result4, 128.0, 255.0, cv::THRESH_TOZERO);
    cv::cuda::threshold(d_img1, d_result5, 128.0, 255.0, cv::THRESH_TOZERO_INV);

    cv::Mat h_result1, h_result2, h_result3, h_result4, h_result5;
    //Copy results back to host
    d_result1.download(h_result1);
    d_result2.download(h_result2);
    d_result3.download(h_result3);
    d_result4.download(h_result4);
    d_result5.download(h_result5);
   
    cv::namedWindow("THRESH_BINARY", 0);
    cv::namedWindow("THRESH_BINARY_INV", 0);
    cv::namedWindow("THRESH_TRUNC", 0);
    cv::namedWindow("THRESH_TOZERO", 0);
    cv::namedWindow("THRESH_TOZERO_INV", 0);
    cv::imshow("THRESH_BINARY", h_result1);
    cv::imshow("THRESH_BINARY_INV", h_result2);
    cv::imshow("THRESH_TRUNC", h_result3);
    cv::imshow("THRESH_TOZERO", h_result4);
    cv::imshow("THRESH_TOZERO_INV", h_result5);
    cv::waitKey();

    return 0;
}

[15] 使用Opencv_CUDA 模块实现基本计算机视觉程序-LMLPHP

06-20 08:53