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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <stdlib.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
using namespace cv;
/*************************************** FONCTION SOBEL + THRESHOLD ************************************************/
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/Kopie von assemblage sobel + threshold/Release/image sans contre jour.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 );
//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*/
cvThreshold(mono_thres,mono_thres,60, /*70 is the lower cut off*/
140, /*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" );
/**************************************************** HOUGH Fonction ***************************************************/
//Mat gray;
/*if( argc != 2 && !(img=imread(argv[1], 1)).data)
return -1;*/
Mat gray(mono_thres);
Mat img(mono_thres);
//cvtColor(img, gray, CV_BGR2GRAY);
// smooth it, otherwise a lot of false circles may be detected
//GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
vector<Vec3f> circles;
HoughCircles(gray, circles, CV_HOUGH_GRADIENT,
2, gray.rows/4, 200, 100 );
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// draw the circle center
circle( img, center, 3, Scalar(255,0,0), -1, 8, 0 );
// draw the circle outline
circle( img, center, radius, Scalar(255,0,0), 3, 8, 0 );
}
namedWindow( "circles", 1 );
imshow( "circles", img );
waitKey(0);
return 0;
} |
Partager