Thursday, October 29, 2009

Experiences with OpenCV

OpenCV has really lots of useful classes and features.
The square detection for my virtual reality class work is being made with it.

Unfortunately, on my tests, I was detecting squares which I didn't want the software to detect. Like on the image below:It would be much better if I could do the image analysis only inside the laptop screen. "Googling" on "OpenCV image crop" I had reached the nashruddin.com (OpenCV Region of Interest - ROI). This blog has a good explanation on how to crop (extract the region of interest) from a image.
With those informations I got the following result for the same frame:

Now I have to check the area of the detected squares. At the web archives of the computer geometry class mailing list, from the Illinois University, google had shown me one algorithm to calculate the area of a closed polygon, as follows:
Let 'vertices' be an array of N pairs (x,y), indexed from 0
Let 'area' = 0.0
for i = 0 to N-1, do
Let j = (i+1) mod N
Let area = area + vertices[i].x * vertices[j].y
Let area = area - vertices[i].y * vertices[j].x
end for

In C, to use with OpenCV:
double polygonArea(CvPoint vertices[]){
double area = 0.0;
int i, j;
for (i=0;i<4;i++){
j = (i+1) % 4;
area = area + vertices[i].x * vertices[j].y;
area = area - vertices[i].y * vertices[j].x;
}
if (area<0) area = -area;
return area;
}

Tuesday, October 27, 2009

Becoming father...

After 4 years of marriage we will finally have our first kid.

My wife is writing a blog with more details (in Portuguese). But which is letting me more exited about this near future is the new excuse to buy toys. The chart below from graphjam illustrates well:

song chart memes
see more Funny Graphs

And here is one echo scan image made when we were with 12 weeks of pregnancy:

Saturday, October 24, 2009

Developing PlayStation2 Games

The I finally have a PlayStation2 at home. Now it is time to start looking for tools for game developing for it.

The first place google has shown was http://ps2dev.org/. The first look has been promising. Besides the PlayStation2 related pages, it also has tutorials and articles related to the PlayStation3 and the PSP.

I hope to sooner be posting something about my experiences developing for the PlayStation2.

The results should probably be tested with some PlayStation2 emulator (http://pcsx2.net/, http://www.emulator-zone.com/doc.php/ps2/)

Wednesday, October 21, 2009

How many times each user has logged in

With this line, we can check how many times our users had logged in with SSH:

cat /etc/passwd | cut -d ":" -f 1 | sort | uniq | while read line ; do echo -n $line" "; zgrep "user $line" /var/log/auth.log* | grep -i ssh | grep -vi fail | grep -vi invalid | wc -l; done | sort -n -r -k 2

On my system, the only user which has logged in was my own user:
filipi 52

And for non SSH logins:
cat /etc/passwd | cut -d ":" -f 1 | sort | uniq | while read line ; do echo -n $line" "; zgrep "user $line" /var/log/auth.log* | grep -vi fail | grep -vi invalid | wc -l; done | sort -n -r -k 2

Tuesday, October 20, 2009

Checking auth.log for ssh brute force attacks

As I am letting my personal computer always on, as a homelinux server, I decided to check if someone is trying to breaking in with SSH brute force attacks.

First I did a grep for fail at the /var/log/auth.log. (grep -i /var/log/auth.log)

And I got lots of lines with the string "fail". With [grep -i /var/log/auth.log | wc -l] I figured out that were 1164 fail entries at auth.log

With an [grep -i fail auth.log | cut -d " " -f 6 | sort | uniq] I checked that were two kind of failed attempts:
Failed
pam_unix(sshd:auth):

So I wrote the following line to check with which users they were attempting to log:
grep Failed auth.log | cut -d " " -f 11 | sort | uniq | while read line ; do echo -n $line" "; grep $line auth.log | wc -l; done | sort -n -k 2

Here, the field position (the number 11 at the above command lines [-f 11]) may change in some systems. At my desktop at work, the username came at the position 9.

Here are the "top ten":
root 2922
user 2884
test 30
oracle 26
admin 22
mythtv 12
user1 6
teste 4
silentios 4
setup 4

Iauch! 2922 attempts! Luckily I always change my SSH config to not permit root logons.

At the /etc/ssh/sshd_config, PermitRootLogin no
And a good habit is to add a last AllowUsers line, followed by the usernames enabled to log trough SSH.

As most unixes do log rotation with gzip, the line above can be changed to zgrep all auth logs as follows:
zgrep Failed auth.log* | cut -d " " -f 11 | sort | uniq | while read line ; do echo -n $line" "; zgrep Failed auth.log* | grep $line | wc -l; done | sort -n -k 2

This particular line took me around 4 minutes running. After that, it came with a list with 676 diferent users which attemped to log with ssh in my host.

Here are the top 50:
w 9707
u 9707
t 9707
sshd 9707
ssh 9707
s 9707
r 9707
p 9707
o 9707
m 9707
log 9707
l 9707
k 9707
i 9707
h 9707
g 9707
f 9707
ed 9707
e 9707
desktop 9707
d 9707
am 9707
a 9707
v 9706
user 9706
n 9706
id 9706
z 8419
c 6818
root 2322
y 840
b 677
j 381
test 319
adm 268
admin 253
at 222
x 189
it 167
q 134
ftp 124
mail 113
web 102
postgres 79
mysql 78
mini 74
suporte 71
guest 67
pop 65
oracle 62

Saturday, October 17, 2009

Control and Industrial applications

http://epics.web.psi.ch/software/ioc-octave/index.php

GNU Octave from PHP without temp files

Of course... How haven't I thought that? Pipes!

I was "googling" about PHP and Octave and I had found some simple approach at a mailing list archive. I had already used Octave with PHP but I had always used temporary files, but pipes are a much simpler approach.

From the mailing list post:
in php
start "octave -q script" with popen

create a string in php e.g. "input_data = [102 10 10 10 10 10];"

write this string to the pipe

in octave

recive the string with fread on stdin
let the eval function do the job
calculate

print the result with printf in php syntax "$result = array(12,10,9,12,)"

php
read the output from the pipe (with php fread)
use phps eval function


but using eval with octave and php is a security risk, to avid eval parse
the matrix wird scanf

this is also not the fastes way to use octave (the octave startup needs a
lot of time)

Friday, October 9, 2009

Plotting with AJAX + Javascript


A friend of mine had written an application which communicates with a small microcotroled board and saves data read from this board on a mysql database. He wants to plot these data on the web. He wrote a PHP script using the GD lib. He did a real good job, but he wants it to plot the data dynamically and an http refresh made things a little ugly. He is now trying to plot directly with AJAX + Javascript. He is trying to use Flot, which is a javascrit for plotting data.
Another usefull resource for realtime web graphics is the CavasDemos.

Monday, October 5, 2009

Regex within PostgreSQL

Regex (Regular expressions) are very useful. I had to make a SQL query to return only the firstname from a table where the field provides the full name.
ideia_onde=# SELECT name FROM users;
name
--------------------------------
Linus Torvalds
Randal Schwartz
Steve Wozniak
(3 rows)

ideia_onde=#
Now, with a simple substring using a regular expression:
SELECT SUBSTRING(name, '(.*?(\\s))') FROM users;
substring
-----------
Linus
Randal
Steve
(3 rows)

ideia_onde=#
What do I did?
The substring function extracts from the name field everything (.*) until (?) a space character (\\s).

Saturday, October 3, 2009

Experiences with Tecgraf Libraries

My advisor is always right. So I have decided to take a look at Im lib, from Tecgraf.

As a learner by example, I first tried to compile and run the examples provided with Im lib, but most of their examples came written in lua and I need to write my code in C (or C++). Fortunately there were 6 examples written in C++:
glut_capture.c im_copy.cpp im_info.cpp im_view.c iupglview.c proc_fourier.cpp
But, I wans't use glut (discarted glut_capture.c and iupglview.c)
So I've choosen im_view.c to try to compile, and the torture has started. There wans't any doc, readme or anything which tells something on how to compile the examples. The only clue was the comments written on the source header:
/*
IM 3 sample that shows an image.
Needs "im.lib", "iup.lib", "cd.lib" and "cdiup.lib". Usage: im_view Example: im_view test.tif Click on image to open another file.
*/
Well, I had discovered that I was needing three more libs from Tecgraf to compile this example. So let's get them. With all these four libs I did put them all at /usr/local/tec/include and /usr/local/tec/lib, changed my /etc/ld.so.conf to add /usr/local/tec/lib and had runned the ldconfig command. But now, which GCC parameters I should use?

Issuing a gcc -I/usr/local/tec/include/ -L/usr/local/tec/lib im_view.c
was always resulting on error. Taking a look at imlib dependencies with ldd
# ldd libim.so
linux-gate.so.1 => (0xb7f27000)
libm.so.6 => not found
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7dc9000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7c79000) /lib/ld-linux.so.2 (0xb7f28000)
I have discovered that my system was missing the libm.so.6. After googling a little I have figured out that libm is the motif library. With a apt-cache search libmotif:
apt-cache search libmotif
libmotif-dev - Open Motif - development files
libmotif3 - Open Motif - shared libraries
Ubuntu has a package for libmotif3. Installing it the ldd on libim.so din'd show bronken dependencies anymore:
# ldd libim.so
linux-gate.so.1 => (0xb7f27000)
libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7dd4000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7dc9000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7c79000) /lib/ld-linux.so.2 (0xb7f28000)
And adding the -l flag to the gcc, I got this output:
gcc -I/usr/local/tec/include/ -L/usr/local/tec/lib im_view.c -liup -lim -lcd
/tmp/ccKdeRAP.o: In function `CreateDialog': im_view.c:(.text+0x634): undefined reference to `cdContextIup'
/usr/local/tec/lib/libim.so: undefined reference to `operator new[](unsigned int)'
/usr/local/tec/lib/libim.so: undefined reference to `operator delete[](void*)'
/usr/local/tec/lib/libim.so: undefined reference to `operator delete(void*)'
/usr/local/tec/lib/libim.
so: undefined reference to `__cxa_pure_virtual'
/usr/local/tec/lib/libim.so: undefined reference to `__gxx_personality_v0'
/usr/local/tec/lib/libim.so: undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info'
/usr/local/tec/lib/libim.so: undefined reference to `vtable for __cxxabiv1::__class_type_info'
/usr/local/tec/lib/libim.so: undefined reference to `operator new(unsigned int)'
/usr/local/tec/lib/libim.so: undefined reference to `vtable for __cxxabiv1::__si_class_type_info'

collect2: ld returned 1 exit status

Very strange, as im_view.c was suposed to be a C program, not C++. Well the libs must have C++ operations, so let's compile with g++.
g++ -I/usr/local/tec/include/ -L/usr/local/tec/lib im_view.c -liup -lim -lcd
/tmp/cc01yIbF.o: In function `CreateDialog()':

im_view.c:(.text+0xd8): undefined reference to `cdContextIup'
collect2: ld returned 1 exit status
But there still one missing reference. Obviously I had missed -lcdiup, but:
g++ -I/usr/local/tec/include/ -L/usr/local/tec/lib im_view.c -liup -lim -lcd -lcdiup
/usr/bin/ld: cannot find -lcdiup
collect2: ld returned 1 exit status
So what's up.
Fortunately, the project has an on-line history log
http://www.tecgraf.puc-rio.br/cd/en/history.html
And looking at the history, I found this peace of information:
Changed: IMPORTANT - the "cdiup" and "cdluaiup" libraries moved from CD to IUP under the name "iupcd" and "iupluacd". But headers and documentation remains on the CD package. Function names were NOT changed. This change eliminates a cross-dependency that IUP and CD had, now only IUP depends on CD.

So, it wasn't -lcdiup, in stead it was -liupcd. I finally had my g++ command line:
g++ -I/usr/local/tec/include/ -L/usr/local/tec/lib im_view.c -liup -lim -lcd -liupcd
And here is a screenshot of my im_view running:


Im lib looks very promising. Now that I have runned my first example, I hope to learn it soon.

Converting a video file to lots of images

Still for that grad school work on stereo video, I want to convert the videos I made to a bunch of bitmap images.

With linux and ffmpeg it is easy. It is just type:
ffmpeg -i video.dv image%03d.bmp

There are more ffmpeg cheats at:
http://webupd8.blogspot.com/2009/08/ffmpeg-cheat-sheet-19-best-practices.html

Here is a couple of links very useful:

Specifically, the command line I was looking for is:

ffmpeg -i esquerda.dv -f image2 foo_%02d.bmp

Thursday, October 1, 2009

Google Bookmarks And Google Notebook

As an advise from my grad school advisor, I am now putting all references I am reading for my master's thesis on google bookmarks. There also is a short cut to directly add a page on ones bookmarks, using a simple firefox bookmarks toolbar button. The best part, is that with google notebook, another tool, one can share the bookmarks. So my advisor can check what I am reading.

The URL of my public bookmarks is:
https://www.google.com/notebook/public/02836905453520795692/BDT3iIgoQh_GewpYj