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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
| #include "stdio.h"
#include "string.h"
#include "cv.h"
#include "conio.h"
#include "highgui.h"
#include "math.h"
#ifndef _EiC
#endif
char file_name[] = "test.jpg";
int pos;
int _threshold = 200;
int _brightness = 200;
int _contrast = 200;
int thr_low = 0.2, thr_high = 0.4;
int hist_size = 64;
float range_0[]={0,256};
float* ranges[] = { range_0 };
IplImage *img = 0, *dst_image = 0, *hist_image = 0, *img_bordes, *bn, *krl = 0,*src = 0;
CvHistogram *hist;
uchar lut[256];
CvMat* lut_mat;
//////////////////////////////////////TESTE MORPHOLOGY//
IplConvKernel* element = 0;
int element_shape = CV_SHAPE_RECT;
//the address of variable which receives trackbar position update
int max_iters = 10;
int open_close_pos = 0;
int erode_dilate_pos = 0;
///////////////////////////////////////////////////////////////////////////////////////
/* brightness/contrast callback function */
void update_brightcont( int arg )
{
int brightness = _brightness - 100;
int contrast = _contrast - 100;
int i, bin_w;
float max_value = 0;
if( contrast > 0 )
{
double delta = 127.*contrast/100;
double a = 255./(255. - delta*2);
double b = a*(brightness - delta);
for( i = 0; i < 256; i++ )
{
int v = cvRound(a*i + b);
if( v < 0 )
v = 0;
if( v > 255 )
v = 255;
lut[i] = (uchar)v;
}
}
else
{
double delta = -128.*contrast/100;
double a = (256.-delta*2)/255.;
double b = a*brightness + delta;
for( i = 0; i < 256; i++ )
{
int v = cvRound(a*i + b);
if( v < 0 )
v = 0;
if( v > 255 )
v = 255;
lut[i] = (uchar)v;
}
}
cvLUT( img, dst_image, lut_mat );
cvShowImage( "image", dst_image );
cvCalcHist( &dst_image, hist, 0, NULL );
cvZero( dst_image );
cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
cvScale( hist->bins, hist->bins, ((double)hist_image->height)/max_value, 0 );
/*cvNormalizeHist( hist, 1000 );*/
cvSet( hist_image, cvScalarAll(255), 0 );
bin_w = cvRound((double)hist_image->width/hist_size);
for( i = 0; i < hist_size; i++ )
cvRectangle( hist_image, cvPoint(i*bin_w, hist_image->height),
cvPoint((i+1)*bin_w, hist_image->height - cvRound(cvGetReal1D(hist->bins,i))),
cvScalarAll(0), -1, 8, 0 );
cvShowImage( "histogram", hist_image );
}
///////////////////////////////////////////////////////////////////////////////////////
int ola (void) {
int i,t;
img = cvLoadImage ( file_name , 0 );
if( !img )
{
printf("Image inexistente.\n");
getchar ();
return -1;
}
dst_image = cvCloneImage(img);
hist_image = cvCreateImage(cvSize(320,200), 8, 1);
hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
cvSetData( lut_mat, lut, 0 );
cvNamedWindow("image", 0);
cvMoveWindow("image", 500, 0);
cvNamedWindow("histogram", 0);
cvMoveWindow("histogram", 0, 0);
/*int cvCreateTrackbar( 1, 2, 3, 4, 5)
1 - const char* trackbar_name
2 - const char* window_name
3 - int* value
4 - int count
5 - CvTrackbarCallback on_change
*/
cvCreateTrackbar("brightness", "image", &_brightness, 200, update_brightcont);
cvCreateTrackbar("contrast", "image", &_contrast, 200, update_brightcont);
update_brightcont(0);
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&dst_image);
cvReleaseHist(&hist);
cvDestroyWindow( "Example1" );
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
int foto (CvCapture *capture) {
static int i ;
IplImage *frame2 = 0;
frame2 = cvQueryFrame( capture );
i++;
printf ("%d",i);
cvSaveImage( file_name+i , frame2);
//cvSaveImage( file_name , frame2);
ola ();
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
int captura (int argc, char** argv) {
CvCapture *capture = 0;
IplImage *frame = 0;
int key = 0;
int i=0;
/* initialize camera */
capture = cvCaptureFromCAM( -1 );
/* always check */
if ( !capture ) {
fprintf( stderr, "\nWebcam not detected!\nENTER to continue.." );
getchar();
return 1;
}
/* create a window for the video */
cvNamedWindow( "Camara", CV_WINDOW_AUTOSIZE );
while( key != 'q' ) {
/* get a frame */
frame = cvQueryFrame( capture );
/* always check */
if( !frame ) break;
/* display current frame */
cvShowImage( "Camara", frame );
key = cvWaitKey( 1 );
if (key == 'a')
foto (capture);
if (key == 'q') /* exit if user press 'q' */
break;
}
/* free memory */
cvDestroyWindow( "Camara" );
cvReleaseCapture( &capture );
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
int test ()
{
int i,j,k;
int temp1,temp2,temp3;//Temp variables
int height,width,step,channels;
int stepr, channelsr;
int temp=0;
uchar *data,*datar;
i=j=k=0;
IplImage *frame=cvLoadImage(file_name ,1);
IplImage *result=cvCreateImage( cvGetSize(frame), 8, 1 );
if(frame==NULL ) {
puts("unable to load the frame");exit(0);}
int diff=0;
printf("What do you want the Difference in the pixels to be..? ");
scanf("%d",&diff);
height = frame->height;
width = frame->width;
step =frame->widthStep;
channels = frame->nChannels;
data = (uchar *)frame->imageData;
cvNamedWindow("original",CV_WINDOW_AUTOSIZE);
cvNamedWindow("Result",CV_WINDOW_AUTOSIZE);
/*Here I use the Suffix r to diffenrentiate the result data and the frame data
Example :stepr denotes widthStep of the result IplImage and step is for frame IplImage*/
stepr=result->widthStep;
channelsr=result->nChannels;
datar = (uchar *)result->imageData;
for(i=0;i< (height-1);i++)
{
for(j=0;j<(width-1);j++)
{/*width-1 and height-1 to ovoid memory overleak*/
temp1=data[i*step+j*channels];/*Blue pix assigned to temp and same below*/
temp2=data[i*step+j*channels+1];/**/
temp3=data[i*step+j*channels+2];
if((temp1>(data[(i+1)*step+(j+1)*channels]+diff) ||(temp1)< data[(i+1)*step+(j+1)*channels]-diff )
//temp1 compared with the next pixel "(i+1)" and "(j+1)" means next pixel
&&( temp2>data[(i+1)*step+(j+1)*channels+1]+diff || temp2< data[(i+1)*step+(j+1)*channels+1]-diff)
//same as above here.comparing with the second channel and third channel.
&& (temp3>data[(i+1)*step+(j+1)*channels+2]+diff || temp3<data [(i+1)*step+(j+1)*channels+2]-diff)){
datar[(i)*stepr+(j)*channelsr]=255;// "&&" above because There should be difference in
//individually in all the channels."||" because when we compare 2 values one might be
//more or less than the other but not both
}
else datar[(i)*stepr+(j)*channelsr]=0;}}
/*If loop explanation
First taking the first channel and checking
If you want to use cvErode you may...*/
/*Destroying all the Windows*/
cvShowImage("original",frame);
cvShowImage("Result",result);
cvSaveImage("resultedges.jpg",result);
cvWaitKey(0);
cvDestroyAllWindows();
//cvDestroyWindow("original");
//cvDestroyWindow("Result");
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
int corner ()
{
IplImage* image;
image = cvLoadImage(file_name, CV_LOAD_IMAGE_COLOR );
if (!image)
return -1;
int method=1;
/* I will show three methods of finding corners */
printf("Digite methods:\n0-Laplaciano\n1-Sobel\n2-Canny\n");
scanf("%d",&method);
IplImage* workfile;
IplImage* workfile2;
IplImage* dst;
CvSize size = cvGetSize(image);
/* Allocate a CvSize, its a structure that have width and lenght, cvGetSize get the
size of the original image, so our corner image will have the same width and height
*/
workfile = cvCreateImage(size,IPL_DEPTH_8U,1);
workfile2 = cvCreateImage(size,IPL_DEPTH_8U,1);
dst = cvCreateImage(size,IPL_DEPTH_32F,1);
switch (method)
{
case 0: cvCvtColor( image, workfile, CV_RGB2GRAY );
cvLaplace( workfile, dst, 3 ); /* dst is IPL_DEPTH_32F, because cvLaplace
only accepts 8u->16s, 8u->32f, 32f->32f */
break;
case 1: cvCvtColor( image, workfile, CV_RGB2GRAY );
cvSobel( workfile, workfile2, 2, 0, 3 );
cvSobel( workfile, workfile, 0, 2, 3 );
cvAdd(workfile, workfile2, workfile, NULL);
break;
case 2: cvCvtColor( image, workfile, CV_RGB2GRAY );
cvCanny( workfile, workfile, 50,100, 3 );
break;
}
cvNamedWindow( "Original", CV_WINDOW_AUTOSIZE);
cvNamedWindow( "Corners", CV_WINDOW_AUTOSIZE);
cvShowImage("Original", image);
if (method == 0)
cvShowImage("Corners", dst);
else
cvShowImage("Corners", workfile);
cvWaitKey(0);
cvDestroyAllWindows();
/* Destroy all Windows */
/* Destroy todas as janelas */
if (method == 0)
cvSaveImage( "output.jpg", dst );
else
cvSaveImage( "output.jpg", workfile );
/* cvSaveImage save the image with the name inside "" */
cvReleaseImage(&image);
cvReleaseImage(&workfile);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
int red()
{
int i,j,k,vermelho=15;
int height,width,step,channels;
int stepr, channelsr;
int temp=0;
uchar *data,*datar;
i=j=k=0;
IplImage *frame=cvLoadImage(file_name ,1);
IplImage *result=cvCreateImage( cvGetSize(frame), 8, 1 );
if(frame==NULL ) {
puts("unable to load the frame");exit(0);}
puts("frame loaded");
puts ("valor vermelho?");
scanf ("%d", &vermelho);
cvNamedWindow("original",CV_WINDOW_AUTOSIZE);
cvNamedWindow("Result",CV_WINDOW_AUTOSIZE);
height = frame->height;
width = frame->width;
step =frame->widthStep;
channels = frame->nChannels;
data = (uchar *)frame->imageData;
/*Here I use the Suffix r to diffenrentiate the result data and the frame data
Example :stepr denotes widthStep of the result IplImage and step is for frame IplImage*/
stepr=result->widthStep;
channelsr=result->nChannels;
datar = (uchar *)result->imageData;
for(i=0;i < (height);i++) for(j=0;j <(width);j++)
if(((data[i*step+j*channels+2]) > (vermelho+data[i*step+j*channels]))
&& ((data[i*step+j*channels+2]) > (vermelho+data[i*step+j*channels+1])))
datar[i*stepr+j*channelsr]=255;
else
datar[i*stepr+j*channelsr]=0;
/*If you want to use cvErode you may...*/
/*Destroying all the Windows*/
cvShowImage("original",frame);
cvShowImage("Result",result);
cvSaveImage("result.jpg",result);
cvWaitKey(0);
cvDestroyWindow("original");
cvDestroyWindow("Result");
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
void threshold()
{
int i=0;
printf ("\n 'q' go out");
img = cvLoadImage ( file_name , 0 );
krl = cvCreateImage( cvGetSize(img), img->depth, 1);
cvNamedWindow( "1" , CV_WINDOW_AUTOSIZE );
cvCreateTrackbar("threshold", "1", &_threshold, 255, NULL);
//Outros tipos de Threshold!!
//CV_THRESH_BINARY_INV
//CV_THRESH_TRUNC
//CV_THRESH_TOZERO_INV
//CV_THRESH_TOZERO
while(1) {
cvThreshold(img,krl,_threshold,255,CV_THRESH_BINARY);
cvShowImage ("1", krl);
char c = cvWaitKey(33);
if( c == 'q' ) break;
}
cvSaveImage("test2.jpg",krl);
cvReleaseImage(&img);
cvReleaseImage(&krl);
cvDestroyWindow( "1" );
}
///////////////////////////////////////////////////////////////////////////////////////
void Opening(int id) {
id;
cvErode(src,img,NULL,pos);
cvDilate(img,dst_image,NULL,pos);
cvShowImage("Opening&Closing window",dst_image);
}
void Closing(int id) {
id;
cvDilate(src,img,NULL,pos);
cvErode(img,dst_image,NULL,pos);
cvShowImage("Opening&Closing window",dst_image);
}
void Erosion(int id) {
id;
cvErode(src,dst_image,NULL,pos);
cvShowImage("Erosion&Dilation window",dst_image);
} //callback function for slider ,implements dilation
void Dilation(int id) {
id;
cvDilate(src,dst_image,NULL,pos);
cvShowImage("Erosion&Dilation window",dst_image);
}
void dila_ero() {
src = cvLoadImage("test2.jpg",0);
img = cvCloneImage(src);
dst_image = cvCloneImage(src);
//create windows for output images
cvNamedWindow("Opening&Closing window",1);
cvNamedWindow("Erosion&Dilation window",1);
cvShowImage("Opening&Closing window",src);
cvShowImage("Erosion&Dilation window",src);
cvCreateTrackbar("Open","Opening&Closing window",&pos,10,Opening);
cvCreateTrackbar("Close","Opening&Closing window",&pos,10,Closing);
cvCreateTrackbar("Dilate","Erosion&Dilation window",&pos,10,Dilation);
cvCreateTrackbar("Erode","Erosion&Dilation window",&pos,10,Erosion);
cvWaitKey(0);
//releases header an dimage data
cvReleaseImage(&src); cvReleaseImage(&img);
cvReleaseImage(&dst_image);
//destroys windows
cvDestroyWindow("Opening&Closing window");
cvDestroyWindow("Erosion&Dilation window");
}
///////////////////////////////////////////////////////////////////////////////////////
void OpenClose(int pos)
{
int n = open_close_pos - max_iters;
int an = n > 0 ? n : -n;
element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
if( n < 0 )
{
cvErode(img,dst_image,element,1);
cvDilate(dst_image,dst_image,element,1);
}
else
{
cvDilate(img,dst_image,element,1);
cvErode(dst_image,dst_image,element,1);
}
cvReleaseStructuringElement(&element);
cvShowImage("Open/Close",dst_image);
}
// callback function for erode/dilate trackbar
void ErodeDilate(int pos)
{
int n = erode_dilate_pos - max_iters;
int an = n > 0 ? n : -n;
element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
if( n < 0 )
{
cvErode(img,dst_image,element,1);
}
else
{
cvDilate(img,dst_image,element,1);
}
cvReleaseStructuringElement(&element);
cvShowImage("Erode/Dilate",dst_image);
}
void Morphology () {
img = cvLoadImage("test1.jpg",0);
dst_image = cvCloneImage(img);
//create windows for output images
cvNamedWindow("Open/Close",1);
cvNamedWindow("Erode/Dilate",1);
open_close_pos = erode_dilate_pos = max_iters;
cvCreateTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
cvCreateTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
OpenClose(open_close_pos);
ErodeDilate(erode_dilate_pos);
cvWaitKey(0);
/*
for(;;)
{
int c;
c = cvWaitKey(0);
if( c == 27 )
break;
if( c == 'e' )
element_shape = CV_SHAPE_ELLIPSE;
else if( c == 'r' )
element_shape = CV_SHAPE_RECT;
else if( c == 'c' )
element_shape = CV_SHAPE_CROSS;
else if( c == '\r' )
element_shape = (element_shape + 1) % 3;
}
*/
//release images
cvReleaseImage(&img);
cvReleaseImage(&dst_image);
//destroy windows
cvDestroyWindow("Open/Close");
cvDestroyWindow("Erode/Dilate");
}
///////////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv ) {
int op;
puts ("1 --- camera on\n");
puts ("2 --- frame\n");
puts ("3 --- Corner\n");
puts ("4 --- Test\n");
puts ("5 --- Laser\n");
puts ("6 --- Threshold\n");
puts ("7 --- Erode & Dilate\n");
puts ("8 --- Morphology\n");
puts ("q --- sortir)");
op = getch ();
if (op == '1')
captura (0,0);
if (op == '2')
ola ();
if (op == '3')
corner ();
if (op == '4')
test();
if (op == '5')
red ();
if (op == '6')
threshold();
if (op == '7')
dila_ero();
if (op == '8')
Morphology();
} |
Partager