summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/opencv_adapthisteq.cpp
blob: 334f529ca7e8bdc2ca30dce5aa182a506e7b2ece (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*************************************************************************************
Author : Yash S. Bhalgat
***************************************************************************************
---------- Performs Contrast Limited Adaptive Histogram Equalisation -------------
Usage :
	1) output_img = adapthisteq(input_img);
		In this usage, the image itself is used as the guidance image.
	
	2) output_img = adapthisteq(input_img, clip_limit);
Example : 
	img = imread("lena.jpg");
	imshow(img);
	output_img = adapthisteq(img, img, 9);
	imshow(output_img);
***********************************************************************/

#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <algorithm>    // std::max

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

    SciErr sciErr;
    int intErr = 0;
    int iRows=0,iCols=0;
    int *piAddr2  = NULL;
    int i,j,k;
    double clip_limit;
	
	//Default clip limit
	clip_limit = 0.001;
	
    //checking input argument
    CheckInputArgument(pvApiCtx, 1, 2);
    CheckOutputArgument(pvApiCtx, 1, 1);
	
	int inputarg = *getNbInputArgument(pvApiCtx);
	
	
    Mat input_img;
    retrieveImage(input_img,1);
	
	if(inputarg == 2){
		//for value of clip_limit
		sciErr = getVarAddressFromPosition(pvApiCtx,2,&piAddr2);
		if (sciErr.iErr)
		{
			printError(&sciErr, 0);
			return 0;
		}
		intErr = getScalarDouble(pvApiCtx, piAddr2, &clip_limit);
		if(intErr)
			return intErr;
	}


    //Main function
    //cvtColor(input_img, input_img, CV_RGB2GRAY);
	
	float C[256];
    
    int row = input_img.rows;
    int col = input_img.cols;
    
    int window_x = 90;
    int window_y = 90;
    
    int min_x, min_y, max_x, max_y;
    Mat window_matrix;
    
    //float clip_limit = 0.001;

    int histSize = 256; //from 0 to 255
	float range[] = { 0, 256 } ;
	const float* histRange = { range };
	
	Mat H;
    Mat output_img = Mat::zeros(input_img.size(), input_img.type());
	
	if(input_img.channels()==1){
		for(int i=0; i<row; i++){
			for(int j=0; j<col; j++){
				min_x = max(0,i-window_x);
				min_y = max(0,j-window_y);
				max_x = min(row-1,i+window_x);
				max_y = min(col-1,j+window_y);
				window_matrix = input_img(Range(min_x, max_x), Range(min_y, max_y));
				//window_matrix = inputImage(min_x:max_x,min_y:max_y);
				
				if(input_img.at<uchar>(i,j)==0) output_img.at<uchar>(i,j) =0;
				else{
					calcHist(&window_matrix, 1, 0, Mat(), H, 1, &histSize, &histRange);
					
					int N = window_matrix.rows;
					int M = window_matrix.cols;
					H=H/(N*M);
	//				cout<<H<<endl;
					
					for(int z=0; z<256; z++){
						if(H.at<float>(0,z) > clip_limit) H.at<float>(0,z) = clip_limit;
					}
					
					float contrastArea = 1.0 - cv::sum(H).val[0];
					float height = contrastArea / 256.00;
					
					H = H + height;
					//cout<<cv::sum(H).val[0]<<endl;
					C[0] = H.at<float>(0,0)*255;
					for(int k=1; k<256; k++){
						C[k]= C[k-1] + H.at<float>(0,k)*255;
					}
					//cout<<C[255]<<endl;
					output_img.at<uchar>(i,j) = C[input_img.at<uchar>(i,j)];
				}
			}
		}
	}
    else if(input_img.channels()==3){
		vector<cv::Mat> Ichannels;
		split(input_img, Ichannels);
		
		for(int d=0; d<3; d++){
			for(int i=0; i<row; i++){
				for(int j=0; j<col; j++){
					min_x = max(0,i-window_x);
					min_y = max(0,j-window_y);
					max_x = min(row-1,i+window_x);
					max_y = min(col-1,j+window_y);
					
					window_matrix = Ichannels[d](Range(min_x, max_x), Range(min_y, max_y));
										
					if(Ichannels[d].at<uchar>(i,j)==0) output_img.at<Vec3b>(i,j)[d] =0;
					else{
						calcHist(&window_matrix, 1, 0, Mat(), H, 1, &histSize, &histRange);
						
						int N = window_matrix.rows;
						int M = window_matrix.cols;
						H=H/(N*M);
		//				cout<<H<<endl;
						
						for(int z=0; z<256; z++){
							if(H.at<float>(0,z) > clip_limit) H.at<float>(0,z) = clip_limit;
						}
						
						float contrastArea = 1.0 - cv::sum(H).val[0];
						float height = contrastArea / 256.00;
						
						H = H + height;
						//cout<<cv::sum(H).val[0]<<endl;
						C[0] = H.at<float>(0,0)*255;
						for(int k=1; k<256; k++){
							C[k]= C[k-1] + H.at<float>(0,k)*255;
						}
						//cout<<C[255]<<endl;
						output_img.at<Vec3b>(i,j)[d] = C[Ichannels[d].at<uchar>(i,j)];
					}
				}
			}
		}
		
		
	}
    
	

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

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