The following code implements the grey world algorithm in OpenCV. Grey world hypothesis assumes that the statistical mean of a scene is achromatic. Based on this assumption obtains an estimate for the scene illuminant to perform colour correction (white balancing)
#include <iostream> //preprocessor directive
#include<vector>
#include "opencv2/opencv.hpp" //include OpenCV libraries
//Main function (starting point of execution)
int main(int argc, char argv[])
{
// Read the image from file (replace the image file name in your code)
cv::Mat imgIn = cv::imread("snow.jpg");
//Check if the image is empty
if ( imgIn.empty() )
{
//Print error message
std::cout << "Error occured when loading the image" << std::endl;
//Return negative 1 to indicate an error has occured in the program
return -1;
}
//Sum the colour values in each channel
cv::Scalar sumImg=sum(imgIn);
//normalise by the number of pixes in the image to obtain an extimate for the illuminant
cv::Scalar illum=sumImg/(imgIn.rows*imgIn.cols);
// Split the image into different channels
std::vector<cv::Mat> rgbChannels(3);
cv::split(imgIn, rgbChannels);
//Assign the three colour channels to CV::Mat variables for processing
cv::Mat redImg=rgbChannels[2];
cv::Mat graanImg=rgbChannels[1];
cv::Mat blueImg=rgbChannels[0];
//calculate scale factor for normalisation you can use 255 instead
double scale=(illum(0)+illum(1)+illum(2))/3;
//correct for illuminant (white balancing)
redImg=redImg*scale/illum(2);
graanImg=graanImg*scale/illum(1);
blueImg=blueImg*scale/illum(0);
//Assign the processed channels back into vector to use in the cv::merge() function
rgbChannels[0]=blueImg;
rgbChannels[1]=graanImg;
rgbChannels[2]=redImg;
cv::Mat imgOut; //to hold the output image
/// Merge the processed colour channels
merge(rgbChannels, imgOut);
//Create a window
cv::namedWindow("My Window", 1);
//Display the image
imshow("My Window", imgOut);
// Wait until a key is pressed
cv::waitKey(0);
//Return 0 to indicate normal run of the program
return 0;
}
OpenCV
Thursday, 21 May 2015
Wednesday, 20 May 2015
Create a negative image in OpenCV
The following code shows how to create a negative in OpenCV.
#include <iostream> //preprocessor directive
#include "opencv2/opencv.hpp" //include OpenCV libraries
//Main function (starting point of execution)
int main(int argc, char argv[])
{
// Read the image from file (replace the image file name in your code)
cv::Mat imgIn = cv::imread("snow.jpg");
//Check if the image is empty
if ( imgIn.empty() )
{
//Print error message
std::cout << "Error occured when loading the image" << std::endl;
//Return negative 1 to indicate an error has occured in the program
return -1;
}
//Scan all the rows of the image
for (int row=0;row<imgIn.rows;row++)
{
//Scan all the columns of the image
for (int col=0;col<imgIn.cols;col++)
{
//Invert each colour channel
imgIn.at<cv::Vec3b>(row,col)[0]=255-imgIn.at<cv::Vec3b>(row,col)[0];
imgIn.at<cv::Vec3b>(row,col)[1]=255-imgIn.at<cv::Vec3b>(row,col)[1];
imgIn.at<cv::Vec3b>(row,col)[2]=255-imgIn.at<cv::Vec3b>(row,col)[2];
}
}
//Create a window
cv::namedWindow("My Window", 1);
//Display the image
imshow("My Window", imgIn);
// Wait until a key is pressed
cv::waitKey(0);
//Return 0 to indicate normal run of the program
return 0;
}
#include <iostream> //preprocessor directive
#include "opencv2/opencv.hpp" //include OpenCV libraries
//Main function (starting point of execution)
int main(int argc, char argv[])
{
// Read the image from file (replace the image file name in your code)
cv::Mat imgIn = cv::imread("snow.jpg");
//Check if the image is empty
if ( imgIn.empty() )
{
//Print error message
std::cout << "Error occured when loading the image" << std::endl;
//Return negative 1 to indicate an error has occured in the program
return -1;
}
//Scan all the rows of the image
for (int row=0;row<imgIn.rows;row++)
{
//Scan all the columns of the image
for (int col=0;col<imgIn.cols;col++)
{
//Invert each colour channel
imgIn.at<cv::Vec3b>(row,col)[0]=255-imgIn.at<cv::Vec3b>(row,col)[0];
imgIn.at<cv::Vec3b>(row,col)[1]=255-imgIn.at<cv::Vec3b>(row,col)[1];
imgIn.at<cv::Vec3b>(row,col)[2]=255-imgIn.at<cv::Vec3b>(row,col)[2];
}
}
//Create a window
cv::namedWindow("My Window", 1);
//Display the image
imshow("My Window", imgIn);
// Wait until a key is pressed
cv::waitKey(0);
//Return 0 to indicate normal run of the program
return 0;
}
Processing pixels in OpenCV (Colour filter effect)
The following code shown how to open an image, process each pixel and display on the screen. The resultant image is an image filtered with red colour.
#include <iostream> //preprocessor directive
#include "opencv2/opencv.hpp" //include OpenCV libraries
//Main function (starting point of execution)
int main(int argc, char argv[])
{
// Read the image from file (replace the image file name in your code)
cv::Mat imgIn = cv::imread("snow.jpg");
//Check if the image is empty
if ( imgIn.empty() )
{
//Print error message
std::cout << "Error occured when loading the image" << std::endl;
//Return negative 1 to indicate an error has occured in the program
return -1;
}
//Scan all the rows of the image
for (int row=0;row<imgIn.rows;row++)
{
//Scan all the columns of the image
for (int col=0;col<imgIn.cols;col++)
{
//assign 255 to the red colour channel
imgIn.at<cv::Vec3b>(row,col)[2]=255;
}
}
//Create a window
cv::namedWindow("My Window", 1);
//Display the image
imshow("My Window", imgIn);
// Wait until a key is pressed
cv::waitKey(0);
//Return 0 to indicate normal run of the program
return 0;
}
#include <iostream> //preprocessor directive
#include "opencv2/opencv.hpp" //include OpenCV libraries
//Main function (starting point of execution)
int main(int argc, char argv[])
{
// Read the image from file (replace the image file name in your code)
cv::Mat imgIn = cv::imread("snow.jpg");
//Check if the image is empty
if ( imgIn.empty() )
{
//Print error message
std::cout << "Error occured when loading the image" << std::endl;
//Return negative 1 to indicate an error has occured in the program
return -1;
}
//Scan all the rows of the image
for (int row=0;row<imgIn.rows;row++)
{
//Scan all the columns of the image
for (int col=0;col<imgIn.cols;col++)
{
//assign 255 to the red colour channel
imgIn.at<cv::Vec3b>(row,col)[2]=255;
}
}
//Create a window
cv::namedWindow("My Window", 1);
//Display the image
imshow("My Window", imgIn);
// Wait until a key is pressed
cv::waitKey(0);
//Return 0 to indicate normal run of the program
return 0;
}
Read an image and display it using OpenCV
The following code shows how to read an image and display it using OpenCV.
#include <iostream> //preprocessor directive
#include "opencv2/opencv.hpp" //include OpenCV libraries
//Main function (starting point of execution) int main(int argc, char argv[])
{
// Read the image from file (replace the image file name in your code)
cv::Mat imgIn = cv::imread("your_image_filename.jpg");
//Check if the image is empty
if ( imgIn.empty() )
{
//Print error message
std::cout << "Error occured when loading the image" << std::endl;
//Return negative 1 to indicate an error has occured in the program
return -1;
}
//Create a window
cv::namedWindow("My Window", 1);
//Display the image
imshow("My Window", imgIn);
// Wait until a key is pressed
cv::waitKey(0);
//Return 0 to indicate normal run of the program
return 0;
}
#include <iostream> //preprocessor directive
#include "opencv2/opencv.hpp" //include OpenCV libraries
//Main function (starting point of execution) int main(int argc, char argv[])
{
// Read the image from file (replace the image file name in your code)
cv::Mat imgIn = cv::imread("your_image_filename.jpg");
//Check if the image is empty
if ( imgIn.empty() )
{
//Print error message
std::cout << "Error occured when loading the image" << std::endl;
//Return negative 1 to indicate an error has occured in the program
return -1;
}
//Create a window
cv::namedWindow("My Window", 1);
//Display the image
imshow("My Window", imgIn);
// Wait until a key is pressed
cv::waitKey(0);
//Return 0 to indicate normal run of the program
return 0;
}
Subscribe to:
Posts (Atom)