using namespace std;
using namespace cv;
#include //图像大小
#define DATA_W 512
#define DATA_H 512
//thread
#define N 16
__global__ void MediaFilter(uchar* In,uchar* Out, int Width, intHeight)
{
ucharwindow[9];
unsigned int x=blockIdx.x * blockDim.x+ threadIdx.x;
unsigned int y=blockIdx.y * blockDim.y+ threadIdx.y;
//中值滤波
if(x>=Width && y>=Height) {printf("break %s\n","");return;}
window[0]=(y==0||x==0)?0:In[(y - 1 ) * Width + x - 1];
window[1]=(y==0)?0:In[( y - 1 ) * Width + x ];
window[2]=(y==0||x==DATA_W-1)?0:In[( y - 1 ) * Width + x + 1 ];
window[3]=(x=0)?0:In[y * Width + x ];
window[4]=In[y * Width + x ];
window[5]=(x==DATA_W-1)?0:In[y * Width + x + 1 ];
window[6]=(y==DATA_H-1|| x == 0)?0:In[ (y + 1 ) * Width + x - 1 ];
window[7]=(y==DATA_H-1)?0:In[( y + 1 ) * Width + x];
window[8]=(y==DATA_H-1||x==DATA_W-1)?0:In[( y + 1 ) * Width + x + 1 ];
for (unsigned int j = 0; j < 5; ++j)
{
int min1 = j;
for (unsigned int l=j+1; l<9; ++l)
{
if (window[l] < window[min1] )
{
min1= l;
}
}
const uchar temp = window[j];
window[j]= window[min1];
window[min1]= temp;
}
Out[y*Width+ x] = window[4];
}
int main()
{
Mat Img =imread("C:\\Users\\scczyy\\Desktop\\study\\ImageMedia\\lena512.bmp",1);
cvtColor(Img,Img,CV_BGR2GRAY);
int Height = Img.rows;
int Width = Img.cols;
int Len = Height*Width;
int MemSize = Len*sizeof(uchar);
printf("data type is %i\n",Img.type());
printf(" the image width is %i\n the image height is%i\n",Width,Height);
uchar*dev_Img;
uchar*dev_OutImg;
cudaMalloc((void**)&dev_Img,MemSize);
cudaMalloc((void**)&dev_OutImg,MemSize);
cudaMemcpy(dev_Img,Img.data,MemSize,cudaMemcpyHostToDevice);
dim3 threadsPerBlock(N,N);
dim3 blocks((Width+threadsPerBlock.x-1)/threadsPerBlock.x, (Height+threadsPerBlock.y-1)/threadsPerBlock.y);
MediaFilter<<>>(dev_Img,dev_OutImg,Width,Height);
Mat OutImg =Mat::zeros(Img.rows,Img.cols, CV_8UC1);
cudaMemcpy(OutImg.data,dev_OutImg,MemSize,cudaMemcpyDeviceToHost);
imshow("中值滤波",OutImg);
waitKey(0);
cudaFree(dev_Img);
cudaFree(dev_OutImg);
return 0;
}解决啦,红色部分错啦