Skip to content

Instantly share code, notes, and snippets.

@sumit4iit
Last active Dec 25, 2015
Embed
What would you like to do?
Play With Intensities.
// Scan Images and Modify.
#include<iostream>
#include "cv.h"
#include "highgui.h"
#include <cstring>
using namespace cv;
using namespace std;
void saturate(Mat image, int c, int increase)
{
// No of channels in the current input image.
int channels = image.channels();
// if image is gray scale then number of channels is 1.
if(channels == 1)
{
// create iterator for image and initialize it with the first element.
// First element is returned by begin<uchar>() function.
// Since we know that image is gray scale and we have assumed that the input
// image is of 8 bit representation format we pass uchar or unsigned char as a
// return data type to begin() function.
// Similarly end() returns location of the last element of the container.
for(typeof(image.begin<uchar>()) it = image.begin<uchar>(); it!= image.end<uchar>() ; it++)
{
// *it is the value pointed by iterator it.
*it += increase;
}
}
// if we have color input
else if(channels == 3)
{
// Since input is a color image, every pixel represents a vector of size 3.
// Vec3b is data type which represents Vector of length 3 where each element is uchar
for(typeof(image.begin<Vec3b>())it = image.begin<Vec3b>(); it!= image.end<Vec3b>(); it++ )
{
// c represents channel number.
(*it)[c]+= increase;
}
}
}
int main()
{
// path of image.
string path = "res/download.jpg";
// read color image
Mat image = imread(path,CV_LOAD_IMAGE_GRAYSCALE);
if(!image.data)
{
cout<<"fail\n";
return -1;
}
// create clone of the image.
Mat In = image.clone();
// Function which adds increases intensity of every pixel in image by given value.
saturate(image,1,40);
namedWindow("original",CV_WINDOW_AUTOSIZE);
namedWindow("out",CV_WINDOW_AUTOSIZE);
imshow("out",image);
imshow("original",In);
waitKey(0);
imwrite("res/original.png",image);
imwrite("res/out.png",In);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment