summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/opencv_deconvlucy.cpp
blob: 9523f911a15e83a3a597767e3f8dea06c1f9f2f0 (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
/********************************************************
Author: Vinay Bhat
********************************************************
Usage: return_image = deconvlucy(input_image, PSF)
       return_image = deconvlucy(input_image, PSF, no_of_iterations)
********************************************************/

#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_deconvlucy(char *fname, unsigned long fname_len)
  {

    SciErr sciErr;
    int intErr = 0;
    int *piAddr = NULL;
    int i;
    double h;

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

    // Get the input image from the Scilab environment
    Mat image, psf;
    retrieveImage(image, 1);
    retrieveImage(psf, 2);

    // Check if number of arguments are three,
    // if so, then the number of iterations is also
    // specified.
    // If not, then set number of iterations as
    // ten.
    if (nbInputArgument(pvApiCtx) == 3)
    {
      // Get the address of the iterations scalar
      sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr);
      if (sciErr.iErr)
      {
          printError(&sciErr, 0);
          return 0;
      }

      // Get the no. of iterations scalar
      intErr = getScalarDouble(pvApiCtx, piAddr, &h);
      if(intErr)
      {
          return intErr;
      }   

      if (h < 0)
      {
          sciprint("Please enter a nonnegative scalar for number of iterations.\n");
          return 0;
      }
    }
    else 
      h = 10;

    // Check if the input image is grayscale or not,
    // if not, convert it to grayscale.
    if (image.type() != CV_8UC1)
    {
      Mat temp;
      temp = image.clone();
      cvtColor(temp, image, CV_BGR2GRAY);
    }

    // Check if the PSF is grayscale or not,
    // if not, convert it to grayscale.
    if (psf.type() != CV_8UC1)
    {
      Mat temp;
      temp = psf.clone();
      cvtColor(temp, psf, CV_BGR2GRAY);
    }

    // Apply the Lucy-Richardson method for deblurring
    // an image.
    Mat convRes, convRes2, temp;
    temp = image.clone();
    for (i = 0; i < h; i++)
    {
      filter2D(temp, convRes, -1, psf);
      max((temp - convRes), 0, convRes2);
      min((temp + convRes2), 255, convRes);
      temp = convRes.clone();
    }
    image = temp.clone();

    string tempstring = type2str(image.type());
    char *checker;
    checker = (char *)malloc(tempstring.size() + 1);
    memcpy(checker, tempstring.c_str(), tempstring.size() + 1);
    returnImage(checker, 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;

  }
/* ==================================================================== */
}