Exercise 1 - Negative of a region
This is the first exercise using OpenCV image manipulation. In this example, we'll be reading and processing a region of a given image in order to create a negative effect on it. To achive this goal, the following fomula is used to invert the color of the pixels: pixel_color = 255 - pixel_color
.
As the image used is in grayscale, we will be aplying it for one channel only. The region is seleted by a couple of for-loops that go through an area within the image, inverting its pixels, as shown below.
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image;
int pxl;
image= imread("img/camera__.jpg",CV_LOAD_IMAGE_GRAYSCALE);
if(!image.data)
cout << "couldn't load image" << endl;
namedWindow("window",WINDOW_AUTOSIZE);
for(int i=100;i<200;i++){
for(int j=100;j<200;j++){
pxl = image.at<uchar>(i,j);
image.at<uchar>(i,j) = 255 - pxl;
}
}
imwrite("negative.jpg", image);
imshow("window", image);
waitKey();
return 0;
}