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;
}
No comments:
Post a Comment