How to deal with video frames in simulink - simulink

I am working on videos in simulink.As we know that multimedia file block reads one frame at a time so when I will attach it with matlab function block it should read one frame at a time with imread command. If I double click the matlab function block (as shown at http://tinypic.com/view.php?pic=dggujd&s=6) then you will see that I have to give the name of the function as written in matlab mfile along with the input; which in this case is video(named as convid.avi). I am reading one frame at a time but I have given the whole video as an argument to the matlab function.This is the problem how can I resolve it.What should be given as an argument in matlab function block rather than the whole video.I have also uploaded my model at http://tinypic.com/view.php?pic=55jggw&s=6. The code which I am using for vidfunc is:
function h=vidfunc(u)
a=imread(u); % read frame
BW = edge(a,'sobel'); %sobel edge detection
[H,thetaa,rhoo]=hough(BW); % Hough Transform
P = houghpeaks(H,6,'threshold',ceil(0.5*max(H(:))));
lines=houghlines(BW,thetaa,rhoo,P,'FillGap',15,'Minlength',15)
figure,imshow(I),hold on
for k = 1:length(lines) % Draw lines
xy = [lines(k).point1; lines(k).point2];
z(k)=lines(k).point2(2);
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
end
h=z(k)

It looks like the meat of the work done in this "Simulink" model is actually being performed by your MATLAB function. So the answer to this question is going to largely rely on what that function is actually doing. Specifically, what is the expected input to vidfunc, and what is this function's output? I suspect that this function may need to be revised to fit into your model.
To debug your model, it's useful to think about what the signal output is from each block. At each time step, your From Multimedia File block will output a single image frame, which according to the doc looks to be structured as an
M-by-N-by-P color video signal where P is the number of color planes.
Moving downstream, we next come to the Color Space Conversion block, which in this case looks like it will most likely output an image frame in the form of an M-by-N matrix (where each element of the matrix corresponds to the image's intensity at that pixel).
Now we come to the interesting part -- the MATLAB Fcn block. As we just saw, the input to this block will be an M-by-N matrix representing a single image frame. When you look in the parameter dialog box for the MATLAB Fcn block, the input to this block is represented by the variable u. Therefore, to execute the vidfunc function on the image frame being input to this block, you would simply enter vidfunc(u) for your MATLAB function.
Now, based on the input going to the MATLAB Fcn block, and the fact that you have a Video Viewer block connected to the output, vidfunc should be structured such that it operates on a single image frame as its input and outputs another single image frame. If vidfunc is not structured in this way, you will need to edit it (or just re-implement the same functionality using Simulink blocks).
That said, let's assume that vidfunc is also returning an M-by-N matrix representing a processed image frame. You will want to set the Output Dimensions parameter for your MATLAB Fcn block to -1 to indicate that the output will have the same dimensions as the input. Also, (as indicated in the doc) you will want to make sure Collapse 2-D results to 1-D is unchecked or else your image output will be in the form of one long vector rather than an M-by-N matrix.
Provided that vidfunc is structured correctly, this should solve your problem.
NOTE: To make your life a lot easier, I would strongly suggest displaying the signal data type and dimensions in your Simulink model. This can help to avoid a lot of confusion. This doc describes exactly how to do this.
--UPDATE--
After looking at your code, this confirms my suspicion that the inputs/outputs of vidfunc are inconsistent with what your Simulink model expects. The way that you proceed is highly dependent on what your own design constraints are and what you actually want out of this system. Basically, your Simulink model and MATLAB function disagree... which one is right? I'll give some general thoughts based on my best guesses at what you're aiming for.
First, Simulink is passing an image (in the form of an M-by-N matrix) into vidfunc. This means that vidfunc no longer needs to load an image at the start of the code. So I believe that you could update the first few lines of code to be as such:
function h=vidfunc(a)
BW = edge(a,'sobel'); %sobel edge detection
See that now vidfunc is taking the actual image (not the filename that contains the image) as its input. Basically, you are removing the a=imread(u); line and jumping right in to processing a.
The other issue is the output of vidfunc. Simulink is expecting the output to be an image, but it is not. I'm not 100% sure what h is supposed to be in this code (when I first glanced at your code I thought these were handles to line objects but that doesn't seem to be the case). It looks to possibly be the y coordinate of the endpoint of one of the houghlines. Nevertheless, it's not what your Simulink model is expecting. This one is not as straight-forward to fix. Possibly you could try to use getframe to grab an image from the plot of your lines.
I actually feel that the best advice that I can give to you is to just scrap the MATLAB function and implement everything in Simulink. I think this will be a lot easier than trying to get vidfunc to play nicely with your model. vidfunc does not actually contain that much code, so this should not be too difficult of a task for you. Another benefit is that at the end of this process you'll have a nice Simulink model that explicitly shows all of the image processing steps that you are taking.
I believe that all of the image processing that you are doing with MATLAB functions can also be done with Simulink blocks (see the Simulink Blocks section of this doc).
Good luck.

Related

How to use imbinarize function in matlab correctly when the input is a 3D data

I am trying to use Otsu's thresholding method on my 3D matrix by using "imbinarize" function on Matlab. There are no error warnings, but the result is incorrect.
If I compare the original 3D matrix to the thresholded model, the thresholded model looks very bad.
This is the code that I ran on Matlab:
T2=graythresh (data);
BW2=imbinarize(data,T2);
BW2=single(BW2);
figure ();
volshow(BW2);
figure ();
volshow(data);
After imbinarize my single data turns into a logical data, so I added BW2=single(BW2) for consistency. I feel like the issue is caused by turning the single data to logical data.
Before and after adding this line.
Here are my results. The left model is the thresholded result and the right model is the original one.
After comment of Alamakanambra I changed normalized my input before using imbinarize, which led to a better result, but the visualization of the data is still different (e.g. texture).
I don't understand why my output is worse than my input. What have I done wrong?

Sample sequence of points from continuous signal simulink

I have a Matlab function (created by me) that must be evaluated only at a given rate. I would like to sample the value of a signal, give to this function (discrete values) and then, the calculated output must be hold until the next value is available. Is there a way in simulink to do this? All answers I have found use quantizer + ZOH but in this case I still get "a continuum" (or almost it) of points to be evaluated by thsi function which is really slow. Changing the rate of simulink's solver is also not an option as the result of this function will be given for a continuous time system.
Any help will be highly appreciatted!
Thank you
Assuming by Matlab function you mean a MATLAB Function block, then it sounds as if all you need to do is make the block discrete. Do that by right-clicking on the block, going down to Block Properties and then in the resulting dialog enter your required sample time.
The block will then sample its input and generate an output (which is held between sample times) at each sample time.

Matlab Function inside simulink

I need to use a Matlab function inside of a Simulink model. I know how to use a Matlab function to do simple stuff. But what I need now is a little bit more complicated. Let me give you a basic example.
Assume that I need to have a block to generate a sine wave to be viewed directly on the scope (I know that there is already a sine-wave block, I'm just taking that as an example). If I'm writing in Matlab NOT in Simulink, I would do something like:
t = [0:1/30000:0.2];
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
plot(t(1:100),y(1:100))
How can I build the same function inside a Simulink matlab-function block and see the results directly on the Scope?
Remember: The Matlab Function Block has two ports, u and y. Which represents input and output respectively. In the above-given example, a sine-wave generator doesn't need an input.
Perhaps it's just that you haven't chosen a very good example, but there are several things to be aware of when translating the code you've given into Simulink.
The easiest way to get the simulation time into a MATLAB Function block is by feeding a Clock block into an input port (which as #Daniel indicates, are optional, but in this case I suggest would be used for t). So I think you do want an inport in this example.
Your use of plot in your example only plots the first 100 points, where as a Simulink Scope rolls through the data being displayed. There's no concept of only displaying the first X points in a Scope when the simulation runs for longer than that.
You need to remember that Simulink generates data one simulation time step at a time, so you can't generate them all (as per your MATLAB code) and then plot them all. (Well... you can if you want to use frame based signals, but I assume that's not what you're asking here.)
So, to implement the equivalent of what you have would involve doing the following.
write a MATLAB Function block containing the following code (although you might want to make A and f input parameters rather than hard coding them)
function y = myCustomSineWave(t)
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
Feed a Clock block into the above block, and have a Scope block on its output

Generating square wave using Matlab embeded function

I intend to generate a square wave which is applied on a DSP.
I have written these codes and put them in an embeded Matlab function.
function y = fcn(u)
%#eml
t=0:0.001:1
h = sign(sin(125600*t+u));
y= (h+1)/2
where, u is a constant value of 0.582 which is used for shifting the square wave.
The problem is at the output in the simulation, instead of getting a square wave, I see only two straight lines of y=o and y=1.
Please let me know where is the problem that I can not get the square wave?
Note that the frequency of square wave must be 20 kHz. Therefore, I adjust the sampling time as 1e-7 s. And also its amplitude is between 0 and 1 In addition, due to this signal must be transferred to a DSP board, in the "solver option" I chose the type: " Fixed-step" and for the Solver: "Discrete (no continues state)".
Thanks a lot.
This is wrong on many levels.
First of all, you never define the time vector inside a MATLAB Function, that's what the Simulink engine does. Pass time as an input to your MATLAB Function block and use a Clock block to generate the time input.
Second, the above is fine for simulation, but it sounds like you are generating C code from the Simulink model to run it (in real-time) on your DSP. This is not my area of expertise, but from memory, I think you need to enable "absolute time" or something similar for the above to work with code generation. However, I think this is target-dependent and so I'm not sure whether this will work on your DSP.
In you function type plot(t,y) at the end. You are generating a 20khz square wave (assuming you are sampling at 1e-7). Essentially your generating it is working.
Now, what is the DSP board you are using/any information that is relevant to your problem?
I don't know what you are referring to when you say "Solver" either.
Is the "simulation" an oscilloscope or a program? Either way perhaps it is not triggering correctly? Is there an edge trigger option?

Inconsistency between Simulink Scope and its Matlab plot obtained using to Workspace block

I'm running a SimEvents simulation using Variable-step discrete solver. I save a signal data using a 'to Workspace' block, but the plot that I obtain is different from the one shown in the 'Scope' block inside the model.
The original signal remains constant between t=64[h] and t=65.4[h] (and this seems to be correctly done also in the matlab plot), while elsewhere it is like the plot command and the 'scope' block are working with different "sample times".
I'd like to obtain a plot showing the typical "step shape" of a discrete signal rather than a "nearly continuous" signal.
I've used the Scope 'save data to workspace' as well, but I didn't solve the problem.
I would have attached some screenshots in order to make the question more complete, but this is my first question so I have not enough reputation to post images. If you need, I can send them to you via e-mail.
Thank you in advance!
There are no screenshots to look at, but my guess would be that the signal is discrete, and the Simulink scope block, knowing this, is only changing at the discrete time points.
However, if you are plotting the data dumped to MATLAB using the standard plot function then it treats the data as continuous and joins successive points with a (non-constant) line.
If that's the case, you most likely want to be using the stairs function to do your MATLAB plottig.