Image 基于CUDA和OpenCV的Sobel边缘检测在灰度jpg图像上的应用
这个问题以前已经被问过了,但是提问者没有提供足够的信息,没有回答,我对这个项目很好奇 我正在尝试使用opencv和cuda库进行sobel边缘检测, X方向的sobel核是Image 基于CUDA和OpenCV的Sobel边缘检测在灰度jpg图像上的应用,image,opencv,cuda,Image,Opencv,Cuda,这个问题以前已经被问过了,但是提问者没有提供足够的信息,没有回答,我对这个项目很好奇 我正在尝试使用opencv和cuda库进行sobel边缘检测, X方向的sobel核是 -1 0 1 -2 0 2 -1 0 1 我的项目中有3个文件 main.cpp CudaKernel.cu CudaKernel.h main.cpp #include <stdlib.h> #include <iostream> #include <string.h&g
-1 0 1
-2 0 2
-1 0 1
我的项目中有3个文件
main.cpp
CudaKernel.cu
CudaKernel.h
main.cpp
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <Windows.h>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\gpu\gpu.hpp>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include "CudaKernel.h"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
IplImage* image;
try
{
image = cvLoadImage("4555472_460s.jpg", CV_LOAD_IMAGE_GRAYSCALE);
gpu::DeviceInfo info = gpu::getDevice();
cout << info.name() << endl;
cout << "Stream Processor : "<< info.multiProcessorCount() << endl;
cout << "Total Graphic Memory :" << info.totalMemory()/1048576 << " MB" << endl;
}
catch (const cv::Exception* ex)
{
cout << "Error: " << ex->what() << endl;
}
if(!image )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
IplImage* image2=cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,image->nChannels);
IplImage* image3=cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,image->nChannels);
unsigned char * pseudo_input=(unsigned char *)image->imageData;
float *output=(float*)image2->imageData;
float *input=(float*)image3->imageData;
int s=image->widthStep/sizeof(float);
for(int w=0;w<=(image->height);w++)
for(int h=0;h<(image->width*image->nChannels);h++)
{
input[w*s+h]= pseudo_input[w*s+h];
}
Pixel *fagget = (unsigned char*) image->imageData;
kernelcall(input, output, image->width,image->height, image->widthStep);
// cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
cvShowImage( "Original Image", image ); // Show our image inside it.
cvShowImage("Sobeled Image", image2);
waitKey(0); // Wait for a keystroke in the window
return 0;
}
#include <iostream>
#include <opencv2/opencv.hpp>
#include "CudaKernel.h"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
IplImage* image;
image = cvLoadImage("4555472_460s.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if(!image )
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
IplImage* image2 = cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,image->nChannels);
IplImage* image3 = cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,image->nChannels);
//Convert the input image to float
cvConvert(image,image3);
float *output = (float*)image2->imageData;
float *input = (float*)image3->imageData;
kernelcall(input, output, image->width,image->height, image3->widthStep);
//Normalize the output values from 0.0 to 1.0
cvScale(image2,image2,1.0/255.0);
cvShowImage("Original Image", image );
cvShowImage("Sobeled Image", image2);
cvWaitKey(0);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“CudaKernel.h”
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
IplImage*图像;
尝试
{
image=cvLoadImage(“4555472_460s.jpg”,CV_LOAD_image_灰度);
gpu::DeviceInfo info=gpu::getDevice();
cout图像数据;
浮点*输入=(浮点*)图像3->图像数据;
float*inputnormalized=(float*)image4->imageData;
int s=图像->宽度步长/sizeof(浮点);
for(int w=0;wheight);w++)
对于(int h=0;hwidth*image->n通道);h++)
{
输入[w*s+h]=伪_输入[w*s+h];
}
内核调用(输入、输出、图像->宽度、图像->高度、图像->宽度步长);
cvNormalize(输入、输入规范化、0255、标准最小值、CV 8UC1);
cvShowImage(“原始图像”,Image);//在其中显示我们的图像。
cvShowImage(“Sobeled图像”,图像2);
但是现在我得到一个未处理的异常错误。这里:-
Here:-
unsigned char * pseudo_input=(unsigned char *)image->imageData;
float *output=(float*)image2->imageData;
float *input=(float*)image3->imageData;
int s=image->widthStep/sizeof(float);
for(int w=0;w<=(image->height);w++)
for(int h=0;h<(image->width*image->nChannels);h++)
{
input[w*s+h]= pseudo_input[w*s+h];
}
无符号字符*伪_输入=(无符号字符*)图像->图像数据;
浮点*输出=(浮点*)图像2->图像数据;
浮点*输入=(浮点*)图像3->图像数据;
int s=图像->宽度步长/sizeof(浮点);
for(int w=0;wheight);w++)
对于(int h=0;hwidth*image->n通道);h++)
{
输入[w*s+h]=伪_输入[w*s+h];
}
输入为float*,伪输入为uchar*。将所有内容转换为float,然后进行处理。最后使用cvNormalize with NORM_MINMAX在0和255之间进行规格化,以获得正确的结果。OpenCV规则编号1:
切勿通过基础数据指针直接访问图像数据,除非
绝对必要,例如将数据复制到GPU。参考(Me:p)
错误/建议:
cvConvert
更改图像数据类型。循环非常有用
很容易出错kernelcall
的函数时,传递的是
float
图像的数据指针,但传递
原始8位图像。这是导致以下错误结果的主要原因:
这将导致内核内的索引错误cudaMemcpy2D
,cudaMemcpy2DToArray
等。在您的情况下,cuArray
内部具有未知的宽度步长,并且输入的IplImage
与cuArray
具有不同的宽度步长#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <Windows.h>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\gpu\gpu.hpp>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include "CudaKernel.h"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
IplImage* image;
try
{
image = cvLoadImage("4555472_460s.jpg", CV_LOAD_IMAGE_GRAYSCALE);
gpu::DeviceInfo info = gpu::getDevice();
cout << info.name() << endl;
cout << "Stream Processor : "<< info.multiProcessorCount() << endl;
cout << "Total Graphic Memory :" << info.totalMemory()/1048576 << " MB" << endl;
}
catch (const cv::Exception* ex)
{
cout << "Error: " << ex->what() << endl;
}
if(!image )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
IplImage* image2=cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,image->nChannels);
IplImage* image3=cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,image->nChannels);
unsigned char * pseudo_input=(unsigned char *)image->imageData;
float *output=(float*)image2->imageData;
float *input=(float*)image3->imageData;
int s=image->widthStep/sizeof(float);
for(int w=0;w<=(image->height);w++)
for(int h=0;h<(image->width*image->nChannels);h++)
{
input[w*s+h]= pseudo_input[w*s+h];
}
Pixel *fagget = (unsigned char*) image->imageData;
kernelcall(input, output, image->width,image->height, image->widthStep);
// cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
cvShowImage( "Original Image", image ); // Show our image inside it.
cvShowImage("Sobeled Image", image2);
waitKey(0); // Wait for a keystroke in the window
return 0;
}
#include <iostream>
#include <opencv2/opencv.hpp>
#include "CudaKernel.h"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
IplImage* image;
image = cvLoadImage("4555472_460s.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if(!image )
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
IplImage* image2 = cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,image->nChannels);
IplImage* image3 = cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,image->nChannels);
//Convert the input image to float
cvConvert(image,image3);
float *output = (float*)image2->imageData;
float *input = (float*)image3->imageData;
kernelcall(input, output, image->width,image->height, image3->widthStep);
//Normalize the output values from 0.0 to 1.0
cvScale(image2,image2,1.0/255.0);
cvShowImage("Original Image", image );
cvShowImage("Sobeled Image", image2);
cvWaitKey(0);
return 0;
}
#包括
#包括
#包括“CudaKernel.h”
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
IplImage*图像;
image=cvLoadImage(“4555472_460s.jpg”,CV_LOAD_image_灰度);
如果(!图像)
{
壁炉);
//将输入图像转换为浮点
cvConvert(图像,图像3);
浮点*输出=(浮点*)图像2->图像数据;
浮点*输入=(浮点*)图像3->图像数据;
内核调用(输入,输出,图像->宽度,图像->高度,图像3->宽度步长);
//将输出值从0.0规范化为1.0
cvScale(图像2,图像2,1.0/255.0);
cvShowImage(“原始图像”,图像);
cvShowImage(“Sobeled图像”,图像2);
cvWaitKey(0);
返回0;
}
CudaKernel.cu
#include<cuda.h>
#include<iostream>
#include "CudaKernel.h"
using namespace std;
#define CudaSafeCall( err ) __cudaSafeCall( err, __FILE__, __LINE__ )
#define CudaCheckError() __cudaCheckError( __FILE__, __LINE__ )
#define checkCudaErrors(err) __checkCudaErrors (err, __FILE__, __LINE__)
texture <float,2,cudaReadModeElementType> tex1;
texture<unsigned char, 2> tex;
static cudaArray *array = NULL;
static cudaArray *cuArray = NULL;
//Kernel for x direction sobel
__global__ void implement_x_sobel(float* garbage,float* output,int width,int height,int widthStep)
{
int x=blockIdx.x*blockDim.x+threadIdx.x;
int y=blockIdx.y*blockDim.y+threadIdx.y;
float output_value=((0*tex2D(tex1,x,y))+(2*tex2D(tex1,x+1,y))+(-2*tex2D(tex1,x- 1,y))+(0*tex2D(tex1,x,y+1))+(1*tex2D(tex1,x+1,y+1))+(-1*tex2D(tex1,x-1,y+1))+ (1*tex2D(tex1,x+1,y-1))+(0*tex2D(tex1,x,y-1))+(-1*tex2D(tex1,x-1,y-1)));
output[y*widthStep+x]=output_value;
}
inline void __checkCudaErrors( cudaError err, const char *file, const int line )
{
if( cudaSuccess != err) {
fprintf(stderr, "%s(%i) : CUDA Runtime API error %d: %s.\n",
file, line, (int)err, cudaGetErrorString( err ) );
exit(-1);
}
}
//Host Code
inline void __cudaSafeCall( cudaError err, const char *file, const int line )
{
#ifdef CUDA_ERROR_CHECK
if ( cudaSuccess != err )
{
printf("cudaSafeCall() failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( -1 );
}
#endif
return;
}
inline void __cudaCheckError( const char *file, const int line )
{
#ifdef CUDA_ERROR_CHECK
cudaError err = cudaGetLastError();
if ( cudaSuccess != err )
{
printf("cudaCheckError() failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( -1 );
}
#endif
return;
}
void kernelcall(float* input,float* output,int width,int height,int widthStep){
//cudaChannelFormatDesc channelDesc=cudaCreateChannelDesc(32,32,0,0,cudaChannelFormatKindFloat);
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
//cudaArray *cuArray;
CudaSafeCall(cudaMallocArray(&cuArray,&channelDesc,width,height));
cudaMemcpyToArray(cuArray,0,0,input,widthStep*height,cudaMemcpyHostToDevice);
tex1.addressMode[0]=cudaAddressModeClamp;
tex1.addressMode[1]=cudaAddressModeClamp;
tex1.filterMode=cudaFilterModeLinear;
cudaBindTextureToArray(tex1,cuArray,channelDesc);
tex1.normalized=false;
float * D_output_x;
float * garbage=NULL;
CudaSafeCall(cudaMalloc(&D_output_x,widthStep*height));
dim3 blocksize(16,16);
dim3 gridsize;
gridsize.x=(width+blocksize.x-1)/blocksize.x;
gridsize.y=(height+blocksize.y-1)/blocksize.y;
implement_x_sobel<<<gridsize,blocksize>>>(garbage,D_output_x,width,height,widthStep/sizeof(float));
cudaThreadSynchronize();
CudaCheckError();
CudaSafeCall(cudaMemcpy(output,D_output_x,height*widthStep,cudaMemcpyDeviceToHost));
cudaFree(D_output_x);
cudaFree(garbage);
cudaFreeArray(cuArray);
}
#include<cuda.h>
#include<iostream>
#include "CudaKernel.h"
using namespace std;
#define CudaSafeCall( err ) __cudaSafeCall( err, __FILE__, __LINE__ )
#define CudaCheckError() __cudaCheckError( __FILE__, __LINE__ )
#define checkCudaErrors(err) __checkCudaErrors (err, __FILE__, __LINE__)
texture <float,2,cudaReadModeElementType> tex1;
static cudaArray *cuArray = NULL;
//Kernel for x direction sobel
__global__ void implement_x_sobel(float* output,int width,int height,int widthStep)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
//Make sure that thread is inside image bounds
if(x<width && y<height)
{
float output_value = (-1*tex2D(tex1,x-1,y-1)) + (0*tex2D(tex1,x,y-1)) + (1*tex2D(tex1,x+1,y-1))
+ (-2*tex2D(tex1,x-1,y)) + (0*tex2D(tex1,x,y)) + (2*tex2D(tex1,x+1,y))
+ (-1*tex2D(tex1,x-1,y+1)) + (0*tex2D(tex1,x,y+1)) + (1*tex2D(tex1,x+1,y+1));
output[y*widthStep+x]=output_value;
}
}
inline void __checkCudaErrors( cudaError err, const char *file, const int line )
{
if( cudaSuccess != err) {
fprintf(stderr, "%s(%i) : CUDA Runtime API error %d: %s.\n",
file, line, (int)err, cudaGetErrorString( err ) );
exit(-1);
}
}
//Host Code
inline void __cudaSafeCall( cudaError err, const char *file, const int line )
{
#ifdef CUDA_ERROR_CHECK
if ( cudaSuccess != err )
{
printf("cudaSafeCall() failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( -1 );
}
#endif
return;
}
inline void __cudaCheckError( const char *file, const int line )
{
#ifdef CUDA_ERROR_CHECK
cudaError err = cudaGetLastError();
if ( cudaSuccess != err )
{
printf("cudaCheckError() failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( -1 );
}
#endif
return;
}
void kernelcall(float* input,float* output,int width,int height,int widthStep)
{
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
CudaSafeCall(cudaMallocArray(&cuArray,&channelDesc,width,height));
//Never use 1D memory copy if host and device pointers have different widthStep.
// You don't know the width step of CUDA array, so its better to use cudaMemcpy2D...
cudaMemcpy2DToArray(cuArray,0,0,input,widthStep,width * sizeof(float),height,cudaMemcpyHostToDevice);
cudaBindTextureToArray(tex1,cuArray,channelDesc);
float * D_output_x;
CudaSafeCall(cudaMalloc(&D_output_x,widthStep*height));
dim3 blocksize(16,16);
dim3 gridsize;
gridsize.x=(width+blocksize.x-1)/blocksize.x;
gridsize.y=(height+blocksize.y-1)/blocksize.y;
implement_x_sobel<<<gridsize,blocksize>>>(D_output_x,width,height,widthStep/sizeof(float));
cudaThreadSynchronize();
CudaCheckError();
//Don't forget to unbind the texture
cudaUnbindTexture(tex1);
CudaSafeCall(cudaMemcpy(output,D_output_x,height*widthStep,cudaMemcpyDeviceToHost));
cudaFree(D_output_x);
cudaFreeArray(cuArray);
}
#包括
#包括
#包括“CudaKernel.h”
使用名称空间std;
#定义CudaSafeCall(err)\ CudaSafeCall(err、\文件、\行)
#定义CudaCheckError()\uuuu CudaCheckError(\uuuuu文件\uuuuu,\uuuuu行\uuuu)
#定义校验错误(err)\校验错误(err、\文件、\行)
纹理tex1;
静态cudaArray*cuArray=NULL;
//x方向sobel核
__全局\uuuuvoid工具\ux\uSobel(浮点*输出、整数宽度、整数高度、整数宽度步长)
{
intx=blockIdx.x*blockDim.x+threadIdx.x;
int y=blockIdx.y*blockDim.y+threadIdx.y;
//确保线程在图像边界内
if(xok)我更改了代码,但现在我得到一个未处理的异常错误。处理后进行规格化。使用cvNormalize(输入,输入规格化,0255,NORM_MINMAX,CV_8UC1)因此,在Cuda内核调用后对其进行规范化?cvNormalize函数总是导致未经处理的异常错误,请查看我的帖子,我更新了代码您不能将normalize用于float*。必须进行规范化。通常,建议您就某个问题开始悬赏,而不是询问可能的重复项。如果您只是在搜索gpu sobel筛选器,gpu opencv已经提供了几个过滤函数,看起来它们也有一个。你的代码工作得很完美,也很简单,谢谢你指出我的错误!我真的很感激你的代码非常有用。但是我有一个问题:为什么要使用浮点数组来存储输入和输出?我可以使用无符号字符数组吗?我使用浮点,因为OP是有意的使用它。是的,您可以使用无符号字符
。在这种情况下,您必须创建深度为IPL\U depth\U 8U
的主机映像image2
和image3
。