Using loops to draw pixels
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
How about would one go in using a nested FOR loop with IF's to plot pixels onto a screen. I have a screen with width 500 and length 300 and I would like to use the loops to plot a line red pixcels across the middle.
0 comentarios
Respuestas (1)
Cam Salzberger
el 23 de Oct. de 2017
Hello Jason,
MATLAB is not able to draw directly on the screen. Most plotting capabilities must be done within a figure or uifigure window. I am assuming that is what you are referring to by "a screen with width 500 and length 300".
If you put an axes on the figure, you can simply use plot to draw a line. However, you would be plotting in "data units", and would have determine the translation of thickness of the line to pixels, so this may be less idea for your workflow.
Likely the easiest method would be to simply create an image, and then display it with image or imshow. An image in MATLAB is just a matrix, so you can simply create a blank image at first, and then modify the pixels you desire to be red.
You could call image or imshow each time you modify the image, but it's more efficient to get the handle to the image when you first create it, then just modify the CData property to change the pixels on each iteration.
If you want to make a point red, the image will have to be a 3-D matrix (500 x 300 x 3). A red pixel will have the first layer be max value, and the other two layers be min value.
I hope this helps to get you started.
-Cam
2 comentarios
Cam Salzberger
el 23 de Oct. de 2017
I'm still a little confused when you say "window". Do you mean that you have some other objects/UI elements in a figure, and you want to display some red spots over those, but not block them out with the rest of the image?
I guess that gets a bit trickier. You can make the axes go invisible pretty simply, as long as you do it after displaying the image. Then you can use the AlphaData property of the image to make most of it transparent, leaving only the red line. Here's a quick example:
figure
hAx = axes;
I = ones(300, 500, 3);
hI = image(I, 'Parent', hAx, 'AlphaData', ~I(:, :, 3));
hAx.Visible = 'off';
for k = 1:size(I, 1)
I(k, k, 2:3) = 0;
hI.CData = I;
hI.AlphaData = ~I(:, :, 3);
pause(0.05)
end
-Cam
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!