我正在尝试在 Cuda c 中制作影像过滤器,但我认为我不完全了解每个像素的执行绪分配是如何作业的。到目前为止,这是我的代码:imageFilter.cu
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "Filtering_Functions.h"
__global__ void Grayscale_image(int h, int w, unsigned char* Image) {
int x = (blockIdx.x * blockDim.x) threadIdx.x;
int y = (blockIdx.y * blockDim.y) threadIdx.y;
unsigned int tid = threadIdx.y * blockDim.y threadIdx.x;
if (x > 0 && x < w - 1 && y > 0 && y < h - 1)
{
Image[tid] = 0.299 * Image[tid] 0.587 * Image[tid 1] 0.114 * Image[tid 2];
Image[tid 1] = Image[tid];
Image[tid 2] = Image[tid];
}
}
void Image_Grayscale(unsigned char* Image, int Height, int Width) {
unsigned char* Uploaded_Image = NULL;
dim3 blocks(Width / 16, Height / 16);
dim3 threads(16, 16);
cudaMalloc((void**)&Uploaded_Image, Height * Width * 3);
cudaMemcpy(Uploaded_Image, Image, Height * Width * 3, cudaMemcpyHostToDevice);
Grayscale_image << <blocks, threads >> > (Height, Width, Uploaded_Image);
cudaMemcpy(Image, Uploaded_Image, Height * Width * 3, cudaMemcpyDeviceToHost);
cudaFree(Uploaded_Image);
}
过滤功能.h
#ifndef filtering_functions
#define filtering_functions
void Image_Grayscale(unsigned char* Image, int Height, int Width);
#endif
影像过滤.cpp
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include "Filtering_Functions.h"
using namespace std;
using namespace cv;
int main() {
Mat Image = imread("Example.png");
cout << "The uploaded image has Height: " << Image.rows << ", Width: " << Image.cols << endl;
Image_Grayscale(Image.data, Image.rows, Image.cols);
imwrite("Grayscale_Filter.png", Image);
system("pause");
return 0;
}
最后我没有看到任何变化。有人可以告诉我我做错了什么,或者至少我不明白什么?
uj5u.com热心网友回复:
Cuda 操作相对于主机处理器是异步的。我怀疑您正试图在 GPU 触及资料之前将资料写入磁盘。考虑cudaStreamSynchronize(0)
在尝试检查结果之前打电话。
Cuda 流管理
uj5u.com热心网友回复:
您提供的代码无法编译 - 您Image2
在此行中使用了未宣告的识别符号:
imwrite("Grayscale_Filter.png", Image2);
也许这就是问题?您是否在档案中写入其他内容?
0 评论