Thilina's Blog

I might be wrong, but…

View Streamed Image data on Simulink in Real-time

Two more months to go and it ends the final semester of final year at the University. All are rushing to finalise their final year projects, do simulations again and again, create test cases etc. Since our final year project is related to FPGA implementation of an image processing application, we used to work with images and they need to send as a data stream to the FPGA for processing. When performing simulations (Using MATLAB Simulink Xilinx Block set) we created the image in to data stream and sent it via Ethernet cable to the FPGA, so called Hardware co-simulation. The way we created the data stream is described in my previous article and I wish to add another article on Hardware Co-simulations in future.

However we had a small problem which the resulted image can only be viewed after performing the full simulation, which takes a lot of time, sometimes it takes hours. So finally by using a combination of MATLAB function block, unit delay block and video viewer block we were abled to design a model which reshapes the image stream back to its original image in real-time. From this article I will share my experience on creating the real-time image viewer for image streams.

As I described previously the model consists of three blocks. The video viewer is in the “Video/Image processing tool box” or in “Computer Vision System tool box” in Simulink depending on the MATLAB version you use. The unit delay block set can be found in “Simulink block” set under “Discrete” category. This block is used to stop creating an algebraic loop during the simulation.

The most important part of the model is the function inserted in the MATLAB function block is as follows.

function [image, imgout] = imageControl(data, index, eos, img)
  row_cnt = 130;
  col_cnt = 198;
  if (eos==0)
    if (index<2)
      frm = zeros(row_cnt,col_cnt);
      image = cast(frm,'uint8');
      imgout = cast(frm, 'double');
      image = cast(img,'uint8');
    data = cast(data,'uint8');
    x = 1 + rem(index,row_cnt);
    y = 1 + floor(index/row_cnt);
    image(x,y) = data;
    imgout = cast(image, 'double');
    imgout = cast(img, 'double');
    image = cast(img,'uint8');

The variable “data” is used to send the processed pixel from the application. Index is simply a counter output which defines the pixel number. “eos” is the flag which goes up with the last pixel of the image comes out from the application which denotes that very last pixel of the image has also been processed. “img” is the feedback reference of last output image.

Since MATLAB function block does not have a storage kind of thing we store the image in delay block (since direct feedback is impossible) and update that image with new data. When using this model you need to update the variables “row_cnt” and ”col_cnt” by the number of rows and columns of the image. Block diagram which describes how to connect the model in to an application is as follow.


The blocks “step1” and “step2” is used to represent the flags of data out is ready and end of stream. With this block you can visualise the image processing simulations as follow using video viewer block.


From that I conclude this article and hope that I shared some useful information with you. Thank you very much for reading.

2011 September 25 - Posted by | Electronics, FPGA, Image Processing, MATLAB


  1. Good try. It will be really great if you achieve real time streamed data acquisition using MATLAB. But, if you feel you need higher acquisition speed I think it will be wise to use “LABVIEW” will it specially built for real time interfacing and data acquisition. Don’t try to medal with “LABVIEW” right now since you have been working with MATLAB thus far. So, try to improve what you have developed already. But, if you still feel higher speeds are necessary, try “LABVIEW” if you hope to carry your work forward, Wish you all the very best !!!


    Comment by Nalika | 2011 September 26 | Reply

    • Thank you very much Nalika. I’m not sure whether LABVIEW has a block set which supports to compile and send hardware code + data in to FPGA. If its so it will be a great help to us. Since MATLAB has Xilinx Simulink block set which compiles the HDL code and configure FPGA and allows send data and receive data via Ethernet cable we made these two models to send and receive data. Thanks again.. 🙂

      Comment by Thilina S. | 2011 September 26 | Reply

  2. Hi

    i am also doing the same thing.
    Firstly i have saved video data by converting in 8 frames and each frame is having matrix 76800.
    I have saved in it array of size (8*76800).
    But the problem is how to take only two frames in serially so that some processing can be done.

    Comment by shamim | 2011 October 17 | Reply

  3. you used the max_cnt , what is that value here ??

    Comment by amith khandakar | 2012 November 16 | Reply

    • its taken from workspace.

      Comment by Thilina S. | 2012 November 16 | Reply

      • .thank u so much for the reply. What is the significance of the value and wat was the values in your case and did u get it . if possible can you send me your email address as i have more enquiries on a certain that i am tryin to work on and was wondering if you could help me . Thank u once again

        Comment by amith khandakar | 2012 November 16 | Reply

      • ms thilina , i have tried it the way u showed it above but just as a query did u use the default for Step 1 and Step2 blocks.

        Comment by amith khandakar | 2012 November 17 | Reply

      • Mr. Thilina , badly need your help as i tried your simulink model and get a blank screen on the video viewer, really confused about the reason.

        Comment by amith khandakar | 2012 November 17 | Reply

  4. Hello,
    I am currenly working in an imageprocessing system genrator work.I am trying to view the images using the video viewer. Though it showed no errors,the image cannot be displayed in the video viewer. It is taking alot of time, but still no image comes up and gets stuck.Do you have any idea how to rectify the problem?

    Comment by Sindhu | 2017 May 19 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: