0% found this document useful (1 vote)
2K views2 pages

OPENCV - Topic 03 - Contrast Stretching

This C++ code loads an image, applies contrast stretching to modify pixel intensities, and displays the original and processed images. It takes pixel intensity values between user-defined thresholds and maps them to new ranges, leaving values outside the thresholds unchanged. The code reads pixel data, calls a function to compute new intensity values, writes the results back to a second image, and displays both images before and after processing.

Uploaded by

huahongquan2007
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
2K views2 pages

OPENCV - Topic 03 - Contrast Stretching

This C++ code loads an image, applies contrast stretching to modify pixel intensities, and displays the original and processed images. It takes pixel intensity values between user-defined thresholds and maps them to new ranges, leaving values outside the thresholds unchanged. The code reads pixel data, calls a function to compute new intensity values, writes the results back to a second image, and displays both images before and after processing.

Uploaded by

huahongquan2007
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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