Keep changes in an array (SIMULINK)

Hey guys,
I want to fill a blank image iterative with ones. Most of it is done with matlab function blocks so far..
Example of my Problem/Idea: Let's say I have a binary image:
img = false([5,6]);
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
I have a signal which changes it's values and is the middle point of a (selfmade) rect:
dot = [3,2];
img(dot(1,1)-1:dot(1,1)+1, dot(1,2)-1:dot(1,2)+1)
0 0 0 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
I want to save it up, such that in the next step the old rect stays there, when dot changes to dot = [3,5]:
img(dot(1,1)-1:dot(1,1)+1, dot(1,2)-1:dot(1,2)+1)
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0
And with next iteration, when dot changes to dot = [4,5]
img(dot(1,1)-1:dot(1,1)+1, dot(1,2)-1:dot(1,2)+1)
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 1 1 1
etc..
I want to update the image step by step.
In my model the rect changes every time the values of dot change...
the function in the model (image down below) creates a rect with size 8by12 with a middle point coming from the variable input. Everything else is like my example description. Incoming binary image (78x120) is not full blank but hast lot of spots with 1. You can see it as a map with objects, but zeros = blank space, ones = occupied.
-
Is there a clever way to get this done in SIMULINK?
Thanks!

11 Kommentare

Walter Roberson
Walter Roberson am 6 Mai 2020
If you are using MATLAB function blocks, you can use persistent or global variables. However if you do use global variables then it is best to register them with the global data store, which I linked to above.
Domi
Domi am 6 Mai 2020
Bearbeitet: Domi am 6 Mai 2020
Could you be so kind to give me an example? Because I do not see how I can use it the right way.
global img
if isempty(img)
img = false([5, 6]);
end
Domi
Domi am 6 Mai 2020
By using global I get the error "The GLOBAL declaration must precede any use of the variable img."
Walter Roberson
Walter Roberson am 6 Mai 2020
Those lines should occur once, close to the top of the function, and not in a loop.
Domi
Domi am 6 Mai 2020
Bearbeitet: Domi am 6 Mai 2020
This is the function from the model im using (out of the image):
it creates a rect with size 8by12 with a middle point of [uPose,vPose] coming from the variable input. Everything else is like my example description. Incoming binary image is not full blank but hast lot of spots with 1. You can see it as a map with objects, but zeros = blank space, ones = occupied.
function img = fcn(img, randomVal, newimg)
global img;
uPose = randomVal(1,1);
vPose = randomVal(1,2);
vGesRobi = 12;
uGesRobi = 18;
vRobi = vGesRobi/2;
uRobi = uGesRobi/2;
vInflate = 2;
uInflate = 3;
vAdd = vRobi + vInflate; % 8
uAdd = uRobi + uInflate; % 12
u1 = uPose - uAdd;
u2 = uPose + uAdd;
v1 = vPose - vAdd;
v2 = vPose + vAdd;
%%%%%%%%%%%%%%%%%%%%%%%
img(v1:v2, u1:u2) = 0;
function img = fcn(img, randomVal, newimg)
global oldimg
if isempty(oldimg)
oldimg = false(size(img));
end
%somewhere in here, use oldimg in figuring out img
uPose = randomVal(1,1);
vPose = randomVal(1,2);
vGesRobi = 12;
uGesRobi = 18;
vRobi = vGesRobi/2;
uRobi = uGesRobi/2;
vInflate = 2;
uInflate = 3;
vAdd = vRobi + vInflate; % 8
uAdd = uRobi + uInflate; % 12
u1 = uPose - uAdd;
u2 = uPose + uAdd;
v1 = vPose - vAdd;
v2 = vPose + vAdd;
%%%%%%%%%%%%%%%%%%%%%%%
img(v1:v2, u1:u2) = 0;
oldimg = img;
Or... since img is an output, feed it back as an additional input after one timestep, like
function img = fcn(img, randomVal, newimg, oldimg)
%somewhere in here, use oldimg in figuring out img
uPose = randomVal(1,1);
vPose = randomVal(1,2);
vGesRobi = 12;
uGesRobi = 18;
vRobi = vGesRobi/2;
uRobi = uGesRobi/2;
vInflate = 2;
uInflate = 3;
vAdd = vRobi + vInflate; % 8
uAdd = uRobi + uInflate; % 12
u1 = uPose - uAdd;
u2 = uPose + uAdd;
v1 = vPose - vAdd;
v2 = vPose + vAdd;
%%%%%%%%%%%%%%%%%%%%%%%
img(v1:v2, u1:u2) = 0;
Domi
Domi am 6 Mai 2020
Bearbeitet: Domi am 6 Mai 2020
use oldimg in figuring out img
-> how?
I tried it like this.. it's still wrong :/
I think the usage of isempty() is not right in my case because the initial image is not fully blank because it has lots of space that are ones. You can see it as a map with objects, but zeros = blank space, ones = occupied.
I want to cover some parts of the ones with every change of randomVal and keep it such that it saves a whole new image/array. e.g.: Imagine an array full of zeros and after some time (lots of iteratiions) the image is full of ones.
function newimg = fcn(img, randomVal, oldimg)
global oldimg;
newimg = img;
uPose = randomVal(1,1);
vPose = randomVal(1,2);
vGesRobi = 12;
uGesRobi = 18;
vRobi = vGesRobi/2; % 6
uRobi = uGesRobi/2; % 9
vInflate = 2;
uInflate = 3;
vAdd = vRobi + vInflate; % 8
uAdd = uRobi + uInflate; % 12
u1 = uPose - uAdd;
u2 = uPose + uAdd;
v1 = vPose - vAdd;
v2 = vPose + vAdd;
%%%%%%%%%%%%%%%%%%%%%%%
newimg(v1:v2, u1:u2) = 0;
if newimg ~= oldimg
oldimg(v1:v2, u1:u2) = 0;
newimg = oldimg;
end
Walter Roberson
Walter Roberson am 6 Mai 2020
Your question stated,
Let's say I have an false array full of zeros (or binary image):
If that is not the case, then you should be using the initial map as an input signal and you should be sending the signal back to the block with a suitable delay mechanism.
Domi
Domi am 6 Mai 2020
Bearbeitet: Domi am 7 Mai 2020
Got it by myself. Thank you.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte

Gefragt:

am 6 Mai 2020

Bearbeitet:

am 7 Mai 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by