Sie sind auf Seite 1von 34

Elec4 Project - 2008/2009

Automatic Insect Monitoring


Renaud GENSOLEN
Guillaume LAUER
Jonathan MOLON
June 19, 2009

Internship mentor:
Eric WAJNBERG
Internship coordinator:
Serge MILLON

Acknowledgement
First of all, we would express our gratitude to our internship mentor Mr Eric WAJNEBERG for
his welcoming at his INRAs workspace and all the explanations about the project he gave us.
We also would like to acknowledge Mr Serge MILLON - our internship coordinator - for his
advice and experience feedback.
We thank Mr Vincent GRANET for his help in understanding some concepts we werent familiar
with.
We finally give thanks to the NXP team for sharing their working room and their support.

Contents
Introduction

1 Project overview
1.1 DC motors . . . . . . .
1.2 Linear power amplifiers
1.3 End stops . . . . . . . .
1.4 Incremental encoders . .
1.5 Motor DC card . . . . .
1.6 Joystick card . . . . . .
1.7 The camera . . . . . . .
1.8 The computer . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

7
7
7
7
7
8
8
8
8

2 Project charter
2.1 Project title . . .
2.2 Description . . .
2.3 Needs . . . . . .
2.4 Sponsors . . . . .
2.5 Stakeholders . . .
2.6 Project manager
2.7 Resources . . . .
2.8 Deliverables . .
2.9 Constraints . . .
2.10 Planning . . . . .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

9
9
9
9
9
9
9
9
10
10
10

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

3 Acquisition
11
3.1 An appropriate library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Adaptation in Trajecto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3 JNI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4 JPEG/BMP
4.1 Bitmap Image . . . . . . . . . . . . . . . . . . . . . . . . .
4.1.1 File header . . . . . . . . . . . . . . . . . . . . . .
4.1.2 Bitmap informations headers . . . . . . . . . . . .
4.1.3 Color palette . . . . . . . . . . . . . . . . . . . . .
4.1.4 Bitmap data . . . . . . . . . . . . . . . . . . . . .
4.2 JPEG Image . . . . . . . . . . . . . . . . . . . . . . . . .
4.3 Considered solution . . . . . . . . . . . . . . . . . . . . . .
4.3.1 Work directly on the JPEG images pixels . . . . .
4.3.2 Convert JPEG into bitmap . . . . . . . . . . . . .
4.3.3 Find a solution with AXIS the suppliers of camera

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

15
15
15
15
16
16
16
17
17
17
17

5 Image processing
5.1 Issue source . . . . . . . . . . . . . . .
5.2 Size and definition of the BMP picture
5.3 Problem with the matrix size . . . . .
5.4 Conversion from 3 bpp to 1 bpp . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

19
19
19
19
20

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

Conclusion
Appendix
A
Gantt Diagram . . . . . . . . . . . .
B
Acquiring an image (C-source) . . .
C
Acquiring an image (Java-source) . .
D
Jpeg2bmp.c . . . . . . . . . . . . . .
E
Matlab: obtain the binary code of an
F
Get an image in Trajecto . . . . . .

23
. . . .
. . . .
. . . .
. . . .
image
. . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

24
24
25
27
29
33
34

List of Figures
1
2

Project environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions sequence acquiring an image . . . . . . . . . . . . . . . . . . . . . . . . . .

7
13

List of Tables
1

Bitmap headers informations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

Introduction
The INRA - Agricultural Research National Institute - works on wild insects behaviour. A research
in the Lab of Biology of Invertebrates at INRA Antibes is to realize biological insecticides using
useful insects. Thus, to analyze their behavior, we must know their moving in various experimental
conditions. This experience requires a system performing and recording monitoring the movement
of insects in various environments.
Our work is to continue the work of previous student which was to develop an automatic
monitoring system of insects with a camera moving above the insect. This application will allow
researchers to perform statistics on wild insects movements.
This project begin again this year because last year a few weeks before presentation, the acquisition card burned making the camera and the entire project unusable.
The last team working on it and the coordinator chose to buy a camera working on IP network.
So the new project was to suit this new camera with the software Trajecto for, then, being able to
develop the part where the camera dynamically follow insects.

Project overview

The institute developed a device allowing the display of insect made by students in Polytech Nice
Sophia. This system consists of a camera which recorded the workplan and it connected to the
network. The result is observed on a PC which is connected to the network too. The camera is
positioned on two axes moving through a graphical user interface or a Joystick card . The camera
filmed the movement of an insect placed on the workplan. The image is then processed to get the
trajectory of an insect.

Figure 1: Project environment


The model is formed with several elements:

1.1

DC motors

Two DC motors are used to move the camera. These are connected to two worms located on two
orthogonal axes. The speed of these engines evolves according to the tension that we send from
power amplifiers.

1.2

Linear power amplifiers

The voltage setting ranging from -10V to +10V is amplified to supply DC motors.

1.3

End stops

They are active if the camera is a stop on one of axes.

1.4

Incremental encoders

Two incremental encoders were fixed on the DC motor and generate 500 pulses at every turn.

Each contains two channels out of step about 90 degrees to know the meaning of displacement
DC motors (see figure). These encoders will enable us to know the exact position of the camera
along two axes in impulses or centimeters for the dynamic tracking of moving insects.

1.5

Motor DC card

This card is the link between the motors, encoders, end stops and the computer.
It contains a pic microcontroller that provides transmission between the mechanical and computer via serial port and the enslavement of DC motors when moving the camera. Besides, it was
two digital analog converters transforming the digital record of eight bits in a tension between -10V
and +10 V sent to the power amplifiers.

1.6

Joystick card

The joystick card can order DC motors through 4 push buttons and a button on / off managing by
control relay placed on the motor DC card. This card can function without the microcontroller in
order to move camera without the computer.

1.7

The camera

This camera is an Axis 211M working under IP network.

1.8

The computer

It runs on the Linux operating system, it drives the system using the graphical interface program
developed in previous years: "Trajecto." It manages the movement of the camera but also treat the
experience made by the biologist.

Project charter

2.1

Project title

The title of this project is:


Automatic Insect Monitoring
His code name is: Trajecto.

2.2

Description

Designing a software using a camera working on IP network. This camera can:


View the movement of the insect on a screen
Follow the trajectory of an insect moving on a glass plate
Control motors which permit to move the camera in two dimensions
Automatically command motors to center insects in the center of the camera vue.
Motors are controlled by a PIC18FXXXX powered by an external 5V power.

2.3

Needs

Objectives are the study of a trajectory followed by an insect. It is a research project, so there is
no industrial needs.

2.4

Sponsors

INRA represented by Mr Eric WAJNBERG.


PolytechNice-Sophia represented by Mr Serge MILLON, co-responsible of elec4 projects.

2.5

Stakeholders

Stakeholders are represented by the projects sponsor.

2.6

Project manager

Mr Renaud GENSOLEN was designated as the project manager. Under the supervisition of the
projects sponsors, M. Renaud GENSOLEN has to bear, and got all powers to do, planning and
budgeting, monitoring, control and close the project.
Mr Renaud GENSOLEN will be assisted by Messrs Guillaume LAUER and Jonathan MOLON
to realize the project.

2.7

Resources

INRA provides a room equipped with a computer and in which we can install all the required
equipment (camera, motors).
PolytechNice-Sophia will provide us all required electronic equipments to test and programming
the PIC.

2.8

Deliverables

This project will get these deliverables:


The software Trajecto.
Datasheets.
These deliverables concerning INRA - project sponsor. PolytechNice-Sophia, other sponsor, will
define deliverables they waits for.

2.9

Constraints

The project starts this year with a new camera working on IP network.
The image flow must be around 20 images/second.

2.10

Planning

At the beginning of the project, we plan the differents objectives which we have to reach on the
first Gantt (cf. appendix A-left).
Then we have done a second Gantt with the differents problems we have met during this project
(cf. appendix A-right).

10

Acquisition

The software Trajecto works under Ubuntu - a Linux environment - with some additional libraries.
So we decided to recreate Trajectos environment on our computers. Thus we were not oblige to
move each Friday to the INRAs lab. By the way, we could also work on it at home.
Our team was finally ready to start this project !

3.1

An appropriate library

The previous camera had an owner library which worked only with that camera and that acquisition
card.
In the software Trajecto, the previous acquisition function was in the C-programmed part because the previous camera was working with an ISA port on the computer which could be easily
describe in C.
The new camera is working by the IP network so the communication could be directly describe
in the Java-programmed part. But we quickly saw that the program was using global variables. So
we had to stop and must decide if we will modify all the C code to avoid global variables or finding
a C-programmed acquisition library.
We took the second solution first and we find one library that may surely concur: libcurl
(http://curl.haxx.se/).
Then we had to write a first acquisition function to acquire images. We did and tested it quickly.
s t a t i c s i z e _ t write_data ( v o i d ptr , s i z e _ t s i z e , s i z e _ t nmemb, v o i d stream )
{
r e t u r n f w r i t e ( ptr , s i z e , nmemb , ( FILE ) stream ) ;
}
i n t main ( v o i d )
{
/ I n s t a n t i a t i o n /
CURL c u r l ;
CURLcode r e s ;
/ I n i t i a l i s a t i o n /
i f ( ( c u r l = c u r l _ e a s y _ i n i t ( ) ) == NULL)
{
f p r i n t f ( stderr ," I n i t i a l i s a t i o n f a i l e d :
curl_global_init ()\n ");
exit (0);
}
/ Debug f u n c t i o n /
c u r l _ e a s y _ s e t o p t ( c u r l ,CURLOPT_VERBOSE, 1 ) ;
/ URL D e f i n i t i o n /
c u r l _ e a s y _ s e t o p t ( c u r l ,CURLOPT_URL,
" h t t p : / / 1 9 2 . 1 6 8 . 0 . 9 0 / j p g / image . j p g " ) ;

11

/ No p r o g r e s s meter /
c u r l _ e a s y _ s e t o p t ( c u r l , CURLOPT_NOPROGRESS, 1L ) ;
/ Write f u n c t i o n d e f i n i t i o n /
c u r l _ e a s y _ s e t o p t ( c u r l ,CURLOPT_WRITEFUNCTION, write_data ) ;
/ Write f u n c t i o n params /
c u r l _ e a s y _ s e t o p t ( c u r l ,CURLOPT_WRITEDATA, f i c h i e r ) ;
/ R e t r i e v e i n f o r m a t i o n /
r e s = curl_easy_perform ( c u r l ) ;

/ Cleanup /
curl_easy_cleanup ( c u r l ) ;

We run into another issue which was that the obtain image was always formed of 128 bytes.
We found in the camera datasheet that to acquire the image we need an HTTP Authentication to
reach data. Fortunately, the library bears it and we had only two lines to add:

/ HTTP A u t h e n t i c a t i o n /
c u r l _ e a s y _ s e t o p t ( c u r l ,CURLOPT_HTTPAUTH,CURLAUTH_BASIC) ;
c u r l _ e a s y _ s e t o p t ( c u r l ,CURLOPT_USERPWD, " r o o t : t r a j e c t o " ) ;

3.2

Adaptation in Trajecto

Hence, we had our function we should now locate how we will insert it in Trajecto. So we asked
Mr Eric WAJNBERG to show us how to acquire an image in Trajecto.
Knowing how it works, we put in every function printf -or print- method then we can follow the
sequence of functions and methods. We find these links to acquire an image:

12

Figure 2: Functions sequence acquiring an image


In these function (cf. appendix B & C) we saw that the program were for 512x512 pixels images.
With the new camera we only cant have square image, we only have 4:3 format. We note that
we will solve that issue later by cutting a part of the image or suit the code to use with the new
camera.
Reading the code we saw that the program was treating bitmap images. Yet, we were only able to
acquire jpeg compressed images from the camera. So we thinked about implement a decompression
function or find a way to reach pixel matrix in jpeg.
We couldnt find a way to reach the matrix without using a decompression function.
So we had a talk with Mr Eric WAJNBERG that told us he wants to have a rate of at least 20
images/seconds. So get through a decompression function would be too slow. We measured it and
we could only get 1 or 2 images/seconds.
We then shared our issue with Mr Serge MILLON. He told us about a little society Agix which
is working on Axis solution -brand of the camera- that may help us finding a way to advance in our
project.
We phoned them and they find us a better solution: we can directly acquire a bitmap image.
Happy to learn it we directly programmed and tested it. It worked well but we reached another
issue. Our acquisition function were writing data into a file and we after read it to do the treatment.
The timing report was still not good, it takes to long to access the hard drive ...
We should find a way to store data in the memory. We dont find the solution in our library
documentation. We tried to ask the library support but it was only a professional support. Non-

13

free. We decided to see Mr Vincent GRANET, he maybe will give us a clue finding a solution. And
that is what happened, he saw that libcurl was an opensource library, so we have to directly modify
the library source to get our data in the memory and not in the hard drive.
We took times at this point because many concepts was there: modifying an existing library,
moreover modifying a callback function that can have everyname we want. So it was really hard to
find where we had to modify but with some clues and help we succeeded.
Now, we finally get an encouraging timing report. Lets get into the implementation of this code
into Trajecto.

3.3

JNI

With Trajecto we also learned that a software can work with multiple programming language. In
fact, Trajecto is design with Java and C.
The two programming language are linked by a programming framework JNI (Java Native
Interface) that allows Java code running in a JVM (Java Virtual Machine) to call and to be called
by native applications (programs specific to a hardware and operating system platform) and libraries
written in other languages, such as C.
These two programming language mingled we have to be really careful when we exchange data
between the two.
The issue we had is that Java is manipulating the bitmap data as "byte" type. This type
is present in C with the "unsigned char" type. But there is limitations in C using the keyword
unsigned and we could not use it because of libcurl that didnt accept that type. So we used the
"char" type. The difference between the two is that byte is unsigned (data can get value from 0 to
255) and char is signed (-128 to 127). So we add 128 to all the value recovered.

14

JPEG/BMP

The camera generates a JPEG image while Trajecto is designed for a bitmap,
Therefore, it didnt work with the camera image. In order to adjust the camera image in
Trajecto, it was necessary to understand how bitmap and JPEG images are formed.

4.1

Bitmap Image

A BMP file is a bitmap, ie a graphic image storing the pixels in a table of points and managing
color either in true color or wtih indexed palette.
The structure of a bitmap is :
a file header
bitmap informations headers
a color palette
bitmap data
4.1.1

File header

The file header is at the start of the file and is used to identify the file. All of the integer values are
stored in little-endian format : least-significant byte first.
The file header has four fields:
the magic number used to identify the BMP file: 2 bytes.
the size of the BMP file in bytes:4 bytes
4 bytes reserved
the offset,starting address, of the byte where the bitmap data can be found: 4bytes.
4.1.2

Bitmap informations headers

The bitmap informations headers provides information about the image, including its dimensions
and colors. These headers is
0
40
20

1
2
3
0
0
0
21 22 23
image size

4
24
x

5
6
width
25 26
x
x

27
x

28
x

9 10
height
29 30
x
x

11
31
x

12 13 14 15
1
0
8
0
32 33 34 35
number of colors

Table 1: Bitmap headers informations

15

16
0
36
x

17
0
37
x

18
0
38
x

19
0
39
x

4.1.3

Color palette

Color palette is optional. When a palette is set, it contains 4 bytes successively for each of its
representatives:
The blue component (1 byte)
The green component (1 byte)
The red (1 byte)
A byte reserved
4.1.4

Bitmap data

Pixels are stored "upside-down" , starting in the lower left corner, going from left to right, and then
row by row from the bottom to the top of the image.
The images in 2 colors use 1 bit per pixel, which means that a byte used to encode 8 pixels
The images in 16 colors use 4 bits per pixel, which means that a byte used to encode 2 pixels
The images in 256 colors use 8 bits per pixel, which means that one byte code each pixel.
The color images use 24 bits per pixel, which means 3 bytes to encode each pixel.
Each line of the image must include a total number of bytes that is a multiple of 4, if it is not the
case, the line must be supplemented with 0.

4.2

JPEG Image

JPEG is a commonly used method of compression for photographic images. The degree of compression can be adjusted, allowing a selectable tradeoff between storage size and image quality.
JPEG is a lossy compression method, which removes information from the image.
A JPEG image consists of a sequence of segments, each beginning with a marker, each of which
begins with a 0xFF byte followed by a byte indicating what kind of marker it is. Some markers
consist of just those two bytes; others are followed by two bytes indicating the length of markerspecific payload data that follows.
The encoding of JPEG file consists of several steps:
1. The representation of the colors in the image is converted from RGB to YCbCr, consisting
of one luma component (Y), representing brightness, and two chroma components, (Cb and
Cr), representing color. This step is sometimes skipped.
2. The resolution of the chroma data is reduced, usually by a factor of 2. This reflects the fact
that the eye is less sensitive to fine color details than to fine brightness details.
3. The image is split into blocks of 88 pixels, and for each block, each of the Y, Cb, and Cr
data undergoes a discrete cosine transform (DCT). A DCT is similar to a Fourier transform
in the sense that it produces a kind of spatial frequency spectrum.

16

4. The amplitudes of the frequency components are quantized. Human vision is much more
sensitive to small variations in color or brightness over large areas than to the strength of highfrequency brightness variations. Therefore, the magnitudes of the high-frequency components
are stored with a lower accuracy than the low-frequency components. The quality setting of
the encoder (for example 50 or 95 on a scale of 0100 ) affects to what extent the resolution
of each frequency component is reduced. If an excessively low quality setting is used, the
high-frequency components are discarded altogether.
5. The resulting data for all 88 blocks is further compressed with a loss-less algorithm, a variant
of Huffman encoding.

4.3

Considered solution

In order to resolve this problem: the type of image , we thought of several solutions:
Work directly on JPEG images pixels
Convert JPEG in bitmap with a C program
Find a solution with AXIS the suppliers of camera
4.3.1

Work directly on the JPEG images pixels

With this solution we have to directly manipulate the JPEG images matrix, so we ought to change
most of the functions which already exist on Trajecto. But this changes would have taken too much
time, moreover the Trajecto was created some years ago, so it would have been difficult to ask
some clarification on functions. And Trajecto program works very well with a bitmap image, thus
it would have been dangerous to change it.
Therefore, this solution was abandoned.
4.3.2

Convert JPEG into bitmap

Then we wanted to convert the JPEG image into a bitmap with a C program. So, we found a
library : jpeglib which allows to manipulate JPEG images.
We created a program which, using this library, can change the format of the image. We could
see that for a JPEG image taken on Internet, a BMP image was created in a file (cf. appendix D)
But the issue, with this solution, is that we copy the image on the disk so we lost some time to
get a bitmap image. And objective is to get 20 images/sec, so we calculate the execution time of
our program, then we had for 20 images a time of 750 ms. This is very fair because the time was
calculated without manipulation in Trajecto.
4.3.3

Find a solution with AXIS the suppliers of camera

Then we called AXIS: manufacturer of the camera, to know if it exists a C library with the camera
in order to change the format of the image. They give us a piece of program and an URL which
was really more interesting for us, because it directly gives us.

17

We managed to have a bmp image generated by the camera and we could display it
in a file with our C program. It was able to include this program in Trajecto, we
managed to have an image in Trajecto but it is not the same as the image from the
camera.

18

Image processing

The matrix containing the pixels of an BMP image is the matrix chunk.memory of char* type.
It is observed that this image recovered in a file using a writing function is correct. Whereas the
obtained image under trajecto is completely different. It is observed that the obtained image is a
repetition of a part of the original image acquired by the camera with a 90-rotation.
We have to find where and why this issue came. It could be a function which was called by an
indirect way and which could modified the image. Or maybe the image format? Issues could be
numerous and then we should find the good one. Thus we have to begin the study obtained image.

5.1

Issue source

By passing its size in the code with chunk.size. The original image had a dimension of 320*240
pixels, the dimension of the recovered table should be 320*240 too, multiplied by the number of
bytes per pixels plus some bytes for the signature, the headers and the palette. Nevertheless, the
recovered table was 345654 bytes long, more than 320*240=76800. This could correspond to an
bitmap of 4 bpp (bytes per pixel). So we chose to show this issue to Mr Million to have an external
vue on the subject. After this meeting, Mr Million guided us to a possible issue on the BMP type
or JNI conversion between string and table of bytes.
On PhotoShop we saw that we can generate various type of BMP with a different number
of bpp. So we generated these various types and we put them on Internet to then simulate under
trajecto the acquisition of these image by IP. We observed that the image was correctly recovered
under Trajecto for an BMP image with a 512*512 dimension and with 1 bpp. A new issue come:
the size of the image as well as its definition.

5.2

Size and definition of the BMP picture

After some alterations in the code regarding the dimension of the string in several file as well in the
Java part as in the C part. Trajecto with an image sized as 320*240 with 1 bpp is working now.
So we have to find what is the definition of the BMP returned by the camera. During this year,
one of our tutorials in Java was related to the study of an BMP image. With this tutorials we
could read, in the heading, the dimension of the pixels matrix which was 230400 bytes long. So
the image had 3 bpp.
But we had an other issue: why the size of the table obtained was 345654 bytes long? There
was 115254 additional bytes into the pixels matrix. This number was too big to only describe the
heading, the signature and the palette. There was a new issue with the matrix size.

5.3

Problem with the matrix size

While we simulated with our images on Internet, we observed the dimension returned by the
program for the matrix was 76900 bytes which equals the correct number of pixel of the image plus
some bytes for the information of file BMP. Whereas for an obtained image by the camera if it is
considered that the information of the BMP holds on some bytes, the additional number of bytes
is really important. So we couldnt modify the pixels matrix if we couldnt recover it.
We wondered how come these additional bytes. It surely comes from the callback function associated with the library libcurl which is called many times because of the authentification necessary
to be connected to the camera. Because it is only the difference between an image obtained by
the camera and another obtained by Internet. But the issue was not to know the origin of these

19

additional bytes but to find a means of recovering the matrix of the pixels and the informations of
the image.
Several solutions were considered. Firstly, we wrote a code in MATLAB (cf. appendix E) to
generate a textual file containing the binary code corresponding to the picture. Then we have to
search in this binary code the magic number in the heading of a BMP which is, in hexadecimal,
0x424D. But the issue was that considering the numbers of bytes in this file, this number didnt
appear one time moreover this magic number corresponds to the OS from where the image was
generated. 0x424D correspond to a Windows OS.
The second solution was to write in the file image only a part of the matrix and observe the
.bmp file. We wanted to recover the pixels matrix as well as headers informations. But we thought
these data were at the end of the matrix. We have to pass only the end of the table but we doesnt
obtained a BMP picture. So we decided to keep only the beginning and we obtained a part of the
image. The we knew that the additional bytes were after the file. We obtained the complete image:
230454 bytes long. This is a table of pixel of 230400 bytes (230*420*3) plus 54 bytes for headers
informations. Now that we had our pixels matrix, it was necessary to find a conversion method.

5.4

Conversion from 3 bpp to 1 bpp

Our tutorial in Java was also very useful for this part. The 3 bytes describing each pixels corresponds
each one to a color which are red, green and blue.
In the string values are between -128 and 127, so we used a function unSigned to have only
positive values thus ranging between 0 and 256 which correspond to the byte type in Java.
/ Function unSigned
Param : d : t h e c h a r
Return : t h e a s s o c i a t e d p o s i t i v e i n t e g e r v a l u e t o t h e c h a r
/
i n t unSigned ( c h a r d )
{
r e t u r n d+128;
}
The second function makes it possible starting from a number of bytes to obtain a corresponding
integer.
/ Function g e t I n t
Param : d : s t r i n g
Param : o f s : i n d e x from which we s t a r t r e c o v e r i n g t h e v a l u e
Param : l g t : number o f b y t e s we w i l l u s e
Return : t h e a s s o c i a t e d i n t e g e r t o t h e l g t b y t e s
/
i n t g e t I n t ( c h a r d , i n t o f s , i n t l g t )
{
i n t i , j , s =0;
f o r ( i=t a i l l e 1; i >=0; i )
{
i n t tmp = unSigned ( d [ o f s+l g t 1 i ] ) ;

20

f o r ( j=t a i l l e 1; j >i ; j )
tmp = 256

s += tmp ;
}
return s ;

Then the function conversion create a table containing only heading, signature, palettes and
the new pixels matrix. In this new table the information part is non-changed then in the pixels
matrix we read 3 bytes per 3 bytes to generate an associated integer. This integer is then compared
with a threshold value to know if we have to generate a white pixel on a byte (+255) or a black
pixel (0). This threshold value is obtained by taking the central value of the possible integer with
generated from 3 bytes.
We observed that the obtained image obtained is shifted on the right because between the
palette and the beginning of the pixels matrice, there is a gap of 18 octets.
/ Function c o n v e r s i o n
Param : mem: t h e p i x e l s matrix
Return t h e c o n v e r t p i x e l s matrix
/
c h a r c o n v e r s i o n ( c h a r mem)
{
int x , y ;
c h a r tab = m a l l o c ( s i z e o f ( c h a r ) 3 2 0 2 4 0 ) ;
/ Browse t h e p i x e l s matrix 3 p e r 3 b y t e s /
i n t l a s t _ p x l = 320240 1;
f o r ( y=0;y <240; y++)
f o r ( x=0;x <320; x++)
{
/ Comparing b y t e s r e c o v e r e d by t h e s e 3 b y t e s
with t h e c e n t r a l v a l u e o f p o s s i b l e c o l o r s .
The gap 18 c o r r e s p o n d t o t h e number o f b y t e s
between t h e p a l e t t e and t h e b e g i n n i n g
of the pixel s t a b l e
/
i f ( g e t I n t ( mem, ( x+320y +18)3 ,3) < 8 3 8 8 6 0 7 )
tab [ x+320y ] = 2 5 5 ; / p i x e l n o i r /
else
tab [ x+320y ] = 0 ; / p i x e l b l a n c /
}
}

r e t u r n tab ;

21

We finally obtain an image with the camera in Trajecto (cf. appendix E).

22

Conclusion
The project tooked place in the informatics domain and especially into informatics languages C
and Java. That helped us to strengthen our bases in programming.
The constraints were numerous during this project. First of all, Trajecto is a software started
several years ago and so, many students change its code. It were almost no comments in this code.
So the comprehension of this code was very long and very hard at the beginning of our project.
The work that we have done was primarily a work of debug with the many ways which exists in
data processing (screen impression, simplification of the code by removing the useless functions,. . . ).
We improved our reflexion capacities face an issue in data processing and of course it is not the
most pleasant part in data processing.
We have also improved other fields: image processing and in particular the BMP type. We
expanded our knowledge also on JPEG images. We could exploit of another tools to help us in this
field such as Matlab. We could see that the problems encountered during the image processing are
not easy to interpret then to solve them.
The first objective takes more time than we had plan. So, we realized its very difficult to respect
a planning because we didnt know problems we could have.
Thank to this project, we really could touch several fields for then being confronted with problems of various nature to finally solve them with our team. The study of an issue as a team was
very rewarding because each members of the team have a different vue on it. Whence the solutions
were numerous and allowed us to work on various solutions to finally choose the best.

23

Appendix
A

Gantt Diagram

24

Acquiring an image (C-source)

25

26

Acquiring an image (Java-source)

27

28

Jpeg2bmp.c

29

30

31

32

Matlab: obtain the binary code of an image

33

Get an image in Trajecto

34

Das könnte Ihnen auch gefallen