summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/opencv_bwlookup.cpp
blob: 5471e55125211abfbccf54ddaeb8fd86b2a2f4f4 (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
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
/*
 * bwlookup
 *
 * 2*2 and 3*3 non linear filtering of a binary image based on a lookup table -
 *lut which is taken as input from the user
 *
 */

// Created by Samiran Roy, mail: samiranroy@cse.iitb.ac.in
// An implementation of bwlookup
// Usage:
// bwlookup(I,lut)

// I is the input binary grayscale image. If the image is not binary, it is
// converted to one.
// lut is a 1*16 double vector [2*2 filtering], or a [1*512] double vector [3*3
// filtering]
// The indexing method used is the same as Matlab bwlookup:
// http://in.mathworks.com/help/images/ref/bwlookup.html

// Known Changes from Matlab:
/*
 * 1) None, as of now
 */

#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_bwlookup(char* fname, unsigned long fname_len) {
  SciErr sciErr;
  int intErr = 0;

  int* piAddr = NULL;

  // checking input argument
  CheckInputArgument(pvApiCtx, 2, 2);
  CheckOutputArgument(pvApiCtx, 1, 1);

  // get input matrix

  Mat image;
  retrieveImage(image, 1);

  if (image.channels() > 1) {
    sciprint("The image must be grayscale.");
    return -1;
  }

  double* lut;

  int iRows = 0, iCols = 0;
  sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr);
  if (sciErr.iErr) {
    printError(&sciErr, 0);
    return -1;
  }
  sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &lut);
  if (sciErr.iErr) {
    printError(&sciErr, 0);
    return -1;
  }

  // Error Checking

  if (!((iCols == 16) || (iCols == 512))) {
    sciprint("Expected LUT (argument 2) to have 16 or 512 elements.\n");

    return -1;
  }

  if (iRows != 1) {
    sciprint("Expected input number 2, LUT, to be a vector.\n");
    return -1;
  }

  if (image.channels() != 1) {
    sciprint("Expected input number 1, A, to be two-dimensional.\n");
    return -1;
  }

  // temporary copy of image to perform computation
  // converting the image to a binary image
  Mat tempimg = Mat::zeros(image.size(), CV_8U);

  for (int i = 0; i < image.rows; i++) {
    for (int j = 0; j < image.cols; j++) {
      if (image.at<double>(i, j) != 0) tempimg.at<uchar>(i, j) = 1;
    }
  }

  // pad the temporary copy of the image with zeroes to handle border cases
  copyMakeBorder(tempimg, tempimg, 1, 1, 1, 1, BORDER_CONSTANT, 0);

  // output images
  Mat new_image = Mat::zeros(image.size(), CV_32F);

  // temporary variables
  int ii, jj;
  int index;

  // 2*2 filtering
  if (iCols == 16) {
    for (int i = 0; i < image.rows; i++) {
      for (int j = 0; j < image.cols; j++) {
        ii = i + 1;
        jj = j + 1;

        index = tempimg.at<uchar>(ii, jj) * 1 +
                tempimg.at<uchar>(ii, jj + 1) * 2 +
                tempimg.at<uchar>(ii + 1, jj) * 4 +
                tempimg.at<uchar>(ii + 1, jj + 1) * 8;

        new_image.at<float>(i, j) = lut[ 0, index ];
      }
    }
  }

  // 3*3 filtering
  if (iCols == 512) {
    for (int i = 0; i < image.rows; i++) {
      for (int j = 0; j < image.cols; j++) {
        ii = i + 1;
        jj = j + 1;

        index = tempimg.at<uchar>(ii - 1, jj - 1) * 1 +
                tempimg.at<uchar>(ii - 1, jj) * 2 +
                tempimg.at<uchar>(ii - 1, jj + 1) * 4 +
                tempimg.at<uchar>(ii, jj - 1) * 8 +
                tempimg.at<uchar>(ii, jj) * 16 +
                tempimg.at<uchar>(ii, jj + 1) * 32 +
                tempimg.at<uchar>(ii + 1, jj - 1) * 64 +
                tempimg.at<uchar>(ii + 1, jj) * 128 +
                tempimg.at<uchar>(ii + 1, jj + 1) * 256;

        new_image.at<float>(i, j) = lut[ 0, index ];
      }
    }
  }

  //   sciprint("\n");

  //  for (int i = 0; i < new_image.rows; i++) {
  //    for (int j = 0; j < new_image.cols; j++) {
  //      sciprint("%i ", new_image.at<uchar>(i,j));

  //    }

  // sciprint("\n");
  //  }

  // new_image is sent to scilab as output

  int temp = nbInputArgument(pvApiCtx) + 1;
  string tempstring = type2str(new_image.type());
  char* checker;
  checker = (char*)malloc(tempstring.size() + 1);
  memcpy(checker, tempstring.c_str(), tempstring.size() + 1);
  returnImage(checker, new_image, 1);
  free(checker);

  // Assigning the list as the Output Variable
  AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
  // Returning the Output Variables as arguments to the Scilab environment
  ReturnArguments(pvApiCtx);
  return 0;
}
/* ==================================================================== */
}