summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/opencv_imfindcircles.cpp~
blob: 7201e7d6517baf27fa8457df8c6cb88ff9308381 (plain)
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
/********************************************************
Function   :imfindcircles
Syntax     :B=imfindcircles(A)
Author: Tess  Zacharias

********************************************************/

#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
extern "C"
{
  #include "api_scilab.h"
  #include "Scierror.h"
  #include "BOOL.h"
  #include <localization.h>
  #include "sciprint.h"
  #include "../common.h"

  int opencv_imfindcircles(char *fname, unsigned long fname_len)
  {

    SciErr sciErr;
    int intErr = 0;
    int *piAddrNew  = NULL;
    int *piAddr2  = NULL;
    int *piAddr3  = NULL;
    Mat src_gray,image;
    double Rmin,Rmax;
    //checking input argument
    CheckInputArgument(pvApiCtx, 1, 1);
    CheckOutputArgument(pvApiCtx, 1, 2) ;
    sciprint("here1");
    retrieveImage(image, 1);
//    sciErr = getVarAddressFromPosition(pvApiCtx,2,&piAddr2);
//    if (sciErr.iErr)
//    {
//        printError(&sciErr, 0);
//        return 0;
//    }
//    intErr = getScalarDouble(pvApiCtx, piAddr2, &Rmin);
//    if(intErr)
//    {
//        return intErr;
//    }   
//    if(Rmin!= round(Rmin) || Rmin<=0)
//    {
//        sciprint("The value of minium Radius must be an integer\n");
//        return 0;
//    }  
//    if(Rmin<5)
//    {
//        sciprint("The value of minium Radius too small\n");
//        return 0;
//    }  
//    sciErr = getVarAddressFromPosition(pvApiCtx,3,&piAddr3);
//    if (sciErr.iErr)
//    {
//        printError(&sciErr, 0);
//        return 0;
//    }
//    intErr = getScalarDouble(pvApiCtx, piAddr3, &Rmax);
//    if(intErr)
//    {
//        return intErr;
//    } 
//    if(Rmax!= round(Rmax) || Rmax<=0)
//    {
//        sciprint("The value of maximum Radius must be an integer\n");
//        return 0;
//    }  
    sciprint("here2");
    cvtColor(image,src_gray,CV_BGR2GRAY);
    GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
    vector<Vec3f> circles;
    HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100,0,0);
    sciprint("here3");
    int k=circles.size();
    if (k==0)
    {sciprint("zero");}
    else if (k==1)
    {sciprint("one");}
    else if (k==2)
    {sciprint("two");}
    else if (k==3)
    {sciprint("three");}
    else 
    {sciprint(">3");}
    double *radius=NULL;
    radius=( double *)malloc(sizeof(double)*k);
    double *c1=NULL;
    c1=( double *)malloc(sizeof(double)*k);
    double *c2=NULL;
    c2=( double *)malloc(sizeof(double)*k);
    sciprint("here4");
    for( size_t i= 0; i < circles.size(); i++ )
    {
      c1[i]=cvRound(circles[i][0]);
      cout<<c1[i];
      sciprint("here5");
      c2[i]=cvRound(circles[i][1]);
      cout<<c2[i];
      sciprint("here6");
      radius[i]=cvRound(circles[i][2]);
      cout<<radius[i];
      sciprint("here7");
    }
    sciErr = createList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 2, &piAddrNew);
    if(sciErr.iErr)
     {
        printError(&sciErr, 0);
        return 0;
     }
    sciErr = createMatrixOfDoubleInList(pvApiCtx, nbInputArgument(pvApiCtx)+1, piAddrNew, 1, 1, k, c1);
    free(c1); 
    if(sciErr.iErr)
     {
        printError(&sciErr, 0);
        return 0;
     } 
    sciErr = createMatrixOfDoubleInList(pvApiCtx, nbInputArgument(pvApiCtx)+1, piAddrNew, 2, 1, k, c2);
    free(c2); 
    if(sciErr.iErr)
     {
        printError(&sciErr, 0);
        return 0;
     } 
    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; 
    if(Lhs==2)
     {   
        sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) +2,1,k,radius);
        free(radius);
        if (sciErr.iErr)
        {
          printError(&sciErr, 0);
          return 0;
        }  
       AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
     }
    ReturnArguments(pvApiCtx);
    return 0;         

  }
}