You are on page 1of 2

#include<iostream>

using namespace std;


#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace cv;
int computeOutput(int,int,int,int,int,int);
void main(){
Mat img = imread("pic.jpg");
cvtColor(img,img,0,0);
Mat img_new = img.clone();
int r1,s1,r2,s2,max = 255;
cout << "Input R1" << endl;
cin >> r1;
cout << "Input S1" << endl;
cin >> s1;
cout << "Input R2" << endl;
cin >> r2;
cout << "Input S2" << endl;
cin >> s2;
namedWindow("BEFORE");
imshow("BEFORE",img);
//------------------------------------------// r1 = r2, s1 = s2 -> no change
// Contrast Stretching
// Binary Image
for (int i = 0 ; i < img.rows ; i++)
{
uchar* data = img.ptr<uchar>(i);
uchar* data_new = img_new.ptr<uchar>(i);
for(int j = 0 ; j < img.cols ; j ++)
{
int val = computeOutput((int)data[j *
img.channels() + 0 ],r1,s1,r2,s2,max);
data_new[j * img_new.channels() + 0 ] = data_new[j
* img_new.channels() + 1 ]

=
data_new[j * img_new.channels() + 2 ]
= val;
}
}
//------------------------------------------namedWindow("AFTER");
imshow("AFTER",img_new);
waitKey(0);
}
int computeOutput(int x, int r1, int s1, int r2, int s2, int
max)
{
float result = 0;
if(0 <= x && x <= r1){
result = (s1/r1) * x;
}
else if(r1 < x && x <= r2){
result = ((s2 - s1) / (r2 - r1)) * (x - r1) + s1;
}
else if(r2 < x && x <= max){
result = ((max - s2) / (max - r2)) * (x - r2) + s2;
}
return (int)result;
}

You might also like