当我在windows7 x64上运行vs2008中的程序时,使用cuda4.1和opencv2.3.it会出现“应用程序无法正常启动(0xc000007b)....”,没有编译和链接错误,它只发生在运行时,它让我疯狂。任何帮助将是欣赏!谢谢!
这是我的代码:
#include <cutil_math.h>
#include <cutil_inline.h>
#include <highgui.h>
#include <cv.h>
#include <cxcore.h>
#include <book.h>
#include <cpu_bitmap.h>
#define ThreadX 16
#define ThreadY 16
texture<float, 2, cudaReadModeElementType> texRefSource;
texture<float, 2, cudaReadModeElementType> texRefTarget;
__constant__ float c_trans[9];
__global__ void transformKernel(float* dev_bitmap,int width, int height)
{
//计算拾取坐标
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
float3 v=make_float3(x,y,1);
float3 r0=make_float3(c_trans[0],c_trans[1],c_trans[2]);
float3 r1=make_float3(c_trans[3],c_trans[4],c_trans[5]);
float3 r2=make_float3(c_trans[6],c_trans[7],c_trans[8]);
float tz=dot(r2,v);
float tx=dot(r0,v)/tz;
float ty=dot(r1,v)/tz;
dev_bitmap[y * width + x] = tex2D(texRefTarget, tx, ty);
}
int main()
{
// load and set source file
IplImage* source=cvLoadImage("C:\\1368.bmp");
IplImage* sourcegray =cvCreateImage(cvGetSize(source),source->depth,1);
cvCvtColor(source,sourcegray,CV_BGR2GRAY);//image1图像灰度化
int w1 = sourcegray->width;
int h1 = sourcegray->height;
int size1=w1*h1;
cudaArray* SourcecuArray;
cudaChannelFormatDesc channelDesc1 = cudaCreateChannelDesc(32, 0, 0, 0,cudaChannelFormatKindFloat);
cudaMallocArray(&SourcecuArray, &channelDesc1, w1, h1);
cudaMemcpyToArray(SourcecuArray, 0, 0, sourcegray->imageData,size1,cudaMemcpyHostToDevice);
texRefSource.addressMode[0] = cudaAddressModeWrap; //循环寻址方式
texRefSource.addressMode[1] = cudaAddressModeWrap;
texRefSource.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(texRefSource, SourcecuArray, channelDesc1);
// load and set target file
IplImage* target=cvLoadImage("C:\\1369.bmp");
IplImage* targetgray =cvCreateImage(cvGetSize(target),target->depth,1);
cvCvtColor(target,targetgray,CV_BGR2GRAY);//image1图像灰度化
cvNamedWindow("before", CV_WINDOW_AUTOSIZE );
cvShowImage("before", targetgray);
int w2 = targetgray->width;
int h2 = targetgray->height;
int size2=w2*h2;
cudaArray* TargetcuArray;
cudaChannelFormatDesc channelDesc2 = cudaCreateChannelDesc(32, 0, 0, 0,cudaChannelFormatKindFloat);
cudaMallocArray(&TargetcuArray, &channelDesc2, w2, h2);
cudaMemcpyToArray(TargetcuArray, 0, 0, targetgray->imageData,size2,cudaMemcpyHostToDevice);
texRefTarget.addressMode[0] = cudaAddressModeWrap; //循环寻址方式
texRefTarget.addressMode[1] = cudaAddressModeWrap;
texRefTarget.filterMode = cudaFilterModeLinear;
cudaBindTextureToArray(texRefTarget, TargetcuArray, channelDesc2);
//----------------------构造从image1----->image2的变换矩阵,存入常量内存c_trans----------//
float u[9]={-0.00363945, 123.461, 123.154, 1.0105, 0.868716, 1.2, 1.2, -0.000110196, 0.000174003};
float matM[3][3]={ u[3]*cos(u[0]), u[4]*sin(u[0]), 0,
-u[5]*sin(u[0]), u[6]*cos(u[0]), 0,
u[7], u[8], 1};
CvMat M = cvMat(3,3,CV_32FC1,matM); //求得的是image2-->image1的M。
CvMat invM = cvMat(3,3,CV_32FC1,matM); //求得的是image1-->image2的M。
cvInvert(&M,&invM);
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
c_trans[i*3+j]=CV_MAT_ELEM(invM,float,i,j);
}
}
//-------------------------------------------------------------------------------------------------//
float *dev_bitmap=NULL;
int size=size1*sizeof(float);
cudaMalloc((void**)&dev_bitmap, size);
//开始变换图像
dim3 blockSize(ThreadX, ThreadY);
dim3 gridSize( (w1+ThreadX-1) / blockSize.x, (h1+ThreadY-1) / blockSize.y);
transformKernel<<<gridSize, blockSize>>>(dev_bitmap,w1,h1);
CPUBitmap bitmap(w1,h1);
cudaMemcpy(bitmap.get_ptr(),(unsigned char*)dev_bitmap,bitmap.image_size(),cudaMemcpyDeviceToHost);
bitmap.display_and_exit();
cvWaitKey(0);
cvReleaseImage(&source);
cvReleaseImage(&sourcegray);
cvReleaseImage(&target);
cvReleaseImage(&targetgray);
cvDestroyAllWindows();
cudaFreeArray(SourcecuArray);
cudaFreeArray(TargetcuArray);
cudaFree(dev_bitmap);
return 0;
}