是尘封已久的记忆再次融化 是堆积心底的烈火再次燃烧 是直觉让我来到这久违的大地 是信念让我开始了新的征途 在硝烟中我得到了成长 在伤痛中我学会了坚强 在沉默中我明白了等待 在孤独中我体味了感伤 并不是我不懂得眼泪 并不是我只知道使命 在内心深处我同你一样火热 在我的眼中也有着多情的泪光 也许我的生命如落叶般短暂 也许我只是岁月长河中的一个过客 但我对自己所做的一切无怨无悔 因为我品尝到了那最后一刻的泪光
随笔-6  评论-24  文章-8  trackbacks-0
 1#include <cutil_inline.h>
 2#include <cv.h>
 3#include <cstdio>
 4#include <iostream>
 5#include <cutil.h>
 6#include <ctime>
 7#include <cstdlib>
 8#include <highgui.h>
 9#include <windows.h>
10
11#pragma comment(lib, "cuda.lib")
12#pragma comment(lib, "cudart.lib")
13#pragma comment(lib, "cutil32.lib")
14#pragma comment(lib, "cv.lib")
15#pragma comment(lib, "cxcore.lib")
16#pragma comment(lib, "highgui.lib")
17
18using namespace std;
19
20__global__ void mainKernel(unsigned char *d_data, int widthStep, int width, int height)
21{
22    unsigned int x = blockIdx.x*blockDim.x+threadIdx.x;
23    unsigned int y = blockIdx.y*blockDim.y+threadIdx.y;
24    if( x>0 && x < width && y>0 && y < height )
25    {                  
26        d_data[y*widthStep+x*3+0^= ( ((x&0x0F)==0^ ((y&0x0F)==0)  ) *255;
27        d_data[y*widthStep+x*3+1^= ( ((x&0x0F)==0^ ((y&0x0F)==0)  ) *255;
28        d_data[y*widthStep+x*3+2^= ( ((x&0x0F)==0^ ((y&0x0F)==0)  ) *255;
29    }

30}

31
32int main()
33{
34    IplImage* src = cvLoadImage("IMG_03.JPG");
35
36    int widthStep = src->widthStep;
37    int width   = src->width;
38    int height  = src->height;
39
40    printf("before widthStep = %d\n", widthStep);
41    if( widthStep%4 != 0)
42    {
43        widthStep = (1+widthStep/4)*4;
44    }

45    printf("after widthStep = %d\n", widthStep);
46
47    unsigned char* d_img_data;
48    CUDA_SAFE_CALL(cudaMalloc((void**)&d_img_data, widthStep*height));
49    CUDA_SAFE_CALL(cudaMemcpy(d_img_data, src->imageData, widthStep*height, cudaMemcpyHostToDevice));
50
51    dim3 dimBlock(16161);
52    dim3 dimGrid( (width+dimBlock.x-1)/dimBlock.x, (height+dimBlock.y-1)/dimBlock.y );
53    mainKernel<<<dimGrid, dimBlock, 0>>>(d_img_data, widthStep, width, height);
54    CUDA_SAFE_CALL(cudaThreadSynchronize());
55
56    CUDA_SAFE_CALL( cudaMemcpy( src->imageData, d_img_data, widthStep*height, cudaMemcpyDeviceToHost) );
57    
58    cvNamedWindow("test",CV_WINDOW_AUTOSIZE);
59    cvShowImage("test",src);
60    cvWaitKey(0);
61    cvDestroyAllWindows();
62
63    cvReleaseImage(&src);
64    CUDA_SAFE_CALL(cudaFree(d_img_data));
65    return 0;
66}
posted on 2009-12-28 10:58 noBugnoGain 阅读(1751) 评论(1)  编辑 收藏 引用

评论:
# re: 用cuda操作IplImage中的数据 2010-08-23 13:46 | STICKGY
您好,我是OPENCV以及CUDA的初学者,我现在要完成一个任务,就是把OPENCV和CUDA配置到一起,把它俩结合起来编程,虽然我还不是很懂,但是应该就是得到类似于您文章中的效果吧!但是把不清楚应该怎样进行,甚至创建什么项目、在哪种文件(.cpp or .cu)中编程,希望您能给予适当的指教,谢谢!  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理