我正在嘗試在 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);
也許這就是問題?您是否在檔案中寫入其他內容?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/404751.html
標籤:
