Wednesday, April 30, 2014

Reading Serial Port Data & Plotting Gray Image in Matlab

I my previous post I connected a OV7660 camera module to STM32F4 Discovery board. Then I was needed to display the data transmitted by micro controller to PC as an Gray scale image. To do that I use Matlab. Using Matlab reading a serial port is easy, but you must get familiar with matlab.

My micro controller sends data as bytes (0-255) to PC which indicating intensity values of each pixel in image. I pick those bytes by reading the relevant serial port in matlab. I suggest you to get familiar with matlab Serial functions before looking at the below code .

This code containes only few lines, but it does much work to read and display bytes as an image.


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
clear all
s = serial('COM1');
s.BaudRate = 921600;


%get(s);

image = zeros(70,80);
row =1;
col = 1;

fopen(s);
pix = fgetl(s);

while str2double(pix)~= 256
    pix = fgetl(s);
    
end
while str2double(pix)~= 300
    pix = fgetl(s);
    
end

for i = 1:70*80
 pix = fgetl(s);
 
 %class(str2double(pix));
 image(row,col) =str2double(pix);
%image(row,col) = 255;
 if  mod(i,80) == 0
     row=row+1;
     col = 0;
 end
 col=col+1;
end
fclose(s);
%figure()
colormap(gray);
imagesc(image);


    

Following video presents how this code works. Each and every time pressing the Run icon, It will show the image captured by camera.


Comments are warmly welcome!

No comments:

Post a Comment