00001
00002
00004
00005 #include "stdafx.h"
00006
00007
00008 #include "resource.h"
00009 #include "legoprojectDlg.h"
00010
00011
00012 #include "ImageProcessor.h"
00013
00014 #include "defines.h"
00015
00016
00017 #ifdef _DEBUG
00018 #undef THIS_FILE
00019 static char THIS_FILE[]=__FILE__;
00020 #define new DEBUG_NEW
00021 #endif
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #define WINAPI __stdcall
00055
00056 int myview =0;
00057
00058
00059
00061
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 int iImageCount=0;
00073 char szNameFormat[100];
00074 char szFileName[100];
00075
00076 ImageProcessor::ImageProcessor()
00077 {
00078
00079
00080 }
00081
00082 ImageProcessor::~ImageProcessor()
00083 {
00084
00085 }
00086
00087
00088
00089 Image *ImageProcessor::dib2cvip(CImage *dib)
00090 {
00091 int rows=(int)dib->m_pbi->biHeight;
00092 int cols=(int)dib->m_pbi->biWidth;
00093 byte *pixels=(BYTE *)dib->m_lpbits;
00094
00095 Image *cvipImage = NULL;
00096
00097 int r, c, b;
00098 byte **image;
00099 byte *p;
00100
00101 cvipImage = new_Image(PPM,RGB,3,rows,cols,CVIP_BYTE,REAL);
00102
00103 for(b=0; b< 3; b++)
00104 {
00105 image = (byte **)getData_Image(cvipImage,b);
00106 p=(byte *)pixels;
00107 for(r=rows-1;r>=0;r--)
00108 for(c=0;c<cols;c++)
00109 {
00110
00111 image[r][c]=(byte)p[2-b];
00112 p +=3;
00113 }
00114 }
00115
00116
00117 return cvipImage;
00118 }
00119
00120 void ImageProcessor::cvip2dib(CImage *dib, Image *cvipImage)
00121 {
00122 int rows=(int)dib->m_pbi->biHeight;
00123 int cols=(int)dib->m_pbi->biWidth;
00124 byte *pixels=(BYTE *)dib->m_lpbits;
00125
00126
00127
00128
00129
00130
00131 cvipImage=remap_Image(cvipImage,CVIP_BYTE, 0, 255);
00132
00133
00134 int r, c, b;
00135 byte **imagetwee;
00136 byte *p;
00137
00138
00139 for(b=0; b< getNoOfBands_Image(cvipImage); b++) {
00140 imagetwee = (byte **)getData_Image(cvipImage,b);
00141 p=(byte *)pixels;
00142 for(r=rows-1;r>=0;r--)for(c=0;c<cols;c++) {
00143
00144 p[2-b]= imagetwee[r][c];
00145 p += 3;
00146 }
00147 }
00148
00149 delete_Image(cvipImage);
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 void ImageProcessor::vis2cvip(Image *cvipImage, CImage *vis)
00163 {
00164
00165
00166
00167
00168 int rows=vis->visimage.Height();
00169 int cols=vis->visimage.Width();
00170 byte *pixels=(BYTE *)vis->visimage.RowPointer(0);
00171
00172
00173
00174 int r, c, b;
00175
00176 byte *p;
00177
00178 byte **image[3];
00179
00180 for(b=0; b< 3; b++)
00181 {
00182 image[b] = (byte **)getData_Image(cvipImage,b);
00183 }
00184
00185 p=(byte *)pixels;
00186
00187
00188
00189
00190
00191 for(r=0;r<rows;r++)
00192 {
00193
00194 for(c=0;c<cols;c++)
00195 {
00196
00197 for(b=0; b< 3; b++)
00198 {
00199
00200
00201 image[b][r][c]=(byte)p[2-b];
00202
00203
00204
00205 }
00206 p+=4;
00207 }
00208 }
00209
00210 }
00211
00212
00213 void ImageProcessor::cvip2vis(CImage *vis, Image *cvipImage)
00214 {
00215
00216
00217 int rows=getNoOfRows_Image(cvipImage);
00218 int cols=getNoOfCols_Image(cvipImage);
00219 int bands=getNoOfBands_Image(cvipImage);
00220
00221 if (! vis->visimage.IsValid()) {
00222 vis->visimage.Allocate(cols,rows);
00223 }
00224
00225
00226 byte *pixels=(BYTE *)vis->visimage.RowPointer(0);
00227
00228
00229
00230
00231
00232
00233 int r, c, b;
00234
00235 byte *p;
00236
00237
00238 byte **image[3];
00239
00240 for(b=0; b< bands; b++)
00241 {
00242 image[b] = (byte **)getData_Image(cvipImage,b);
00243 }
00244
00245 p=(byte *)pixels;
00246
00247
00248
00249
00250
00251 for(r=0;r<rows;r++)
00252 {
00253
00254 for(c=0;c<cols;c++)
00255 {
00256
00257 for(b=0; b< bands; b++)
00258 {
00259
00260
00261
00262 p[2-b]=(unsigned char) image[b][r][c];
00263
00264
00265
00266 }
00267 p+=4;
00268 }
00269 }
00270
00271
00272 }
00273
00274
00275
00276
00277
00278
00279
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 Image *ImageProcessor::MyAlgorithm(Image *cvipImage)
00293 {
00294
00295
00296
00297 #define LOGIMAGE
00298
00299
00300
00301 (void) setDisplay_Image(VIEWER, "Default");
00302
00303 iImageCount=0;
00304
00305 char *logpath= get_log_path();
00306 delete_old_logimages(logpath);
00307 delete logpath;
00308
00309
00310
00311
00312 List <Entity> objectlist;
00313 Image *tmp1,*tmp2,*blue_comp,*red_comp,*green_comp;
00314 tmp1 = duplicate_Image(cvipImage);
00315
00316 blue_comp=find_color_objects(cvipImage,LEGO_BLUE,&objectlist);
00317
00318 red_comp=find_color_objects(tmp1,LEGO_RED,&objectlist);
00319
00320
00321 blue_comp=add_Image(blue_comp,red_comp);
00322 log_Image(blue_comp,"_all_objects");
00323
00324
00325
00326
00327 print_entities(&objectlist);
00328 send_entities(&objectlist);
00329
00330
00331 return blue_comp;
00332 }
00333
00334
00335 void print_entities(List <Entity> *objectlist)
00336 {
00337 List <Entity>::iterator i;
00338 int color,type,x,y,angle;
00339 myprintf("--------");
00340 for ( i= objectlist->begin(); i != objectlist->end(); ++i )
00341 {
00342 color=(*i).getColor();
00343 x=(*i).getX();
00344 y=(*i).getY();
00345 type=(*i).getType();
00346 if ( type == Entity::ROBOT ) {
00347 angle=(*i).getAngle();
00348 myprintf("robot \r\n %d,%d,%d,%d",x,y,angle,color);
00349 }
00350 if ( type == Entity::SUBJECT ) {
00351 myprintf("subject \r\n %d,%d,%d",x,y,color);
00352 }
00353 if ( type == Entity::GOAL ) {
00354 myprintf("goal \r\n %d,%d,%d",x,y,color);
00355 }
00356 }
00357
00358 }
00359
00360 void send_entities(List <Entity> *objectlist)
00361 {
00362 List <Entity>::iterator i;
00363 short color,type,x,y,angle;
00364 myprintf("--------");
00365 for ( i= objectlist->begin(); i != objectlist->end(); ++i )
00366 {
00367 color=(*i).getColor();
00368 x=(*i).getX();
00369 y=(*i).getY();
00370 type=(*i).getType();
00371
00372 packetx *cmd_pack;
00373 cmd_pack=new packetx(sizeof(color)+sizeof(type)+sizeof(x)+sizeof(y)+sizeof(angle));
00374 cmd_pack->set_pack_mode();
00375 cmd_pack->process(color);
00376 cmd_pack->process(type);
00377 cmd_pack->process(x);
00378 cmd_pack->process(y);
00379
00380
00381 if ( type == Entity::ROBOT ) {
00382 angle=(*i).getAngle();
00383 cmd_pack->process(angle);
00384 cmd_pack->send(ADDR_RCX,PORT_PC);
00385 }
00386
00387 if ( (type == Entity::SUBJECT ) || (type == Entity::GOAL) ) {
00388 cmd_pack->send(ADDR_RCX,PORT_PC);
00389 }
00390
00391 delete(cmd_pack);
00392 }
00393
00394 }
00395
00396
00397
00398 Image *find_color_objects(Image *inputimage, int color ,
00399 List <Entity> *objectlist )
00400 {
00401 int row,col,angle,label,center_row,center_col,ret;
00402 long objectarea;
00403 int *ctr;
00404 Image *component_img;
00405 Image *labeled_img;
00406 Entity object;
00407
00408
00409 component_img=get_component(inputimage, color);
00410
00411
00412 if ( !get_first_non_null_point(component_img,&row,&col) ) {
00413 myprintf("first label : EMPTY IMG ");
00414 return false;
00415 }
00416 Image *temp = duplicate_Image(component_img);
00417 labeled_img = ::label(temp);
00418
00419
00420 object.setColor((Entity::Color)color);
00421
00422
00423
00424
00425 label=1;
00426 row=col=0;
00427
00428
00429 while ( find_labeled_object(labeled_img,row,col,label) ) {
00430
00431
00432
00433 objectarea=area(labeled_img,row,col);
00434
00435
00436
00437 if ( (SUBJECT_MINAREA < objectarea) && (objectarea <= GOAL_MAXAREA ) ) {
00438
00439 ctr=centroid(labeled_img,row,col);
00440 center_row=ctr[0];
00441 center_col=ctr[1];
00442
00443 object.setX(center_col);
00444
00445 object.setY(240-center_row);
00446 delete(ctr);
00447 }
00448
00449
00450
00451 if ( (ROBOT_MINAREA < objectarea) && (objectarea <= ROBOT_MAXAREA ) ) {
00452
00453 object.setType(Entity::ROBOT);
00454
00455 ret=robot_angle(component_img,center_row,center_col,angle);
00456 if (ret ) {
00457 object.setAngle(angle);
00458
00459 objectlist->push_back(object);
00460
00461 }
00462 }
00463 if ( (SUBJECT_MINAREA < objectarea) && (objectarea <= SUBJECT_MAXAREA ) ) {
00464
00465 object.setType(Entity::SUBJECT);
00466
00467 objectlist->push_back(object);
00468
00469 }
00470 if ( (GOAL_MINAREA < objectarea) && (objectarea <= GOAL_MAXAREA ) ) {
00471
00472 object.setType(Entity::GOAL);
00473
00474 objectlist->push_back(object);
00475
00476 }
00477
00478 label++;
00479 }
00480
00481
00482
00483
00484
00485
00486 delete_Image(labeled_img);
00487
00488 return component_img;
00489
00490 }
00491
00492
00493 bool ImageProcessor::Init(LPVOID param)
00494 {
00495 CLegoprojectDlg *mdlg;
00496 mdlg = (CLegoprojectDlg*)param;
00497 DWORD dwThreadID;
00498
00499 h_MyThread = CreateThread(NULL,0,mdlg->imageproc.MyThread,mdlg,NULL,&dwThreadID);
00500 if (!h_MyThread) {
00501 AfxMessageBox("Thread not started (?!?)", MB_ICONINFORMATION | MB_OK);
00502 return 0;
00503 }
00504
00505 return 1;
00506 }
00507
00508
00509
00510 bool ImageProcessor::Stop()
00511 {
00512 Sleep(700);
00513
00514 return 1;
00515 }
00516
00517
00518 DWORD __stdcall ImageProcessor::MyThread(LPVOID pParam)
00519
00520 {
00521 CLegoprojectDlg *mdlg;
00522 mdlg = (CLegoprojectDlg*) pParam;
00523
00524 ImageProcessor *MyImageProc;
00525 MyImageProc = (ImageProcessor*)&(mdlg->imageproc);
00526
00527
00528 CImage *vis = NULL;
00529 CImage *visout = NULL;
00530
00531 Image *cvipimage = NULL;
00532 Image *viewimage = NULL;
00533
00534
00535
00536 vis=&(mdlg->m_image2);
00537 visout=&(mdlg->m_image3);
00538
00539
00540 PostMessage(mdlg->dlghandle,WM_TAKEPICT,0,0);
00541
00542
00543 WaitForSingleObject(mdlg->picttaken,INFINITE);
00544
00545 int rows=vis->visimage.Height();
00546 int cols=vis->visimage.Width();
00547
00548 while (mdlg->imageprocessor_active) {
00549
00550
00551
00552
00553 PostMessage(mdlg->dlghandle,WM_TAKEPICT,0,0);
00554
00555
00556 WaitForSingleObject(mdlg->picttaken,INFINITE);
00557
00558
00559 cvipimage = new_Image(PPM,RGB,3,rows,cols,CVIP_BYTE,REAL);
00560
00561
00562
00563
00564 MyImageProc->vis2cvip( cvipimage,vis);
00565
00566
00567 cvipimage=MyImageProc->MyAlgorithm(cvipimage);
00568
00569
00570
00571 viewimage = remap_Image(cvipimage ,CVIP_BYTE,0,255);
00572 delete_Image(cvipimage);
00573
00574
00575 MyImageProc->cvip2vis(visout,viewimage);
00576 delete_Image(viewimage);
00577
00578
00579
00580
00581
00582 visout->Invalidate(TRUE);
00583 }
00584 DWORD ExitCode=0;
00585 ExitThread(ExitCode);
00586 return 0;
00587 }
00588
00589 void ImageProcessor::SingleRun(LPVOID pParam)
00590 {
00591 CLegoprojectDlg *mdlg;
00592 mdlg = (CLegoprojectDlg*) pParam;
00593
00594 ImageProcessor *MyImageProc;
00595 MyImageProc = (ImageProcessor*)&(mdlg->imageproc);
00596
00597
00598 CImage *vis = NULL;
00599 CImage *visout = NULL;
00600
00601 Image *cvipimage = NULL;
00602 Image *viewimage = NULL;
00603
00604
00605 vis=&(mdlg->m_image2);
00606
00607 visout=&(mdlg->m_image3);
00608
00609
00610
00612 vis->Snap();
00613
00614
00615
00616
00617
00618
00619
00620
00621 int rows=vis->visimage.Height();
00622 int cols=vis->visimage.Width();
00623
00624
00625 cvipimage = new_Image(PPM,RGB,3,rows,cols,CVIP_BYTE,REAL);
00626
00627
00628 MyImageProc->vis2cvip(cvipimage,vis);
00629
00630
00631 cvipimage=MyImageProc->MyAlgorithm(cvipimage);
00632
00633
00634 viewimage = remap_Image(cvipimage ,CVIP_BYTE,0,255);
00635 delete_Image(cvipimage);
00636
00637
00638 MyImageProc->cvip2vis(visout,viewimage);
00639 delete_Image(viewimage);
00640
00641
00642
00643
00644
00645 visout->Invalidate(TRUE);
00646 }
00647
00648
00649
00650 Image *get_component (Image *inputImage , int color)
00651 {
00652 Image *resultImage;
00653 Matrix *mat;
00654
00656 if (color == LEGO_RED)
00657 {
00658 Image *band2, *band3 ;
00659 band2 = duplicate_Image(inputImage);
00660 band3 = duplicate_Image(inputImage);
00661 band2 = extract_band(band2, 2);
00662 band3 = extract_band(band3, 3);
00663 inputImage = extract_band(inputImage, 1);
00664 inputImage = gray_multiply2(inputImage,2.0);
00665 inputImage = subtract_Image(inputImage, band2);
00666 inputImage = subtract_Image(inputImage, band3);
00667 }
00669
00670
00671 if (color == LEGO_GREEN)
00672 {
00673 Image *band1, *band3 ;
00674 band1 = duplicate_Image(inputImage);
00675 band3 = duplicate_Image(inputImage);
00676 band1 = extract_band(band1, 1);
00677 band3 = extract_band(band3, 3);
00678 inputImage = extract_band(inputImage, 2);
00679 inputImage = gray_multiply2(inputImage,2.5);
00680 inputImage = subtract_Image(inputImage, band1);
00681 inputImage = subtract_Image(inputImage, band3);
00682 }
00683
00685
00686 if (color == LEGO_BLUE)
00687 {
00688 Image *band1, *band2 ;
00689 band1 = duplicate_Image(inputImage);
00690 band2 = duplicate_Image(inputImage);
00691 band1 = extract_band(band1, 1);
00692 band2 = extract_band(band2, 2);
00693 inputImage = extract_band(inputImage, 3);
00694 inputImage = gray_multiply2(inputImage,2.0);
00695 inputImage = subtract_Image(inputImage , band1);
00696 inputImage = subtract_Image(inputImage , band2);
00697 }
00698
00699
00700 log_Image(inputImage,"_subtractedband");
00701
00702 Image *temp;
00703 temp = remap_Image(inputImage ,CVIP_SHORT,-255,255);
00704 delete_Image(inputImage);
00705
00706
00707
00708 mat=(Matrix *)get_default_filter(BLUR_SPATIAL,5,0);
00709 temp = (Image *)convolve_filter(temp,mat);
00710 delete_Matrix(mat);
00711 temp = gray_linear(temp ,130.0,255.0,255.0,0.0,0, -1);
00712
00713 resultImage = remap_Image(temp ,CVIP_BYTE,0,255);
00714 delete_Image(temp);
00715
00716 log_Image(resultImage,"_component");
00717
00718 return resultImage;
00719 }
00720
00721
00722
00723
00724
00725 bool get_centroid_off_first_object(Image *inputImage, int *center_row, int *center_col)
00726 {
00727 int row,col;
00728 Image *lab = NULL;
00729 lab = duplicate_Image(inputImage);
00730
00731
00732
00733
00734
00735 if ( !get_first_non_null_point(lab,&row,&col) ) {
00736 myprintf("first label : EMPTY IMG ");
00737 return false;
00738 }
00739
00740 lab = label(lab);
00741
00742 int *ctr=centroid(lab,row,col);
00743 delete_Image(lab);
00744 *center_row=ctr[0];
00745 *center_col=ctr[1];
00746 delete(ctr);
00747
00748 return true;
00749 }
00750
00751
00752
00753
00754
00755
00756
00757 bool get_first_non_null_point(Image *inputImage, int *row,int *column)
00758 {
00759 int r, c, rows ,cols;
00760 bool found=false;
00761 byte **image;
00762
00763
00764 rows = getNoOfRows_Image(inputImage);
00765 cols = getNoOfCols_Image(inputImage);
00766 image = (unsigned char ** )getData_Image(inputImage, 0);
00767 for(r=0; r < rows; r++)
00768 {
00769 for(c=0; c < cols; c++)
00770 {
00771 if (!found)
00772 {
00773 if ( image[r][c] != 0 )
00774 {
00775 *row=r;
00776 *column=c;
00777 found = true;
00778 }
00779 }
00780 }
00781 }
00782
00783 return found;
00784 }
00785
00786
00787 bool find_labeled_object(Image *inputImage, int &row,int &col, int &label) {
00788 int r, c, rows ,cols;
00789
00790
00791 int **image;
00792
00793
00794 rows = getNoOfRows_Image(inputImage);
00795 cols = getNoOfCols_Image(inputImage);
00796
00797
00798 image = (int ** )getData_Image(inputImage, 0);
00799
00800
00801 for(r=row; r < rows; r++)
00802 {
00803 for(c=0; c < cols; c++)
00804 {
00805
00806
00807
00808
00809
00810
00811
00812
00813 if ( image[r][c] == label )
00814 {
00815 label=image[r][c];
00816 row=r;
00817 col=c;
00818 return true;
00819 }
00820 }
00821 }
00822 return false;
00823 }
00824
00825
00826 bool robot_angle(Image *inputimage,int ¢er_row,int ¢er_col,int &angle)
00827 {
00829
00830
00831
00832
00833
00834 Image *Ccircle,*big_circle,*holes, *holes_and_edgecircle;
00835
00836
00837 Ccircle = create_circle(320,240,center_col,center_row,6);
00838
00839
00840
00841 Image *bigcircle_with_holes,*bigcircle_with_holes_copy1,
00842 *bigcircle_with_holes_copy2;
00843
00844 bigcircle_with_holes = duplicate_Image(inputimage);
00845 bigcircle_with_holes_copy1 = duplicate_Image(inputimage);
00846 bigcircle_with_holes_copy2 = duplicate_Image(inputimage);
00847
00848
00849 holes_and_edgecircle = xor_Image(bigcircle_with_holes_copy2,Ccircle);
00850 big_circle = or_Image(bigcircle_with_holes_copy1,holes_and_edgecircle);
00851 holes = xor_Image(bigcircle_with_holes,big_circle);
00852 log_Image(holes,"_holes");
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872 int center_row_hole,center_col_hole;
00873 bool found=get_centroid_off_first_object(holes,¢er_row_hole,¢er_col_hole);
00874 if ( !found )
00875 {
00876 myprintf("first label : EMPTY IMG ");
00877 delete_Image(holes);
00878 return( false ) ;
00879 }
00880 delete_Image(holes);
00881
00882
00883
00886 int delta_col,delta_row;
00887 delta_col = center_col_hole - center_col;
00888 delta_row = center_row_hole - center_row;
00889
00890
00891
00892
00893
00894 angle=atan2(-delta_row ,delta_col )*180/PI;
00895
00896 if (angle < -180) angle = angle +180;
00897 if (angle > 180) angle = angle -180;
00898
00899
00900
00901
00902
00903 return true;
00904 }
00905
00906
00907
00908
00909
00910
00911 void log_Image(Image *cvipimage, char *description)
00912 {
00913
00914 #ifdef LOGIMAGE
00915
00916
00917
00918
00919 iImageCount++;
00920
00921
00922
00923
00924 char *logpath= get_log_path();
00925 strcpy(szNameFormat,logpath);
00926 delete logpath;
00927
00928 if (getNoOfBands_Image(cvipimage) == 3) {
00929 strcat(szNameFormat,"lego%03d");
00930 strcat(szNameFormat, description);
00931 strcat(szNameFormat,".ppm");
00932
00933
00934 sprintf(szFileName,szNameFormat , iImageCount);
00935 } else {
00936 strcat(szNameFormat,"lego%03d");
00937 strcat(szNameFormat, description);
00938 strcat(szNameFormat,".pgm");
00939
00940
00941 sprintf(szFileName,szNameFormat , iImageCount);
00942 }
00943
00944 Image *resultimage = remap_Image(cvipimage ,CVIP_BYTE,0,255);
00945
00946 write_Image(resultimage,szFileName,CVIP_YES,CVIP_NO,PPM,CVIP_NO);
00947 #endif
00948
00949 }
00950
00951
00952 void view_logged_Image(CImage *visimg, int number)
00953 {
00954
00955 #ifdef LOGIMAGE
00956
00957
00958
00959
00960 char *logpath= get_log_path();
00961 strcpy(szNameFormat,logpath);
00962
00963
00964
00965
00966 strcat(szNameFormat,"lego%03d*.p?m");
00967 sprintf(szFileName,szNameFormat , number);
00968
00969
00970
00971 WIN32_FIND_DATA win32finddata;
00972 HANDLE hFindFirstFile;
00973
00974
00975 hFindFirstFile = FindFirstFile(szFileName, &win32finddata);
00976
00977
00978 if ( hFindFirstFile == INVALID_HANDLE_VALUE) {
00979 return;
00980 }
00981
00982
00983 strcpy(szNameFormat,logpath);
00984 strcat(szNameFormat,win32finddata.cFileName);
00985
00986
00987 delete logpath;
00988
00989 visimg->ReadFromFile(szNameFormat);
00990
00991 #endif
00992
00993 }
00994
00995
00996
00997
00998
00999
01003
01004
01005
01006
01007
01008 Image *Robot_Position_and_Angle(Image *inputImage)
01009 {
01010 int row,col,angle;
01011 Image *result;
01012 Image *inputImage2=duplicate_Image(inputImage);
01013 Image *inputImage3=duplicate_Image(inputImage);
01014 result=Object_Position_and_Angle(inputImage,LEGO_RED,&row,&col,&angle);
01015 delete_Image(result);
01016 myprintf("RED pos: r=%d, c=%d, ang: a=%d", row, col, angle);
01017 result=Object_Position_and_Angle(inputImage2,LEGO_GREEN,&row,&col,&angle);
01018 delete_Image(result);
01019 myprintf("GREEN pos: r=%d, c=%d, ang: a=%d", row, col, angle);
01020 result=Object_Position_and_Angle(inputImage3,LEGO_BLUE,&row,&col,&angle);
01021 myprintf("BLUE pos: r=%d, c=%d, ang: a=%d", row, col, angle);
01022 return result;
01023 }
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036 Image *Object_Position_and_Angle(Image *inputImage,int obj_color, int *obj_row,
01037 int *obj_col,int *obj_angle)
01038 {
01039 Image *lab = NULL, *Ccircle = NULL;
01040 Image *bigcircle_with_holes, *holes_and_edgecircle,*big_circle,*holes;
01041
01042
01043 bool found;
01044
01045 int center_row_1,center_col_1, center_row_2,center_col_2;
01046 int delta_row, delta_col;
01047
01048
01049
01050
01051
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063 bigcircle_with_holes = get_component(inputImage,obj_color);
01064
01065
01066 found=get_centroid_off_first_object(bigcircle_with_holes,¢er_row_1,¢er_col_1);
01067 if ( !found )
01068 {
01069 myprintf("first label : EMPTY IMG ");
01070 return( bigcircle_with_holes) ;
01071 }
01072
01073
01074
01075
01076
01078
01079
01080
01081
01082
01083
01084 Ccircle = create_circle(320,240,center_col_1,center_row_1,6);
01085
01086 Image *bigcircle_with_holes_copy1,*bigcircle_with_holes_copy2;
01087 bigcircle_with_holes_copy1 = duplicate_Image(bigcircle_with_holes);
01088 bigcircle_with_holes_copy2 = duplicate_Image(bigcircle_with_holes);
01089
01090
01091 holes_and_edgecircle = xor_Image(bigcircle_with_holes_copy2,Ccircle);
01092 big_circle = or_Image(bigcircle_with_holes_copy1,holes_and_edgecircle);
01093 holes = xor_Image(bigcircle_with_holes,big_circle);
01094 log_Image(holes,"_holes");
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115 found=get_centroid_off_first_object(holes,¢er_row_2,¢er_col_2);
01116 if ( !found )
01117 {
01118 myprintf("first label : EMPTY IMG ");
01119 return( holes) ;
01120 }
01121
01122
01123
01124
01127 delta_col = center_col_2 - center_col_1;
01128 delta_row = center_row_2 - center_row_1;
01129
01130 int angle;
01131
01132
01133
01134 angle=atan2(-delta_row ,delta_col )*180/PI;
01135
01136 if (angle < -180) angle = angle +180;
01137 if (angle > 180) angle = angle -180;
01138
01139
01141
01142
01143
01144 *obj_angle=angle;
01145 *obj_row=center_row_1;
01146 *obj_col=center_col_1;
01147 return holes;
01148 }