我从Internet粘贴cuda代码并运行它们但是在执行侵蚀和扩展功能时,控制台窗口输出错误:
OpenCV Error: Assertion failed (m.dims >= 2) in cv::Mat::Mat, file
C:\builds\2_4_PackSlave-win32-vc12-
shared\opencv\modules\core\src\matrix.cpp,
line 269
如果我删除侵蚀和扩张功能,它们可以工作。 产生错误信息的侵蚀和扩张功能是opencv内置函数,而不是erodeInCuda或dilateInCuda。我在下面的代码中对它们进行了评论。
以下是代码:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <cuda.h>
#include <device_functions.h>
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
__global__ void erodeInCuda(unsigned char *dataIn, unsigned char *dataOut, Size erodeElement, int imgWidth, int imgHeight)
{
int xIndex = threadIdx.x + blockIdx.x * blockDim.x;
int yIndex = threadIdx.y + blockIdx.y * blockDim.y;
int elementWidth = erodeElement.width;
int elementHeight = erodeElement.height;
int halfEW = elementWidth / 2;
int halfEH = elementHeight / 2;
dataOut[yIndex * imgWidth + xIndex] = dataIn[yIndex * imgWidth + xIndex];
if (xIndex > halfEW && xIndex < imgWidth - halfEW && yIndex > halfEH && yIndex < imgHeight - halfEH)
{
for (int i = -halfEH; i < halfEH + 1; i++)
{
for (int j = -halfEW; j < halfEW + 1; j++)
{
if (dataIn[(i + yIndex) * imgWidth + xIndex + j] < dataOut[yIndex * imgWidth + xIndex])
{
dataOut[yIndex * imgWidth + xIndex] = dataIn[(i + yIndex) * imgWidth + xIndex + j];
}
}
}
}
}
__global__ void dilateInCuda(unsigned char *dataIn, unsigned char *dataOut, Size dilateElement, int imgWidth, int imgHeight)
{
int xIndex = threadIdx.x + blockIdx.x * blockDim.x;
int yIndex = threadIdx.y + blockIdx.y * blockDim.y;
int elementWidth = dilateElement.width;
int elementHeight = dilateElement.height;
int halfEW = elementWidth / 2;
int halfEH = elementHeight / 2;
dataOut[yIndex * imgWidth + xIndex] = dataIn[yIndex * imgWidth + xIndex];
if (xIndex > halfEW && xIndex < imgWidth - halfEW && yIndex > halfEH && yIndex < imgHeight - halfEH)
{
for (int i = -halfEH; i < halfEH + 1; i++)
{
for (int j = -halfEW; j < halfEW + 1; j++)
{
if (dataIn[(i + yIndex) * imgWidth + xIndex + j] > dataOut[yIndex * imgWidth + xIndex])
{
dataOut[yIndex * imgWidth + xIndex] = dataIn[(i + yIndex) * imgWidth + xIndex + j];
}
}
}
}
}
int main()
{
Mat srcImg = imread("1.jpg");
Mat grayImg = imread("1.jpg", 0);
unsigned char *d_in;
unsigned char *d_out1;
unsigned char *d_out2;
int imgWidth = grayImg.cols;
int imgHeight = grayImg.rows;
Mat dstImg1(imgHeight, imgWidth, CV_8UC1, Scalar(0));
Mat dstImg2(imgHeight, imgWidth, CV_8UC1, Scalar(0));
cudaMalloc((void**)&d_in, imgWidth * imgHeight * sizeof(unsigned char));
cudaMalloc((void**)&d_out1, imgWidth * imgHeight * sizeof(unsigned char));
cudaMalloc((void**)&d_out2, imgWidth * imgHeight * sizeof(unsigned char));
cudaMemcpy(d_in, grayImg.data, imgWidth * imgHeight * sizeof(unsigned char), cudaMemcpyHostToDevice);
dim3 threadsPerBlock(32, 32);
dim3 blocksPerGrid((imgWidth + threadsPerBlock.x - 1) / threadsPerBlock.x, (imgHeight + threadsPerBlock.y - 1) / threadsPerBlock.y);
Size Element(3, 5);
erodeInCuda << <blocksPerGrid, threadsPerBlock >> >(d_in, d_out1, Element, imgWidth, imgHeight);
cudaMemcpy(dstImg1.data, d_out1, imgWidth * imgHeight * sizeof(unsigned char), cudaMemcpyDeviceToHost);
Mat erodeImg;
Mat element = getStructuringElement(MORPH_RECT, Size(3, 5));
// error happens here
erode(grayImg, erodeImg, element);
dilateInCuda << <blocksPerGrid, threadsPerBlock >> >(d_in, d_out2, Element, imgWidth, imgHeight);
cudaMemcpy(dstImg2.data, d_out2, imgWidth * imgHeight * sizeof(unsigned char), cudaMemcpyDeviceToHost);
Mat dilateImg;
// error happens here.
dilate(grayImg, dilateImg, element);
return 0;
}
那么问题是什么?我看到opencv官方网站给出的例子并没有发现任何错误。 IDE是visual studio 2015,opencv版本是2.4.13。