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
|
/* ==================================================================== */
/* Author :Priyanka Hiranandani NIT Surat */
/* ==================================================================== */
/* Syntax : return_matrix_of_threshold_levels=multithresh(InputArray src,int level) */
/* ==================================================================== */
/*The function return various threshold levels:*/
/* ==================================================================== */
#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
#include<stdio.h>
#include <sciprint.h>
using namespace cv;
using namespace std;
extern "C"
{
#include "api_scilab.h"
#include "Scierror.h"
#include "BOOL.h"
#include <localization.h>
#include "../common.h"
//global variables
float **p,**s,**h;
float **as,**ds;
float fast(int n,int i,int j,int index[])
{
int k,index1;
float s;
float max = INT_MIN;
if(i>256)
{
return INT_MIN;
}
if(n==0)
return h[i][j];
if(ds[i][n]!=-1.000000)
return ds[i][n];
for(k=i;k<=256;k++)
{
s=h[i][k]+fast(n-1,k+1,j,index);
if(s>max)
{
max=s;
as[i][n]=k;
index[n]=k;
}
}
return ds[i][n]=max;
}
int multithresh(char *fname, unsigned long fname_len)
{
// Error management variable
SciErr sciErr;
//variable info
int iRows = 0;
int iCols = 0;
int piRows = 0;
int piCols = 0;
int* piAddr2=NULL ;
double thresh;
float C;
int i,j;
int error;
//checking input argument
CheckInputArgument(pvApiCtx,2,2);
//checking output argument
CheckOutputArgument(pvApiCtx,1,1);
//for first argument
Mat img;
retrieveImage(img,1);
//second argument
sciErr = getVarAddressFromPosition(pvApiCtx,2,&piAddr2);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//this function will fetch value of fifth argument
error=getScalarDouble(pvApiCtx,piAddr2,&thresh) ;
if(error!=0)
{
sciprint("error in retrieving second argument");
}
//code for histogram calculation
Mat imageh;
cvtColor(img, imageh, CV_BGR2GRAY);
// number of bins
int histSize[1] = {256};
// min andax pixel value
float hranges[2] = {0, 256};
const float* ranges[1] = {hranges};
int channels[1] = {0}; // only 1 channel used
cv::MatND hist;
calcHist(&imageh, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
//code for ostu method for histogram calculation
float *hi;
hi=(float *)malloc(sizeof(float)*257);
float nPixels = imageh.total();
for(i=1;i<257;i++)
{
hi[i]=(float)(hist.at<float>(i-1));
}
p=(float **)malloc(sizeof(float *)*257);
for(i=0;i<257;i++)
{
p[i]=(float *)malloc(sizeof(float)*257);
}
s=(float **)malloc(sizeof(float *)*257);
for(i=0;i<257;i++)
{
s[i]=(float *)malloc(sizeof(float)*257);
}
h=(float **)malloc(sizeof(float *)*257);
for(i=0;i<257;i++)
{
h[i]=(float *)malloc(sizeof(float)*257);
}
for(i=0;i<257;i++)
{
for(j=0;j<257;j++)
{
p[i][j]=0;
s[i][j]=0;
h[i][j]=0;
}
}
p[1][0]=0;
s[1][0]=0;
for(i=0;i<256;i++)
{
p[1][i+1]=p[1][i]+hi[i+1];
s[1][i+1]=s[1][i]+((i+1)*(hi[i+1]));
}
for(i=2;i<257;i++)
{
for(j=1;j<257;j++)
{
p[i][j]=p[1][j]-p[1][i-1];
s[i][j]=s[1][j]-s[1][i-1];
}
}
float temp,temp1;
for(j=1;j<257;j++)
{
for(i=1;i<=j;i++)
{
temp=(s[i][j])*(s[i][j]);
temp1=(temp/p[i][j]);
h[i][j]=temp1;
}
}
as=(float **)malloc(sizeof(float *)*257);
for(i=0;i<257;i++)
{
as[i]=(float *)malloc(sizeof(float)*257);
}
ds=(float **)malloc(sizeof(float *)*257);
for(i=0;i<257;i++)
{
ds[i]=(float *)malloc(sizeof(float)*257);
}
for(i=0;i<257;i++)
{
for(j=0;j<257;j++)
{
ds[i][j]=-1.000000;
}
}
int n=(int )thresh;
int index[n+1];
fast(n,1,256,index);
int a=0;
double *output=NULL;int k=0;
output=( double *)malloc(sizeof(double)*n);
for(i=n;i>=1;i--)
{
a=as[a+1][i];
output[k]=a;
k++;
}
for(i=0;i<257;i++)
{
free(h[i]);
free(as[i]);
free(ds[i]);
}
free(h);
free(as);
free(ds);
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1,1,n,output);
free(output);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
AssignOutputVariable(pvApiCtx,1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
return 0;
}
}
|