Digital Image Processing Class: First Unit Exercises

This post is dedicated to the Digital Image Processing exercises with professor Agostinho, which is happening during the second semester of this year. Here, the exercises related to the first unit of the class are going to be listed, to be a part of the class final grade. The OpenCV is going to be the main library used in this course, with the C++ programming language. A simple tutorial can be found on my friend’s website.

All the codes can be accessed by clicking the titles of each exercise.
The root directory can be accessed here.

2.2.1: Negative of a region

The first exercise is to create the negative of an image inside a given rectangle. The following image illustrates the concept:

negative_img

For implementation simplicity, the images were converted to gray-scale. The trick is inverting the values of the pixels inside the selected region. If we consider the range of the gray-scale to be from 0 to 255, as the following code does:

for(int i=x0;i<y0;i++){
  for(int j=x1;j<y1;j++){
    image.at(i,j)= 255 - image.at(i,j);
  }
}

The user is asked to give the rectangle points at the beginning of execution.

Continue reading

My experience at Gwangju Institute of Science and Technology Global Internship program

gip_participants.jpg

My research opportunity starts with the announcement of an internship program at the city of Gwangju, located in South Korea. My advisor Diomadson saw the GIP flier and noticed me about it. Then, I thought that it could be a great opportunity to enhance my curriculum. With the chance, I started to provide the necessary documents and essays about myself, including academic achievements, personal experiences, and grades. The program details can be found here. The biggest challenge encountered in the registration process was obtaining an English certificate, since many proficiency certificates may take a long time to be got depending on the exam type, and I needed the result fast because I noticed the opportunity late. Luckily, the TOEIC exam is well accepted in a lot of Korean universities as I heard, and it is relatively fast and cheap to obtain its certificate. I recommend new participants to try to do a proficiency exam as soon as possible to avoid deadline issues, but if you are tight on time, TOEIC is a good option to meet this constraint.

I have found many reasons to apply for this program, among them, Korea is a well-developed country in the technology industry which is very interesting for my major. I thought that it could bring me new opportunities and develop my network. The other main reason is to visit my Korean girlfriend, which I had the happiness to meet during an exchange program in the USA.

In the application process, it is required to choose a laboratory where you will be developing your work for two months. From there, I tried to search the laboratory that best fitted with my academic experiences. The ICSL lab seemed to be the best choice, the fields of study are very related to my laboratory in Brazil. So I decided to email the lab professor to assure that it would be a good option. The following paragraph illustrates the email sent and its response.

Continue reading

A way to remove special characters from a text file

Hi everyone! I have been busy for the last months with various duties related to my university classes and research. But now I have separated a little time to write another post! Soon I will bring more contents related to my university environment.

Last month, the campus party happened in my city. So my friends and I decided to participate in two hackathons that happened simultaneously during the event just for fun, which later showed to be a great challenge. The first hackathon involved data science directed to the Brazilian health, and the second one was intended to accelerate the national overall judgment process. In the picture below I am presenting my groups work related to the data science hackathon, the TV shows a correlation matrix made from various databases data.

word

Jumping now to this post purpose!

While trying to develop a solution for one of the hackathons, we had to process the text of pdf files.  To do so, we used the PDFMiner package. The next line illustrates its usage:

pdf2txt.py -o 'output file' 'pdf file'

The application seemed to extract the pdf text pretty well, with just one little peculiarity: There were special/control characters (“Ctrl+l”) inserted whenever there was a new page at the original pdf. The picture below illustrates the character with the VIM text editor:

word2

This character got in our way to process the file for our final purpose. At first, we tried to just remove it with ordinary python functions and with different inputs representing the character, without success at the beginning. So we found a pretty interesting solution!


text_file = open('text.txt','r')
text_file = text_file.read()
text_file = repr(text_file)
text_file = text_file.replace("\\x0c","")
text_file = literal_eval(text_file)

The trick consists of using the “repr()” function which returns the completely “raw” string. From there, the special character can be easily identified an removed. In the end, just return the string to the original form with the “literal_eval()” function! The relation of “\x0c” and “Ctrl+l” was found by analyzing the raw text file with the “repr()” function.

I believe this approach can be used to similarly solve other issues involving different characters and programming languages.

Easily Using SWI-Prolog within Matlab

This post intends to show how to interact with SWI-Prolog codes inside MatLab.

The following code is an arbitrary example of a special system, which covers different situations for “control” based on various inputs.


:- dynamic upOn/0, upOff/0.

upOff.

getOn(sensor,Value) :- Value < 0.5.
getOff(sensor,Value) :- Value > 0.5. 

setOn(valve,Value) :- Value is 1.0.
setOff(valve,Value) :- Value is 0.0.

control(V_in,V_out,S0,S1,S2,SW) :- getOff(sensor,SW),getOff(sensor,S0),getOff(sensor,S1),getOff(sensor,S2),upOff,setOff(valve,V_in),setOff(valve,V_out).
control(V_in,V_out,S0,S1,S2,SW) :- getOn(sensor,SW),getOff(sensor,S0),getOff(sensor,S1),getOff(sensor,S2),upOff,setOn(valve,V_in),setOff(valve,V_out),retract(upOff),asserta(upOn).
control(V_in,V_out,S0,S1,S2,SW) :- getOn(sensor,SW),getOn(sensor,S0),getOff(sensor,S1),getOff(sensor,S2),upOn,setOn(valve,V_in),setOff(valve,V_out).
control(V_in,V_out,S0,S1,S2,SW) :- getOn(sensor,SW),getOn(sensor,S0),getOn(sensor,S1),getOff(sensor,S2),upOn,setOn(valve,V_in),setOff(valve,V_out).
control(V_in,V_out,S0,S1,S2,SW) :- getOn(sensor,SW),getOn(sensor,S0),getOn(sensor,S1),getOn(sensor,S2),upOn,setOff(valve,V_in),setOn(valve,V_out),retract(upOn),asserta(upOff).
control(V_in,V_out,S0,S1,S2,SW) :- getOn(sensor,SW),getOn(sensor,S0),getOn(sensor,S1),getOn(sensor,S2),upOff,setOff(valve,V_in),setOn(valve,V_out).
control(V_in,V_out,S0,S1,S2,SW) :- getOn(sensor,SW),getOn(sensor,S0),getOn(sensor,S1),getOff(sensor,S2),upOff,setOff(valve,V_in),setOn(valve,V_out).
control(V_in,V_out,S0,S1,S2,SW) :- getOn(sensor,SW),getOn(sensor,S0),getOff(sensor,S1),getOff(sensor,S2),upOff,setOn(valve,V_in),setOff(valve,V_out),retract(upOff),asserta(upOn).
control(V_in,V_out,S0,S1,S2,SW) :- getOff(sensor,SW),getOn(sensor,S0),getOn(sensor,S1),getOn(sensor,S2),setOff(valve,V_in),setOn(valve,V_out).
control(V_in,V_out,S0,S1,S2,SW) :- getOff(sensor,SW),getOn(sensor,S0),getOn(sensor,S1),getOff(sensor,S2),upOff,setOff(valve,V_in),setOn(valve,V_out).
control(V_in,V_out,S0,S1,S2,SW) :- getOff(sensor,SW),getOn(sensor,S0),getOn(sensor,S1),getOff(sensor,S2),upOn,setOff(valve,V_in),setOn(valve,V_out),retract(upOn),asserta(upOff).
control(V_in,V_out,S0,S1,S2,SW) :- getOff(sensor,SW),getOn(sensor,S0),getOff(sensor,S1),getOff(sensor,S2),upOff,setOff(valve,V_in),setOn(valve,V_out).
control(V_in,V_out,S0,S1,S2,SW) :- getOff(sensor,SW),getOn(sensor,S0),getOff(sensor,S1),getOff(sensor,S2),upOn,setOff(valve,V_in),setOn(valve,V_out),retract(upOn),asserta(upOff).

The above code can be called by another Prolog script as follows:

#!/usr/bin/env swipl

:- initialization main. 

main:-  current_prolog_flag(argv,Argv),
        nth0(0, Argv, A), % get first argument
		nth0(1, Argv, B), % get second argument
		nth0(2, Argv, C), % get third argument
		nth0(3, Argv, D), % get fourth argument
		format("~w ~w ~w ~w \n",[A,B,C,D]), % Print inputs
        consult('example1Prolog'), % Load main Prolog code
		atom_number(A,E), % Transform inputs into Prolog integers
		atom_number(B,F),
		atom_number(C,G),
		atom_number(D,H),
		control(X,Y,E,F,G,H), % Query control function with inputs
		format("X= ~w Y= ~w \n",[X,Y]),	% Prints output variables
		halt. % Finishes execution

The script is executed with the bash shell command:


swipl -s plscript.pl 1 1 1 1

Where the last 4 numbers stand for the Prolog code inputs. The following output with the above command is:


1 1 1 1
X= 0.0 Y= 1.0

Most of the work is already done! (WOW). The last step is executing the same bash command within Matlab using the “system” function:


[status,term_out] = system('swipl -s plscript.pl 1 1 1 1')

“term_out” variable will hold the script output for the inputs given. The following picture shows the previous call output on the main console with the described method.

matlab_run_prolog

Note: It is important to check if a set of inputs will return valid outputs for the system (if they are covered). Also, make sure to test if the script is reaching the last line “halt.”, otherwise the SWI-Prolog will open without closing and the Matlab will wait until the application finishes (never) and crash.

The following Prolog command is helpful if the developed Prolog code depends on dynamic variables like the one shown in this tutorial:


qsave_program(runtime_data).

The problem is that the saved data can not be loaded into a running environment, making this post approach not to work for dynamic variables (for now).

Files can be accessed at:

https://github.com/YangTavares/Deep_learning/tree/master/Prolog_to_Matlab