Wednesday 20 May 2015

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;



}

No comments:

Post a Comment