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)
相关推荐
乾元30 分钟前
AI 在网络工程中的 12 个高频场景深度实战(Cisco / Huawei 双体系)
人工智能
子午1 小时前
【食物识别系统】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积网络+resnet50算法
人工智能·python·深度学习
Dev7z1 小时前
基于深度学习和图像处理的药丸计数与分类系统研究
图像处理·人工智能·深度学习
Mxsoft6192 小时前
某次联邦学习训练模型不准,发现协议转换字段映射错,手动校验救场!
人工智能
shayudiandian2 小时前
用PyTorch训练一个猫狗分类器
人工智能·pytorch·深度学习
这儿有一堆花2 小时前
把 AI 装进终端:Gemini CLI 上手体验与核心功能解析
人工智能·ai·ai编程
子午2 小时前
【蘑菇识别系统】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积网络+resnet50算法
人工智能·python·深度学习
模型启动机3 小时前
Langchain正式宣布,Deep Agents全面支持Skills,通用AI代理的新范式?
人工智能·ai·langchain·大模型·agentic ai
Python私教3 小时前
别让 API Key 裸奔:基于 TRAE SOLO 的大模型安全配置最佳实践
人工智能
Python私教3 小时前
Vibe Coding 体验报告:我让 TRAE SOLO 替我重构了 2000 行屎山代码,结果...
人工智能