How to show image and plot simultaneously in loop - matlab

I want to show image and plot in the loop. I want to show image in one figure and plot in other figure. So I try to use my code, but it does not work. Could you help me fix it? Thanks so much
x=0;
x_arr=[]
I=imread('peppers.png');
figure
for i=1:100
if mod(i,10)==0
pause(0.5);
x=i.^2+1;
x_arr=[x_arr x]
%show image
hold on
imshow(I);
hold off
%show plot
pause(0.5);
hold on
plot(y_arr);
hold off
end
end

you can do so by using figure for handeling 2 windows:
x=0;
x_arr=[]
I=imread('peppers.png');
for i=1:100
if mod(i,10)==0
pause(0.5);
x=i.^2+1;
x_arr=[x_arr x];
%show image
figure(1)
imshow(I);
%show plot
pause(0.5);
figure(2)
plot(x_arr);
end
end
or by using subplots to keep it in one window:
x=0; x_arr=[]
I=imread('peppers.png');
figure (1)
for i=1:100
if mod(i,10)==0
pause(0.5);
x=i.^2+1;
x_arr=[x_arr x] %show image
subplot(1,2,1);
imshow(I);
%show plot
pause(0.5);
subplot(1,2,2)
plot(x_arr);
end
end

Related

How to plot an array using a for loop?

I want to plot inside a for loop. I tried the following code also used hold on but the plot is still blank. I don't know where I am getting wrong.
M2 = true(21, 6);
M2(1:2, 3:5) = false;
R = [0.5:0.1:2.5];
H=[0:5:25];
m=21,
n=6,
for i=1:m
for j=1:n
if M(i,j)==0
plot(H(i),R(j), 'color', 'r')
drawnow();
end
end
end
As Ander Biguri already said, the plot function actually does not draw anything when you provide only one point and do not specify any markers.
But, if you want to create some animated plot, that one of the points is displayed at a time:
h = scatter(0, 0, 'r');
xlim([min(H) max(H)]);
ylim([min(R) max(R)]);
for i=1:m
for j=1:n
if M(i,j)==0
set(h, 'xdata', H(i), 'ydata', R(j));
pause(0.5);
drawnow();
end
end
end
Or if you want to show all point together, you don't even need a loop:
[HH, RR] = meshgrid(H, R);
scatter(HH(~M), RR(~M), 'r');
xlim([min(H) max(H)]);
ylim([min(R) max(R)]);

Histogram equalization function

I'm trying to implement the histogram equalization using this code:
clc
A=input('please enter image adress','s');
Iimg=imread(A);
Iimg1=double(Iimg);
histi=imhist(Iimg);
mmax=max(Iimg1(:));
h=histi/numel(Iimg1)
cdf=cumsum(h)
cdf=cdf*double(mmax);
c=uint8(cdf);
subplot(1,3,1)
bar(c)
subplot(1,3,2)
imhist(Iimg)
subplot(1,3,3)
imhist(histeq(Iimg))
Is my code wrong?
I don't get expected results.
I get this from your code:
What is wrong? unless your image is rgb, the code works (I used cameraman).
i find correct code for this and write here for others
clc
A=input('please enter image adress: ','s');
GIm=imread(A);
[x, y ,m]=size(GIm);
if m==3
GIm=rgb2gray(GIm);
end
inf=whos('GIm');
Isize=0;
if inf.class=='uint8'
Isize=256;
else if inf.class=='uint68'
Isize=65565;
end
end
HIm=uint8(zeros(size(GIm,1),size(GIm,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
freq=imhist(GIm);%histogram
sum=0;
no_bins=255;
probc=cumsum(freq)/numel(GIm);
output=round(probc*no_bins);
HIm(:)=output(GIm(:)+1);
%show
figure
subplot(2,2,1)
imshow(GIm);
title('original image');
subplot(2,2,2)
imshow(HIm);
title('Image equalization');
subplot(2,2,3)
imhist(GIm);
title('origina');
subplot(2,2,4)
imhist(HIm);
title('histogram equalization');
figure
subplot(2,2,1)
imshow(histeq(GIm));
title('matlab equalization');
subplot(2,2,2)
imshow(HIm);
title('my code equalization');
subplot(2,2,3)
imhist(histeq(GIm));
title('hist matlab eq');
subplot(2,2,4)
imhist(HIm);
title('hist my code eq');
figure
subplot(2,2,1)
imshow(GIm);
title('origina');
subplot(2,2,2)
imshow(HIm);
title('Image equalization');
x=(1:Isize);
subplot(2,2,3)
plot(x,output);
title('transform function');
subplot(2,2,4)
plot(x,freq);
title('transform function');

plot in all the subplots at the same time

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)
You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2
If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code

create subplot with loop

i have following question and please help me to solve following task:
i want to create subplot in matlab which depend on loop variable,for example as i know to crate plot menu 2X2,we are doing like this
subplot(2,2,1)
subplot(2,2,2)
subplot(2,2,3)
subplot(2,2,4)
but can i do linear form?like 1:100?or something like this ,more generally like this
n=100;
for i=1:n
subplot(1,n,i)
?
thanks very much
EDITED CODE
function [order]=find_order(y,fs);
order=0;
n=length(y);
n1=nextpow2(n);
ndft=2^n1;
for i=1:floor(n/2)
[Pxx,f]=pburg(y,i,ndft,fs);
subplot(ndft,ndft,i);
plot(f,Pxx);
title(['order',num2str(i),'i']);
order=i;
end
end
picture :
i can't understand what happens
1-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
figure,
for k=1:4
subplot(4,1,k)
plot(t((k-1)*1000+1:k*1000),y1((k-1)*1000+1:k*1000))
xlim([0 40])
end
Output
2-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
colors=['r' 'g' ; 'y' 'k'];
figure,
for k1=1:2
for k2=1:2
subplot(2,2,(k1-1)*2+k2)
plot(t,y1,colors(k1,k2));
end
end
Output
Hopefully these demos would guide to you something meaningful for your case.
Yes, it is:
n=5;
for i=1:n
subplot(1,n,i)
end
gives
for pat=1: N % main loop
% Define the sublot grid
s1=3; % subplot rows
s2=3; % subplot columns
% find the figure number
fig_num=floor(pat/(s1*s2))+1 % Figure number
% Find the subplot number
sub_fig=mod(pat,s1*s2) % subplot number
% correct for corners
if(sub_fig==0)
sub_fig=s1*s2;
fig_num=fig_num-1;
end
% plot something
figure(fig_num);
subplot(s1,s2,sub_fig) ;
plot(1,1) % plot something
end % of main loop

How can I put a background image in a movie in matlab

I need to put a background image in a movie created using MATLAB?
I have posted below an example code of how I am generating a movie in Matlab. I need to add a background image to this movie. Thanks for your help.
for i=1:128
for j=1:128
p(i,j,:)=randn(1,200);
end
end
[u,v,w]=size(p);
frm_r=1:128;
frm_c=1:128;
figure; j=1;
for t=1:w
surface(frm_c,frm_r,p(:,:,t),'EdgeColor','none');
pause(0.1)
colorbar;
F(j) = getframe;
j=j+1;
end
movie(F,1,50)
If you have access to MATLAB code that creates movie, you can place an axes with IMAGE or IMSHOW as a background and make axes on the top of it transparent.
Here is a simple example:
im = image; % create image object (default pic)
imax = get(im,'parent'); % get image axes handle
axis ij
set(imax,'position',[0 0 1 1]) % make it to fill the whole figure
ax = axes; % new axes
h = plot(ax,rand(100),'ro'); % initial plot
for i=1:20
set(h,'ydata',rand(100,1)); % change the data in a loop
pause(0.1)
end
You can get get better answer if you show your code how you create a movie.
EDIT:
I simplified the code in your answer a little. For example you don't need loops to fill up 3D array. And you don't need to repeat surface function in the loop. Just change zdata property.
Try this code (substitute yourimage.jpg to real file name):
p = randn(128,128,200);
[u,v,w]=size(p);
frm_r=1:128;
frm_c=1:128;
figure;
im = imshow('yourimage.jpg'); % create and display image object from a file
imax = get(im,'parent'); % get image axes handle
set(imax,'position',[0 0 1 1]) % make it to fill the whole figure
ax = axes('color','none'); % new axes
hsurf = surface(frm_c,frm_r,p(:,:,1),'EdgeColor','none','Parent',ax);
colorbar;
xlim([1 u])
ylim([1 v])
j=1;
for t=1:w
set(hsurf,'zdata',p(:,:,t))
F(j) = getframe;
j=j+1;
end
movie(F,1,50)