// ================= // || Worksheet 2 // || In this example, we are going to modify some of the pixels of an image, and then save the new image to disk. // ================= #include "program.h" using namespace std; using namespace cv; int worksheettwo( Mat& colorImage ) { char my_window_name[] = "My Image"; // now, we are going to convert our color image to a greyscale image, so that // we only have one channel. Mat greyImage; // OpenCV's convert color method. // usage: cvtColor(source, destination, conversion type); //cv::cvtColor( colorImage, greyImage, CV_BGR2GRAY ); // First, we are going to create somewhere to store our modified image. cv::Mat outputImage; // initialize outputImage to be exactly the same size and content as greyImage. outputImage.create(colorImage.size(), colorImage.type()); //now, we have loaded an image. let us manipulate some of the pixels. // what we are going to do is set every SECOND pixel // so, we are going to loop over all the rows for(int row = 0; row < colorImage.rows ; row++) { // and loop over each column for( int col = 0; col < colorImage.cols; col++) { // if j modulo 2 equals 0 (i.e. j is even) if( row % 2 == 0 ) { // if it is even, then set the color to black (=0) // to set the value at of the image // usage: mat.at(row, column, channel) = value; outputImage.at(row,col) = 0; } // otherwise (if j is odd) else { // the use the color value of the original image. // copy the data from the input image and store it in the output image. outputImage.at(row,col) = colorImage.at(row,col); } } // end of the 'column' for loop } // end of the 'row' for loop cv::namedWindow( my_window_name, WINDOW_AUTOSIZE ); // display our output image. cv::imshow( my_window_name, outputImage); // now we are going to save our image. // cv 'image write' // usage: imwrite( destination_file, image to save) //cv::imwrite( "images/Output_Image.jpg", outputImage ); cv::waitKey(); return 0; }