Digital Image Processing - Exercise 2.2

Posted on April 13, 2019

The first two exercises, for Digital Image Processing class, consisted in manipulate the matrix of an image in two different ways: the first one should invert the gray scale in a specific region, provided by the user, and the second one should switch the quadrants which compose the image.

Regions.cpp

In the first program (regions.cpp, available in Portuguese here) it was necessary to receive from the user two points in the image, that is: four coordinates. Two for the first point and two for the second point, as it can be seen below.

After the input, the X coordinate from the first point will be stored in the xp1 variable and the Y coordinate from the first point will be stored in the yp1 variable, with the same being applied for the second point. In order to avoid some problem during the program's repetition structure to paint the inputted area, when receiving smaller coordinates values for the second point than the first point, I wrote a conditional code to change the variables values.

With that, the nested loop should begin from the smaller coordinates to the bigger coordinates, both in X and Y and subtract from 255 the gray scale value existing in every pixel of the matrix. The complete code follows:

Original image

Image after code processing

Trocaregioes.cpp

And for the second program, it was necessary to diagonally switch the image quadrants. The biggest difficulty here was realizing that existed two different ways to copy the matrix of an image, in OpenCV. One would make a "shallow" copy, when you assign another name (in this case, another variable name) to the same matrix, and the other is a "deep" copy, which creates a distinct matrix, to different variables, using the clone() command. After noticing this difference, the remaining of the code flowed quickly. I made a separate copy of the original image (deep copy) and wrote a pair of loops for each quadrant, in the original image. Here's the final code:

The original image, same as before

The image after the processing of the second code