2013-03-14 68 views
0

我有下面的代碼:cvSobel問題 - OpenCV的

// Image Processing.cpp : Defines the entry point for the console application. 
// 
//Save an available image. 
#include "stdafx.h" 
#include "cv.h" 
#include "highgui.h" 
#include "cxcore.h" 
/* 
The purpose of this program is to show an example of THRESHOLDING. 
*/ 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
IplImage* src = cvLoadImage("D:\\document\\Study\\university of  technology\\semester_8\\Computer Vision\\Pics for test\\black-white 4.jpg"); 
IplImage* dst = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,3); 
IplImage* temp1 = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); 
IplImage* temp2 = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); 
cvCvtColor(src,temp1,CV_RGB2GRAY); 
cvSobel(temp1,temp2,0,1,3); 
cvMerge(temp2,temp2,temp2,NULL,dst); 
cvNamedWindow("src",1); 
cvNamedWindow("dst",1); 

cvShowImage("src",src); 
cvShowImage("dst",temp2); 

cvWaitKey(0); 

cvReleaseImage(&src); 
//cvReleaseImage(&dst); 
cvDestroyAllWindows(); 
return 0; 
} 

當我運行它,有一個警告,如下圖: enter image description here

,但如果我仍然點擊「countinue」按鈕,結果顯示! enter image description here

希望有人能給我一個解釋!

回答

1

結果是正確的。該程序的描述不是。您的xorder = 0和yorder = 1這意味着您正在檢測y方向上的一階導數。圖像中的白色像素對應於可以通過垂直導數檢測到的邊界,即儘可能接近水平邊界。這就是爲什麼垂直線幾乎沒有被發現。

CvSobel本身與閾值無關。 CvSobel是用於查找邊界和輪廓的函數。閾值處理最常見的操作是從灰度圖像創建黑白圖像。它也被稱爲圖像二值化。

如果要爲圖像設置閾值,請從cvThreshold和cvAdaptiveThreshold開始。

0

我已經解決了,這裏是我的代碼:

// Image Processing.cpp : Defines the entry point for the console application. 
// 
//Save an available image. 
#include "stdafx.h" 
#include "cv.h" 
#include "highgui.h" 
#include "cxcore.h" 
/* 
The purpose of this program is to show an example of Sobel method. 
*/ 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
IplImage* src = cvLoadImage("D:\\document\\Study\\university of technology\\semester_8\\Computer Vision\\Pics for test\\black-white 4.jpg"); 
IplImage* dst = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); 
IplImage* dst_x = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); 
IplImage* dst_y = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); 
IplImage* temp1 = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); 
IplImage* temp2 = cvCreateImage(cvGetSize(src),IPL_DEPTH_16S,1); 

cvCvtColor(src,temp1,CV_RGB2GRAY); 

cvSobel(temp1,temp2,0,1,3); 
cvConvertScale(temp2,dst_y,1.0,0); 

cvSobel(temp1,temp2,1,0,3); 
cvConvertScale(temp2,dst_x,1.0,0); 

//k nen dao ham cung luc theo x va y ma nen dao ham rieng roi dung ham cvAdd. 
//cvSobel(temp1,temp2,1,1,3); 
//cvConvertScale(temp2,dst,1.0,0); 

cvAdd(dst_x,dst_y,dst,NULL); 

cvNamedWindow("src",1); 
cvNamedWindow("dst",1); 
cvNamedWindow("dst_x",1); 
cvNamedWindow("dst_y",1); 

cvShowImage("src",src); 
cvShowImage("dst",dst); 
cvShowImage("dst_x",dst_x); 
cvShowImage("dst_y",dst_y); 

cvWaitKey(0); 

cvReleaseImage(&src); 
cvReleaseImage(&dst); 
cvReleaseImage(&temp1); 
cvReleaseImage(&temp2); 
cvDestroyAllWindows(); 
return 0; 
} 
+1

我們必須使用cvConvertScale功能! – 2013-03-15 13:59:02