CUDA:Sobel算子处理

cpp 复制代码
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

using namespace std;

__global__ void sobel_gpu(unsigned char* in, unsigned char* out,
                          const int Height, const int Width)
{
    int x = threadIdx.x + blockDim.x * blockIdx.x;
    int y = threadIdx.y + blockDim.y * blockIdx.y;


    int index = y * Width + x;
    unsigned char x0, x1, x2, x3, x4, x5, x6, x7, x8;
    int Gx = 0, Gy = 0;


    if(x > 0 && x < Width - 1 && y > 0 && y < Height - 1) {
        x0 = in[(y - 1) * Width + (x - 1)];
        x1 = in[(y - 1) * Width + x];
        x2 = in[(y - 1) * Width + x + 1];


        x3 = in[y * Width + x - 1];
        x4 = in[y * Width + x];
        x5 = in[y * Width + x + 1];


        x6 = in[(y + 1) * Width + (x - 1)];
        x7 = in[(y + 1) * Width + x];
        x8 = in[(y + 1) * Width + (x + 1)];


        Gx = (x0 + 2 * x3 + x6) - (x2 + 2 * x5 + x8);
        Gy = (x0 + 2 * x1 + x2) - (x6 + 2 * x7 + x8);
        out[index] = (abs(Gx) + abs(Gy)) / 2;
    }
}


int main()
{
    cv::Mat img = cv::imread("noise.png", 0);
    int height = img.rows;
    int width = img.cols;


    cv::Mat gaussImg;
    GaussianBlur(img, gaussImg, cv::Size(3,3), 0,0, cv::BORDER_DEFAULT);


    cv::Mat dst_gpu(height, width, CV_8UC1, cv::Scalar(0));


    int memsize = height * width * sizeof(unsigned char);
    unsigned char* in_gpu, *out_gpu;


    cudaMalloc((void**)&in_gpu, memsize);
    cudaMalloc((void**)&out_gpu, memsize);


    dim3 threadsBlocks(32, 32);
    dim3 blocksGrid((width + threadsBlocks.x - 1) / threadsBlocks.x, (height + threadsBlocks.y - 1) / threadsBlocks.y);


    cudaMemcpy(in_gpu, gaussImg.data, memsize, cudaMemcpyHostToDevice);


    sobel_gpu<<<blocksGrid, threadsBlocks>>>(in_gpu, out_gpu, height, width);


    cudaMemcpy(dst_gpu.data, out_gpu, memsize, cudaMemcpyDeviceToHost);


    cv::imwrite("save.png", dst_gpu);


    cudaFree(in_gpu);
    cudaFree(out_gpu);


    printf("Finished \n");
    return 0;
}

CMakeLists.txt配置

cpp 复制代码
cmake_minimum_required(VERSION 3.10)

project(CSobel LANGUAGES CXX CUDA)

add_definitions(-std=c++11)
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CUDA_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O0 -Wfatal-errors -pthread -w -g")


set(OpenCV_DIR ${PROJECT_SOURCE_DIR}/../../3rdparty/opencv3.4.15)

find_package(CUDA REQUIRED)

include_directories(
    ${PROJECT_SOURCE_DIR}
    ${OpenCV_DIR}/include
)

link_directories(
    ${PROJECT_SOURCE_DIR}/lib
    ${OpenCV_DIR}/lib
)

cuda_add_executable(bilateral main.cu)

target_link_libraries(bilateral opencv_world)
相关推荐
CiLerLinux1 天前
第四十九章 ESP32S3 WiFi 路由实验
网络·人工智能·单片机·嵌入式硬件
-dzk-1 天前
【3DGS复现】Autodl服务器复现3DGS《简单快速》《一次成功》《新手练习复现必备》
运维·服务器·python·计算机视觉·3d·三维重建·三维
七芒星20231 天前
多目标识别YOLO :YOLOV3 原理
图像处理·人工智能·yolo·计算机视觉·目标跟踪·分类·聚类
Learn Beyond Limits1 天前
Mean Normalization|均值归一化
人工智能·神经网络·算法·机器学习·均值算法·ai·吴恩达
byzy1 天前
【论文笔记】VisionPAD: A Vision-Centric Pre-training Paradigm for Autonomous Driving
论文阅读·深度学习·计算机视觉·自动驾驶
ACERT3331 天前
5.吴恩达机器学习—神经网络的基本使用
人工智能·python·神经网络·机器学习
C嘎嘎嵌入式开发1 天前
(一) 机器学习之深度神经网络
人工智能·神经网络·dnn
Aaplloo1 天前
【无标题】
人工智能·算法·机器学习
大模型任我行1 天前
复旦:LLM隐式推理SIM-CoT
人工智能·语言模型·自然语言处理·论文笔记
tomlone1 天前
AI大模型核心概念
人工智能