1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <stdlib.h>
#include "StdAfx.h"
using namespace cv;
int main( )
{
int height,width,step,step_mono,channels;
/*Here i have declared step_mono for handling the widthstep member of a monochrome image*/
uchar *data,*data_mono; /*similarly data mono for handling the data of monochrome image*/
int i,j,k;
//Mat src;
Mat src_gray;
Mat grad;
char* window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
int c;
/// Load an image
IplImage* frame=cvLoadImage("D:/Entwicklung/OpenCV/2010/test threshold/Release/sobel.bmp",1);
if( frame == NULL )
{ return -1; }
Mat src(frame);
GaussianBlur(src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
/// Convert it to gray
cvtColor( src, src_gray, CV_RGB2GRAY );
/// Create window
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
imshow( window_name, grad );
//waitKey(0);
//Resultat de grad dans IPL ....
//IplImage mono_thres=cvCreateImage( cvGetSize(frame), 8, 1 );
IplImage frame2(grad);
//Faire à la fin
IplImage* mono_thres=cvCreateImage( cvGetSize(&frame2), 8, 1 );
height = frame2.height; /*height is a member of IPLIMAGE structure and hence it comes handy like this in such situations, and same goes with below four statements*/
width = frame2.width;
step = frame2.widthStep;
step_mono = mono_thres->widthStep;
channels = frame2.nChannels; /*Number of channels in the image*/
data = (uchar *)frame2.imageData; /*Image is treated as as unsigned char data hence we use an unsigned char pointer to point to the same*/
cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE );
data_mono = (uchar *)mono_thres-> imageData; /*data of mono image is handled by the data_mono*/
for(i=0;i < height;i++) for(j=0;j < width;j++) /*I am copying the first channel from the image in "frame" in the monochrome image with the help of this line below..*/
data_mono[i*step_mono+j*1+0]=data[i*step+j*channels+0];
cvThreshold(mono_thres,mono_thres,12, /*70 is the lower cut off*/
40, /*this is the higher cut off*/
CV_THRESH_BINARY /*The type of thresholding,more description in the documentation*/
);
//imshow( window_name, grad );
cvShowImage("My Window",mono_thres);
waitKey(0);
cvDestroyWindow( "My Window" );
return 0;
} |
Partager