src/vapiOperationsDefault.c

Ir para a documentação deste ficheiro.
00001 #include <cv.h>
00002 #include <cxcore.h>
00003 #include <highgui.h>
00004 #include <stdio.h>
00005 #include "vapiTypes.h"
00006 #include "vapiAux.h"
00007 #include "vapiString.h"
00008 #include "vapiImages.h"
00009 #include "vapiImagesAux.h"
00010 #include "vapiOptions.h"
00011 #include "vapi.h"
00012 #include "vapiCameraAcquisition.h"
00013 #include "vapiOperationsDefault.h"
00014 #include "vapiGtkMemory.h"
00015 #include "vapiResult.h"
00016 #include "vapiOperationOptions.h"
00017 #include "vapiCameraAcquisition.h"
00018 #include "vapiCameraSettings.h"
00019 #include "vapiIOCom.h"
00020 #ifndef VAPI_OS_LINUX
00021 #include <process.h>
00022 #include <stdlib.h>
00023 #else
00024 #include <unistd.h>
00025 #endif
00026 #ifndef VAPI_WITHOUT_INTERFACE
00027 #include "vapiGtkAuxWindow.h"
00028 #endif
00029 
00041 void
00042 vapiApplyOperation (vImages * Images, vEffect * Operation,
00043                     vOptions * Options, vMacroResult * PreviousResult)
00044 {
00045         int i;
00049         vapiCheckOptOriginal (Images, Options);
00050 
00051         v_debug ("ApplyOperation: %s", Operation->name);
00052 
00056         if (((vEffectSettings *) Operation->OptionsSettings->
00057              CustomOptionsSettings)->repeatInside)
00058         {
00059                 Operation->func (Images, Options, PreviousResult);
00060                 return;
00061         }
00062 
00063         for (i = 0;
00064              i < ((vEffectOptions *) Options->CustomOptions)->times; i++)
00065         {
00066                 Operation->func (Images, Options, PreviousResult);
00067         }
00068 
00069 
00070 }
00071 
00076 void
00077 vapiThreshold (vImages * Images, vOptions * options,
00078                vMacroResult * PreviousResult)
00079 {
00080         cvThreshold (Images->Actual, Images->Actual,
00081                      vapiOptionsGetInts (options, 0), 255, CV_THRESH_BINARY);
00082 }
00083 
00089 void
00090 vapiGrayscale (vImages * Images, vOptions * options,
00091                vMacroResult * PreviousResult)
00092 {
00093         IplImage *tempImage;
00094 
00095 
00096         /*
00097          * Caso o numero de canais seja diferente de 3,
00098          * ou já é grayscale ou não é RGB.
00099          */
00100         if (!vapiImagesCheckChannels (Images, 3))
00101                 return;
00102 
00103         /*
00104          * cria-se então uma imagem temporária com apenas 1 canal
00105          */
00106 
00107         tempImage = vapiImagesSameSize (Images->Actual, 1);
00108 
00109         /* Transformar a imagem em grayscale */
00110         cvCvtColor (Images->Actual, tempImage, CV_RGB2GRAY);
00111 
00112         /*
00113          * Recria-se a imagem Actual, em branco
00114          */
00115         vapiImagesSetActual (Images, vapiImagesSameSize (tempImage, 1));
00116 
00117         /*
00118          * Copia-se a Imagem temporária para a Actual
00119          */
00120         cvCopy (tempImage, Images->Actual, NULL);
00121 
00122         cvReleaseImage (&tempImage);
00123 }
00124 
00131 void
00132 vapiBinarizar (vImages * Images, vOptions * options,
00133                vMacroResult * PreviousResult)
00134 {
00135 
00136         /*
00137          * Primeiro, mete-se em escala de cinzentos
00138          */
00139         vapiGrayscale (Images, options, NULL);
00140         /*
00141          * Depois binariza-se
00142          */
00143 
00144         vapiThreshold (Images, options, NULL);
00145 
00146 }
00147 
00148 void
00149 vapiSmooth (vImages * Images, vOptions * options,
00150             vMacroResult * PreviousResult)
00151 {
00152         IplImage *tempImage;
00153         int smoothType;
00154 
00155         switch (vapiOptionsGetChoices (options, 0))
00156         {
00157         case 0:
00158         {
00159                 smoothType = CV_BLUR_NO_SCALE;
00160                 break;
00161         }
00162         case 1:
00163         {
00164                 smoothType = CV_BLUR;
00165                 break;
00166         }
00167         case 2:
00168         {
00169                 smoothType = CV_GAUSSIAN;
00170                 break;
00171         }
00172         case 3:
00173         {
00174                 smoothType = CV_MEDIAN;
00175                 break;
00176         }
00177         case 4:
00178         {
00179                 smoothType = CV_BILATERAL;
00180                 break;
00181         }
00182         default:
00183         {
00184                 smoothType = CV_MEDIAN;
00185                 break;
00186         }
00187 
00188         }
00189 
00190         tempImage = vapiImagesSameSize (Images->Actual, 0);
00191 
00192 #ifdef VAPI_OS_LINUX
00193         cvSmooth (Images->Actual, tempImage, smoothType,
00194                   vapiOptionsGetInts (options, 0),
00195                   vapiOptionsGetInts (options, 1), 0);
00196 #else
00197         cvSmooth (Images->Actual, tempImage, smoothType,
00198                   vapiOptionsGetInts (options, 0),
00199                   vapiOptionsGetInts (options, 1), 0, 0);
00200 #endif
00201 
00202         cvCopy (tempImage, Images->Actual, NULL);
00203 
00204         cvReleaseImage (&tempImage);
00205 }
00206 
00207 void
00208 vapiFlip (vImages * Images, vOptions * options, vMacroResult * PreviousResult)
00209 {
00210         IplImage *tempImage;
00211 
00212         tempImage = vapiImagesSameSize (Images->Actual, 0);
00213 
00214         cvFlip (Images->Actual, tempImage, vapiOptionsGetInts (options, 0));
00215 
00216         cvCopy (tempImage, Images->Actual, NULL);
00217 
00218         cvReleaseImage (&tempImage);
00219 
00220 
00221 }
00222 
00223 void
00224 vapiSobel (vImages * Images, vOptions * options,
00225            vMacroResult * PreviousResult)
00226 {
00227 
00228         IplImage *tempImage;
00229 
00230         /*
00231          * Apenas pode ser efectuado em imagens com apenas 1 canal
00232          */
00233 
00234         if (!vapiImagesCheckChannels (Images, 1))
00235         {
00236                 return;
00237         }
00238 
00239         tempImage = vapiImagesSameSize16 (Images->Actual, 0);
00240 
00241         cvSobel (Images->Actual, tempImage, 1, 1, 3);
00242 
00243         vapiImagesSetActual (Images, vapiImagesSameSize (tempImage, 0));
00244 
00245         cvConvertScaleAbs (tempImage, Images->Actual, 1, 0);
00246 
00247         cvReleaseImage (&tempImage);
00248 
00249 }
00250 
00251 void
00252 vapiLaplace (vImages * Images, vOptions * options,
00253              vMacroResult * PreviousResult)
00254 {
00255         IplImage *tempImage;
00256 
00257         if (!vapiImagesCheckChannels (Images, 1))
00258                 return;
00259 
00260         vapiCheckOptOriginal (Images, options);
00261 
00262         tempImage = vapiImagesSameSize16 (Images->Actual, 0);
00263 
00264         cvLaplace (Images->Actual, tempImage, 3);
00265 
00266         cvConvertScaleAbs (tempImage, Images->Actual, 1, 0);
00267 
00268         cvReleaseImage (&tempImage);
00269 
00270 
00271 }
00272 
00276 void
00277 vapiCanny (vImages * Images, vOptions * options,
00278            vMacroResult * PreviousResult)
00279 {
00280         if (!vapiImagesCheckChannels (Images, 1))
00281                 return;
00282 
00283         /*
00284          * FIXME: Énecessário poder definir na interface os parâmetros da função.
00285          */
00286         cvCanny (Images->Actual, Images->Actual, 50, 105, 3);
00287 
00288 }
00289 
00290 
00291 /*
00292  * Erosão
00293  */
00294 void
00295 vapiErode (vImages * Images, vOptions * options,
00296            vMacroResult * PreviousResult)
00297 {
00298         if (!vapiImagesCheckChannels (Images, 1))
00299         {
00300                 return;
00301         }
00302 
00303         cvErode (Images->Actual, Images->Actual, NULL,
00304                  vapiOptionsGetTimes (options));
00305 
00306 }
00307 
00308 /*
00309  * Dilatação
00310  */
00311 void
00312 vapiDilate (vImages * Images, vOptions * options,
00313             vMacroResult * PreviousResult)
00314 {
00315         if (!vapiImagesCheckChannels (Images, 1))
00316                 return;
00317 
00318         cvDilate (Images->Actual, Images->Actual, NULL,
00319                   vapiOptionsGetTimes (options));
00320 
00321 }
00322 
00323 void
00324 vapiColourFilter (vImages * Images, vOptions * options,
00325                   vMacroResult * PreviousResult)
00326 {
00327         IplImage *tempImage, *imagesHSV[3], *resultHSV[3], *finalMask;
00328         int width, height;
00329 
00330         double HUEMAX, HUEMIN, VALUEMIN, VALUEMAX, SATMIN, SATMAX;
00331 
00332         if (!vapiImagesCheckChannels (Images, 3))
00333         {
00334                 return;
00335         }
00336 
00337         /*
00338          * Colocar os valores de HSV de um modo mais utilizável.
00339          */
00340         HUEMIN = vapiOptionsGetFloats (options, 0);
00341         HUEMAX = vapiOptionsGetFloats (options, 1);
00342         SATMIN = vapiOptionsGetFloats (options, 2);
00343         SATMAX = vapiOptionsGetFloats (options, 3);
00344         VALUEMIN = vapiOptionsGetFloats (options, 4);
00345         VALUEMAX = vapiOptionsGetFloats (options, 5);
00346 
00347         v_debug ("HUEMIN %f, HUEMAX %f", HUEMIN, HUEMAX);
00348 
00349         /*
00350          * Converter da escala normalizada 0...1 para 0..180 e 
00351          *  deslocamento de escala.
00352          */
00353         /*
00354          * cvInRange faz < MAX, o que significa que quando max =1, 
00355          *  ele deita fora resultados;
00356          */
00357 
00358         HUEMAX = HUEMAX * 180 + 1;
00359 
00360         HUEMIN = HUEMIN * 180;
00361 
00362         SATMAX = SATMAX * 255 + 1;
00363         SATMIN = SATMIN * 255;
00364         VALUEMIN = VALUEMIN * 255;
00365         VALUEMAX = VALUEMAX * 255 + 1;
00366 
00367         v_debug ("HUEMIN %f, HUEMAX %f", HUEMIN, HUEMAX);
00368 
00369         /*
00370          * Criar Imagens Temporárias.
00371          * tempImage : conversão de RGB para HSV.
00372          * imagesHSV : array de imagens, uma para cada canal, H S V .
00373          */
00374         tempImage = vapiImagesSameSize (Images->Actual, 3);
00375         imagesHSV[0] = vapiImagesSameSize (Images->Actual, 1);
00376         imagesHSV[1] = vapiImagesSameSize (Images->Actual, 1);
00377         imagesHSV[2] = vapiImagesSameSize (Images->Actual, 1);
00378         resultHSV[0] = vapiImagesSameSize (Images->Actual, 1);
00379         resultHSV[1] = vapiImagesSameSize (Images->Actual, 1);
00380         resultHSV[2] = vapiImagesSameSize (Images->Actual, 1);
00381         finalMask = vapiImagesSameSize (Images->Actual, 1);
00382 
00383 
00384 
00385 
00386         /*
00387          * Converter a imagem para HSV.
00388          *
00389          */
00390         cvCvtColor (Images->Actual, tempImage, CV_RGB2HSV);
00391         cvSplit (tempImage, imagesHSV[0], imagesHSV[1], imagesHSV[2], NULL);
00392 
00393         /*
00394          * tempImage já não é necessária para o processamento, servirá 
00395          *  agora para guardar a mascara de filtragem.
00396          */
00397 
00398         cvReleaseImage (&tempImage);
00399         tempImage = vapiImagesSameSize (Images->Actual, 3);
00400 
00401         /*
00402          * Ciclo de filtro;
00403          */
00404         width = Images->Actual->width;
00405         height = Images->Actual->height;
00406 
00407         cvInRangeS (imagesHSV[0], cvScalarAll (HUEMIN), cvScalarAll (HUEMAX),
00408                     resultHSV[0]);
00409         cvInRangeS (imagesHSV[1], cvScalarAll (SATMIN), cvScalarAll (SATMAX),
00410                     resultHSV[1]);
00411         cvInRangeS (imagesHSV[2], cvScalarAll (VALUEMIN),
00412                     cvScalarAll (VALUEMAX), resultHSV[2]);
00413         cvAnd (resultHSV[0], resultHSV[1], finalMask, resultHSV[2]);
00414 
00415         /*
00416          * Copiar a imagem baseada na máscara.
00417          */
00418         cvReleaseImage (&imagesHSV[0]);
00419         cvReleaseImage (&imagesHSV[1]);
00420         cvReleaseImage (&imagesHSV[2]);
00421         cvReleaseImage (&resultHSV[0]);
00422         cvReleaseImage (&resultHSV[1]);
00423         cvReleaseImage (&resultHSV[2]);
00424         /*
00425          * Cópia da imagem actual.
00426          */
00427         cvCopy (Images->Actual, tempImage, finalMask);
00428         vapiImagesSetActual (Images, tempImage);
00429         cvReleaseImage (&finalMask);
00430 }
00431 
00432 void
00433 vapiGeometricCenter (vImages * Images, vOptions * options,
00434                      vMacroResult * PreviousResult)
00435 {
00436         int linha, coluna, somaC = 0, somaL = 0, n = 0;
00437         CvScalar pixelValue;
00438 
00439 
00440         if (!vapiImagesCheckChannels (Images, 1))
00441         {
00442                 return;
00443         }
00444         if (!(cvCountNonZero (Images->Actual) > 0))
00445         {
00446                 return;
00447         }
00448 
00449         for (coluna = 0; coluna < Images->Actual->width; coluna++)
00450         {
00451                 for (linha = 0; linha < Images->Actual->height; linha++)
00452                 {
00453                         pixelValue = cvGet2D (Images->Actual, linha, coluna);
00454 
00455                         if (pixelValue.val[0])
00456                         {
00457                                 somaC += coluna;
00458                                 somaL += linha;
00459                                 n++;
00460                         }
00461                 }
00462 
00463         }
00464         coluna = somaC / n;
00465         linha = somaL / n;
00466 
00467         vapiResultAddPoint (PreviousResult, coluna, linha);
00468 }
00469 
00470 /*
00471  * Função para detectar a existência de objectos.
00472  */
00473 void
00474 vapiObjectExists (vImages * Images, vOptions * options,
00475                   vMacroResult * PreviousResult)
00476 {
00477         if (!vapiImagesCheckChannels (Images, 1))
00478         {
00479                 return;
00480         }
00481 
00482         if (cvCountNonZero (Images->Actual) > 0)
00483         {
00484                 vapiResultSetDecision (PreviousResult, vTRUE);
00485         }
00486         else
00487         {
00488                 vapiResultSetDecision (PreviousResult, vFALSE);
00489         }
00490 }
00491 
00492 void
00493 vapiJumpToConditional (vImages * Images, vOptions * options,
00494                        vMacroResult * PreviousResult)
00495 {
00496         if (vapiResultGetDecision (PreviousResult))
00497         {
00498                 vapiResultSetJumpTo (PreviousResult,
00499                                      vapiOptionsGetInts (options, 0));
00500         }
00501         else
00502         {
00503                 vapiResultSetJumpTo (PreviousResult,
00504                                      vapiOptionsGetInts (options, 1));
00505         }
00506 }
00507 void
00508 vapiEndExecution (vImages * Images, vOptions * options,
00509                   vMacroResult * PreviousResult)
00510 {
00511         vapiResultSetEnd (PreviousResult);
00512 
00513 }
00514 
00515 void
00516 vapiCountObjects (vImages * Images, vOptions * options,
00517                   vMacroResult * PreviousResult)
00518 {
00519         int line, column, objectCount = 0;
00520         CvScalar pixelValue, NewValue;
00521         CvConnectedComp comp_tmp;
00522         int connectivity_tmp = 4, new = 255;
00523         int flags_tmp = connectivity_tmp + (new << 8);
00524         IplImage *tempImage;
00525         vImages *tempImages = vImagesInit ();
00526 
00527         NewValue = cvScalarAll (0);
00528         tempImage = cvCloneImage (Images->Actual);
00529         tempImages->Actual = cvCloneImage (Images->Actual);
00530 
00531         if (!vapiImagesCheckChannels (Images, 1))
00532         {
00533                 return;
00534         }
00535         for (line = 0; line < Images->Actual->height; line++)
00536         {
00537                 for (column = 0; column < Images->Actual->width; column++)
00538                 {
00539                         pixelValue = cvGet2D (tempImage, line, column);
00540                         if (pixelValue.val[0] == 255)
00541                         {
00542                                 objectCount++;
00543                                 cvCopy (tempImage, tempImages->Actual, NULL);
00544                                 cvFloodFill (tempImage,
00545                                              cvPoint (column, line), NewValue,
00546                                              cvScalarAll (20),
00547                                              cvScalarAll (20), &comp_tmp,
00548                                              flags_tmp, NULL);
00549 
00550                                 cvSub (tempImages->Actual, tempImage,
00551                                        tempImages->Actual, NULL);
00558                                 if (vapiOptionsGetBooleans (options, 0)
00559                                     && PreviousResult->pointsNumber <
00560                                     (tempImages->Actual->width *
00561                                      tempImages->Actual->height * 0.03))
00562                                 {
00563                                         vapiGeometricCenter (tempImages, NULL,
00564                                                              PreviousResult);
00565                                 }
00566                         }
00567 
00568                 }
00569         }
00570         vImagesFreeActual (tempImages);
00571         free (tempImages);
00572         cvReleaseImage (&tempImage);
00573         vapiResultAddInt (PreviousResult, objectCount);
00574 }
00575 
00576 void
00577 vapiMarkPoints (vImages * Images, vOptions * options,
00578                 vMacroResult * PreviousResult)
00579 {
00580         vPoint workPoint;
00581         int i, color[3] = { vapiOptionsGetInts (options, 0),
00582                 vapiOptionsGetInts (options, 1), vapiOptionsGetInts (options,
00583                                                                      2)
00584         };
00585         int PointsNumber;
00586         PointsNumber = vapiResultGetPointsNumber (PreviousResult);
00587         if (!(PointsNumber > 0))
00588         {
00589                 return;
00590         }
00591 
00592         for (i = 0; i < PointsNumber; i++)
00593         {
00594                 workPoint = vapiResultGetPoint (PreviousResult, i);
00595 
00596                 vapiImagesDrawCross (Images->Actual, workPoint.y,
00597                                      workPoint.x, vapiOptionsGetInts (options,
00598                                                                       3), 2,
00599                                      color);
00600 
00601         }
00602 }
00603 
00604 void
00605 vapiCompareInt (vImages * Images, vOptions * options,
00606                 vMacroResult * PreviousResult)
00607 {
00608         int result, testValue;
00609         if (PreviousResult->intsNumber == 0)
00610         {
00611                 return;
00612         }
00613         result = vapiResultGetInt (PreviousResult,
00614                                    vapiOptionsGetInts (options, 0));
00615 
00616         testValue = vapiOptionsGetInts (options, 1);
00617         switch (vapiOptionsGetChoices (options, 0))
00618         {
00619         case 0:
00620         {
00621                 if (result > testValue)
00622                 {
00623                         PreviousResult->Decision = vTRUE;
00624                 }
00625                 else
00626                 {
00627                         PreviousResult->Decision = vFALSE;
00628                 }
00629                 break;
00630         }
00631         case 1:
00632         {
00633                 if (result >= testValue)
00634                 {
00635                         PreviousResult->Decision = vTRUE;
00636                 }
00637                 else
00638                 {
00639                         PreviousResult->Decision = vFALSE;
00640                 }
00641                 break;
00642         }
00643         case 2:
00644         {
00645                 if (result == testValue)
00646                 {
00647                         PreviousResult->Decision = vTRUE;
00648                 }
00649                 else
00650                 {
00651                         PreviousResult->Decision = vFALSE;
00652                 }
00653                 break;
00654         }
00655         case 3:
00656         {
00657                 if (result <= testValue)
00658                 {
00659                         PreviousResult->Decision = vTRUE;
00660                 }
00661                 else
00662                 {
00663                         PreviousResult->Decision = vFALSE;
00664                 }
00665                 break;
00666         }
00667         case 4:
00668         {
00669                 if (result < testValue)
00670                 {
00671                         PreviousResult->Decision = vTRUE;
00672                 }
00673                 else
00674                 {
00675                         PreviousResult->Decision = vFALSE;
00676                 }
00677                 break;
00678         }
00679         case 5:
00680         {
00681                 if (result != testValue)
00682                 {
00683                         PreviousResult->Decision = vTRUE;
00684                 }
00685                 else
00686                 {
00687                         PreviousResult->Decision = vFALSE;
00688                 }
00689                 break;
00690         }
00691         }
00692 }
00693 void
00694 vapiCompareFloat (vImages * Images, vOptions * options,
00695                   vMacroResult * PreviousResult)
00696 {
00697         double result, testValue;
00698         if (PreviousResult->floatsNumber == 0)
00699         {
00700                 return;
00701         }
00702         result = vapiResultGetFloat (PreviousResult,
00703                                      vapiOptionsGetInts (options, 0));
00704         testValue = vapiOptionsGetFloats (options, 0);
00705         switch (vapiOptionsGetChoices (options, 0))
00706         {
00707         case 0:
00708         {
00709                 if (result > testValue)
00710                 {
00711                         PreviousResult->Decision = vTRUE;
00712                 }
00713                 else
00714                 {
00715                         PreviousResult->Decision = vFALSE;
00716                 }
00717                 break;
00718         }
00719         case 1:
00720         {
00721                 if (result >= testValue)
00722                 {
00723                         PreviousResult->Decision = vTRUE;
00724                 }
00725                 else
00726                 {
00727                         PreviousResult->Decision = vFALSE;
00728                 }
00729                 break;
00730         }
00731         case 2:
00732         {
00733                 if (result == testValue)
00734                 {
00735                         PreviousResult->Decision = vTRUE;
00736                 }
00737                 else
00738                 {
00739                         PreviousResult->Decision = vFALSE;
00740                 }
00741                 break;
00742         }
00743         case 3:
00744         {
00745                 if (result <= testValue)
00746                 {
00747                         PreviousResult->Decision = vTRUE;
00748                 }
00749                 else
00750                 {
00751                         PreviousResult->Decision = vFALSE;
00752                 }
00753                 break;
00754         }
00755         case 4:
00756         {
00757                 if (result < testValue)
00758                 {
00759                         PreviousResult->Decision = vTRUE;
00760                 }
00761                 else
00762                 {
00763                         PreviousResult->Decision = vFALSE;
00764                 }
00765                 break;
00766         }
00767         case 5:
00768         {
00769                 if (result != testValue)
00770                 {
00771                         PreviousResult->Decision = vTRUE;
00772                 }
00773                 else
00774                 {
00775                         PreviousResult->Decision = vFALSE;
00776                 }
00777                 break;
00778         }
00779         }
00780 }
00781 
00782 void
00783 vapiTemplateMatch (vImages * Images, vOptions * options,
00784                    vMacroResult * PreviousResult)
00785 {
00786         IplImage *result, *template, *OriginalTemplate;
00787         int resultWidth, resultHeight, width, height;
00788 
00789         OriginalTemplate = vapiOptionsGetTemplate (options);
00790 
00791         if ((OriginalTemplate->width > Images->Actual->width)
00792             || (OriginalTemplate->height > Images->Actual->height))
00793         {
00794                 v_info ("TemplateMatch: Medidas do template superior às da imagem");
00795                 return;
00796         }
00797         //vapiShowImage ("template", options->Template);
00798 
00799         /*
00800          * Definição da largura da Matriz de resultados
00801          */
00802         width = Images->Actual->width;
00803         height = Images->Actual->height;
00804         resultWidth = width - OriginalTemplate->width + 1;
00805         resultHeight = height - OriginalTemplate->height + 1;
00806 
00807         result = cvCreateImage (cvSize (resultWidth, resultHeight),
00808                                 IPL_DEPTH_32F, 1);
00809 
00810 
00811         /*
00812          * O template e a imagem t�m que ter o mesmo n�mero de canais.
00813          */
00814         if (!vapiImagesCheckChannels (Images, OriginalTemplate->nChannels))
00815         {
00816                 cvReleaseImage (&result);
00817                 return;
00818         }
00819 
00820         /* Finalmente, fazer o match. */
00821         cvMatchTemplate (Images->Actual,
00822                          OriginalTemplate, result, CV_TM_CCORR_NORMED);
00823 
00824 
00825         template = vapiImagesSameSize (result, 1);
00826 
00827         cvInRangeS (result, cvScalarAll (vapiOptionsGetFloats (options, 0)),
00828                     cvScalarAll (1.1), template);
00829 
00830         vapiResultAddInt (PreviousResult, cvCountNonZero (template));
00831 
00832         vapiImagesSetActual (Images,
00833                              cvCreateImage (cvSize (width, height),
00834                                             IPL_DEPTH_8U, 1));
00835         cvZero (Images->Actual);
00836 
00837         cvSetImageROI (Images->Actual,
00838                        cvRect (((OriginalTemplate->width / 2)),
00839                                ((OriginalTemplate->height / 2)),
00840                                result->width, result->height));
00841 
00842         cvCopy (template, Images->Actual, NULL);
00843         cvResetImageROI (Images->Actual);
00844 
00845         /*
00846          * Libertar memória de componentes temporários
00847          */
00848         cvReleaseImage (&result);
00849         cvReleaseImage (&template);
00850 }
00851 
00852 void
00853 vapiOCR (vImages * Images, vOptions * options, vMacroResult * PreviousResult)
00854 {
00855         FILE *resultFile;
00856         char textFromImage[200];
00857         /*
00858          * O tesseract apenas suporta ficheiros de 1 canal.
00859          */
00860         if (!vapiImagesCheckChannels (Images, 1))
00861         {
00862                 return;
00863         }
00864 
00865         cvSaveImage ("temp1.tif", Images->Actual);
00866         v_info ("existe o temp1");
00867 #ifndef VAPI_OS_LINUX
00868         system ("bin\\tesseract\\tesseract.exe temp1.tif vapiOCR batch");
00869 #else
00870         system ("tesseract temp1.tif vapiOCR batch");
00871 #endif
00872 
00873         resultFile = fopen ("vapiOCR.txt", "r");
00874 
00875         fscanf (resultFile, "%s", textFromImage);
00876         v_info ("lido: %s", textFromImage);
00877 
00878         vapiResultAddChar (PreviousResult, textFromImage);
00879 
00880 
00881         fclose (resultFile);
00882         /*
00883          * Eliminar os ficheiros criados.
00884          */
00885         vapiResultAddChar (PreviousResult, textFromImage);
00886         unlink ("temp1.tif");
00887         unlink ("vapiOCR.txt");
00888         unlink ("vapiOCR.map");
00889         unlink ("vapiOCR.raw");
00890 
00891 }
00892 
00893 void
00894 vapiSelectRegion (vImages * Images, vOptions * options,
00895                   vMacroResult * PreviousResult)
00896 {
00897         IplImage *mask, *copyResult;
00898         CvPoint vertices[4];
00899         int lineThickness = 1, i;
00900         int connectivity_tmp = 4, new = 255;
00901         int flags_tmp = connectivity_tmp + (new << 8);
00902         int intOption;
00903 
00904         for (i = 0; i < 8; i++)
00905         {
00906                 intOption = vapiOptionsGetInts (options, i);
00907                 if (intOption < 0 || intOption < 0)
00908                 {
00909                         return;
00910                 }
00911         }
00912 
00913         mask = vapiImagesSameSize (Images->Actual, 1);
00914         cvSetZero (mask);
00915 
00916         for (i = 0; i < 4; i++)
00917         {
00918                 vertices[i].x = vapiOptionsGetInts (options, i * 2);
00919                 vertices[i].y = vapiOptionsGetInts (options, i * 2 + 1);
00920                 if (i > 0)
00921                 {
00922                         cvLine (mask,
00923                                 vertices[i - 1],
00924                                 vertices[i],
00925                                 CV_RGB (255, 255, 255), lineThickness, 8, 0);
00926                 }
00927                 if (i == 3)
00928                 {
00929                         cvLine (mask,
00930                                 vertices[i],
00931                                 vertices[0],
00932                                 CV_RGB (255, 255, 255), lineThickness, 8, 0);
00933                 }
00934 
00935         }
00936 
00937         cvFloodFill (mask,
00938                      cvPoint (vertices[0].x + 2, vertices[0].y + 2),
00939                      cvScalarAll (255), cvScalarAll (20), cvScalarAll (20),
00940                      NULL, flags_tmp, NULL);
00941 
00942         if (vapiOptionsGetBooleans (options, 0))
00943         {
00944                 cvNot (mask, mask);
00945 
00946         }
00947         copyResult =
00948                 vapiImagesSameSize (Images->Actual,
00949                                     Images->Actual->nChannels);
00950         cvCopy (Images->Actual, copyResult, mask);
00951         vapiImagesSetActual (Images, copyResult);
00952         cvReleaseImage (&mask);
00953 }
00954 
00955 void
00956 vapiEraseIsolatedPixels (vImages * Images, vOptions * options,
00957                          vMacroResult * PreviousResult)
00958 {
00959         double Filter[] = {
00960                 -0.1, -0.1, -0.1, -0.1, 0.8, -0.1, -0.1, -0.1, -0.1
00961         };
00962         CvMat kernel = cvMat (3, 3, CV_32F, Filter);
00963         IplImage *mask, *filterResult;
00964 
00965         if (!vapiImagesCheckChannels (Images, 1))
00966         {
00967                 return;
00968         }
00969         mask = vapiImagesSameSize (Images->Actual, 1);
00970         filterResult = vapiImagesSameSize (Images->Actual, 1);
00971 
00972         cvFilter2D (Images->Actual, filterResult, &kernel, cvPoint (-1, -1));
00973 
00974         /*
00975          * Existe um limite maximo de 255, para o valor na imagem.
00976          *  por exemplo, 8*255 ultrapassaria esse valor, ficando = 255,
00977          * Decidiu-se então utilizar os pesos divididos por 10,
00978          *  em que o áximo é 204.
00979          */
00980         cvInRangeS (filterResult, cvScalarAll (204), cvScalarAll (205), mask);
00981 
00982         cvSub (Images->Actual, mask, Images->Actual, NULL);
00983 
00984         cvReleaseImage (&mask);
00985         cvReleaseImage (&filterResult);
00986 
00987 
00988 }
00989 
00990 void
00991 vapiNot (vImages * Images, vOptions * options, vMacroResult * PreviousResult)
00992 {
00993         IplImage *finalImage;
00994         if (!vapiImagesCheckChannels (Images, 1))
00995         {
00996                 return;
00997         }
00998 
00999         finalImage = vapiImagesSameSize (Images->Actual, 1);
01000 
01001         cvNot (Images->Actual, finalImage);
01002 
01003         vapiImagesSetActual (Images, finalImage);
01004 
01005 
01006 }
01007 
01008 void
01009 vapiResultResetPoints (vImages * Images, vOptions * options,
01010                        vMacroResult * PreviousResult)
01011 {
01012         vResultResetPoints (PreviousResult);
01013 }
01014 
01015 void
01016 vapiResultResetInts (vImages * Images, vOptions * options,
01017                      vMacroResult * PreviousResult)
01018 {
01019         vResultResetInts (PreviousResult);
01020 }
01021 
01022 void
01023 vapiTeste (vImages * Images, vOptions * options,
01024            vMacroResult * PreviousResult)
01025 {
01026         vBoolean *Input;
01027         if ((Input = v_input_NULL_boolean (1)) == NULL)
01028         {       
01029                 v_debug("erro");
01030                 return;
01031         }
01032         if (Input[0])
01033         {
01034                 free(Input);
01035                 v_debug ("É verdadeiro");
01036         }
01037         else
01038         {
01039                 free(Input);
01040                 v_debug ("É falso");
01041         }
01042 }

Gerado em Tue Jul 24 10:34:59 2007 para Vapi por  doxygen 1.5.1