#include "stdafx.h"#include "opencv2/opencv.hpp"int main( int argc, char** argv ){//變量聲明IplImage* depth; int width,height;int x,y;//函數(shù)聲明/*double int2double(int n);int double2int(double n);*///載入深度圖depth=cvLoadImage("depth.png",1);cvNamedWindow("depthImage",1);cvShowImage("depthImage",depth);//獲取圖片的寬高width=depth->width;height=depth->height;//將載入的圖片轉(zhuǎn)換為灰度圖IplImage* gdepth=cvCreateImage(cvGetSize(depth),IPL_DEPTH_8U,1);cvCvtColor(depth,gdepth,CV_RGB2GRAY); //??為什么深度圖已經(jīng)是灰度圖像了還有轉(zhuǎn)換為灰度圖才可以呢?? //對深度圖進(jìn)行sobel邊緣檢測 IplImage* sobel=cvCloneImage(gdepth); /*double temp_d,temp1_d,sum_d;*/int temp,temp1,sum;uchar* data=(uchar*)(sobel->imageData );//data為一個指針int ptr[8]={0,0,0,0,0,0,0,0}; for(y=1;y<height-1;y++){for(x=1;x<width-1;x++){ ptr[0]=*(data+(y-1)*sobel->widthStep+(x-1));ptr[1]=*(data+(y-1)*sobel->widthStep+x);ptr[2]=*(data+(y-1)*sobel->widthStep+(x+1));ptr[3]=*(data+y*sobel->widthStep+(x-1));ptr[4]=*(data+y*sobel->widthStep+(x+1));ptr[5]=*(data+(y+1)*sobel->widthStep+(x-1)); ptr[6]=*(data+(y+1)*sobel->widthStep+x);ptr[7]=*(data+(y+1)*sobel->widthStep+(x+1));temp=abs(ptr[5]+2*ptr[6]+ptr[7]-ptr[0]-2*ptr[1]-ptr[2]);temp1=abs(ptr[2]+2*ptr[4]+ptr[7]-ptr[0]-2*ptr[3]-ptr[5]); sum=temp>temp1?temp:temp1; if(sum>=255){sum=255;}else{sum=0;}/*printf("%d ",sum);*/*(data+y*sobel->widthStep+x)=sum; } } cvNamedWindow("sobel_depth",1);cvShowImage("sobel_depth",sobel);cvWaitKey(0);//銷毀窗口并釋放內(nèi)存空間cvDestroyWindow("sobel_depth");cvReleaseImage(&sobel);return 0;}
- 1 回答
- 0 關(guān)注
- 289 瀏覽
添加回答
舉報(bào)
0/150
提交
取消