Image displayed in GUI is tilted and not straight, MATLAB app designer - matlab

I have displayed an image in GUI and at the start it is displayed fine.
After some number of running it started to be displayed tilted:
And this is the code that I used:
function LoadImageButtonPushed(app, event)
global im;
[rawname, rawpath] = uigetfile({'*.Jpg'},'Select Image Data');
fullname = [rawpath rawname];
ax = app.UIAxes1;
im = imread(fullname);
[~, ~, c] = size(im);
if c ~= 1
im = rgb2gray(im);
end
im = imresize(im,[256 256]);
imshow(im, 'parent', ax);
end
Is there some code that I need to add in order for the image to be displayed straight and not tilted always?
Thank you.

Related

Save animation as gif in Matlab GUI

I am trying to save an animation as animated GIF.
My plot is similar to the given code below.
I created it with animated line too.
The problem is that:
When I defined my figure as f=figure or figure(1) it creates the .gif file properly.
However, instead of plotting my figure in separate screen using "figure" command, I have to plot in an axes on MATLAB GUI axes as the given figure.
I tried it with: f=(handles.axes_threeDOF);, But when I use this function, the gif file creates different part of the screen.
Could you help me to solve my problem?
numpoints = 500;
x = linspace(0,4*pi,numpoints);
y = square(x);
y2 = 3 +square(x+1);
f = figure
h = animatedline('Color','b','LineWidth',2);
h2 = animatedline('Color','r','LineWidth',2);
grid on;
axis([0,12,-3,+6])
for k = 1:numpoints
addpoints(h,x(k),y(k))
addpoints(h2,x(k),y2(k))
drawnow
% Capture the plot as an image
frame = getframe(f);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if k == 1
imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf);
else
imwrite(imind,cm,'test.gif','gif','WriteMode','append');
end
end
I want to create a gif of this animation:
But it creates as given below with this function " f=(handles.axes_threeDOF)"
I think I found the problem:
f = handles.axes_threeDOF gets the handle of the axes instead of getting the handle of the figure.
Since I don't know the name of your figure, I can't give a perfect solution.
You may try the following options:
1.Find the name of the figure, and use something like: f = handles.figure_threeDOF;
2. Use f = gcf();, assuming there is only one figure.
3. Use f = handles.axes_threeDOF.Parent; assuming the figure is the "Parent" of the axes.
Update:
After im = frame2im(frame);, you need to crop the relevant part of the image:
Something like: im = im(36:884, 928:1800, :);
There are more robust solution than using fixed indices, but it requires some knowledge about the internal structure of the figure.
Here is a code that reproduce the problem (instead of figure handle, f gets axes handle):
numpoints = 500;
x = linspace(0,4*pi,numpoints);
y = square(x);
y2 = 3 +square(x+1);
f = figure;
h = animatedline('Color','b','LineWidth',2);
h2 = animatedline('Color','r','LineWidth',2);
grid on;
axis([0,12,-3,+6])
for k = 1:numpoints
addpoints(h,x(k),y(k))
addpoints(h2,x(k),y2(k))
drawnow
%%% Test %%%
%The following code demonstrates the problem.
%f = gca(), returns a handle to the axes, instead of the figure.
%You should remove the following line for the code to work properly...
f = gca();
%%% Test %%%
% Capture the plot as an image
frame = getframe(f);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if k == 1
imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf);
else
imwrite(imind,cm,'test.gif','gif','WriteMode','append');
end
end
Result of correct code (without f = gca();):
Result of wrong code (with f = gca();) - getting axes handle instead of figure handle:

MATLAB: Segment Image

New to MATLAB and image processing.I need to know how to segment an image into foreground and background, then generate a binary image as output.
I need this as an output:
I have already tried to accomplish this with online tutorials and this is what i managed to get:
Its a good start but not exactly what i need.
My Code:
I = imread('AssignmentInput.jpg');
figure;
imshow(I);
title('Step-1: Load input image');
img_filtered = I;
for c = 1 : 3
img_filtered(:, :, c) = medfilt2(I(:, :, c), [3, 3]);
end
figure;
imshow(img_filtered);
title('Step-3:Noise Removal');
H = fspecial('gaussian'); % Create the filter kernel.
img_filtered = imfilter(img_filtered,H); % Blur the image.
Mask = im2bw(img_filtered, 0.9); % Now we are generating the binary mask.
img_filtered([Mask, Mask, Mask]) = 0; % Now we have the image.
figure;
imshow(img_filtered);
title('Step-5:Segmented Image');
For a better noise removal process and cleaner separation between foreground and background, you can also add morphological operations like:
se = strel('square',2);
I = imclose(I,se);
You can try out different variations of 'strel' class also. The image below is after imclose operation with a square of 2 pixel

Apply the same filter in a sequence of images while displaying it animated in the same figure in Matlab

I'm using MATLAB 2013a.
I have a folder with 1151 images that I want to animate in the same figure (window).
I want to find the imabsdiff of each image with only one image (maybe this one as a background image), and I would like it to display it animated and in the same figure, like if it was a video.
I found this code:
srcFiles = dir('C:\Users\coil-20-proc\*.jpeg'); % the folder in which ur images exists
for i = 1 : length(srcFiles)
filename = strcat('C:\Users\coil-20-proc\',srcFiles(i).name);
I = imread(filename);
figure, imshow(I);
end
which does read each image from my folder, but it creates a new figure(window) for each image, but I don't want 1151 windows!
You likely want to create a single imshow image object and then update the CData of this object each time through the loop.
directory = 'C:\Users\coil-20-proc';
srcFiles = dir(fullfile(directory, '*.jpeg');
srcFiles = cellfun(#(x)fullfile(directory, x), {srcFiles.name}, 'uni', 0);
hfig = figure();
him = imshow(NaN);
for k = 1:numel(srcFiles)
im = imread(srcFiles{k});
%// Perform your operation here
set(him, 'CData', im);
axis tight;
drawnow;
end

MATLAB movie2avi cannot made video

I am trying to test the function movie2avi using simple codes in R2014a as follows:
clear; close all;
figure;
Z = peaks;
surf(Z);
axis tight manual;
ax = gca;
ax.NextPlot = 'replaceChildren';
loops = 40;
F(loops) = struct('cdata',[],'colormap',[]);
for j = 1:loops
X = sin(j*pi/10)*Z;
surf(X,Z);
drawnow;
F(j) = getframe(gcf);
end
movie(F);
movie2avi(F, 'myPeaks.avi', 'compression', 'None');
It seems the movie(F) works well but the avi file created contains the toolbar and background instead of just showing the graph. Also the avi file just show stationary picture as follow:
Using quicktime to open it would produce same result:
https://www.dropbox.com/s/fd8vw7jvll5xkpw/b.png?dl=0
There is also a warning:
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure
Variables As Structures Displays Warning, for details.
Please help. Thanks.
Have you tried
F(j) = getframe(gca); %// Gets the current axis
This will capture the axis rather than the entire figure window.

How to plot a real time signal in MATLAB?

I'm building a smile detection system, and I need to plot the probability of smile (from video input) like the graphs on the right in this video.
How can I do this in MATLAB?
Notes
I'm currently displaying the video frames with OpenCV & IntraFace default code, which looks something like this :
cf = 0; % Current Frame.
% create a figure to draw upon
S.fh = figure('units','pixels',...
'position',[100 150 frame_w frame_h],...
'menubar','none',...
'name','Smile Detector',...
'numbertitle','off',...
'resize','off',...
'renderer','painters');
% create axes
S.ax = axes('units','pixels',...
'position',[1 1 frame_w frame_h],...
'drawmode','fast');
set(S.fh,'KeyPressFcn',#pb_kpf);
S.im_h = imshow(zeros(frame_h,frame_w,3,'uint8'));
hold on;
S.frame_h = text(50,frame_h-50,['Frame ' int2str(cf)] , 'fontsize', 15, 'Color' , 'c');
while true && ~stop_pressed
tic;
im = cap.read;
cf = cf + 1;
if isempty(im),
warning('EOF');
break ;
end
set(S.im_h,'cdata',im); % update frame
set(S.frame_h , 'string' ,['Frame ' int2str(cf)]);
do_something_with_frame(im);
if isempty(output.pred) % if lost/no face, delete all drawings
if drawed, delete_handlers(); end
else % face found
update_GUI();
end
drawnow;
end
close;
end
And I want to add a live / moving graph like in the video. The graph will display a single value (a probability) between 0 and 1. And it should be updated with every new frame, therefore the plot should "flow" as the video flows.
What Have I Tried
I tried creating a new figure just like S in the code. But I cannot plot into it. I am also ok with adding the live graph in the same figure (S.fh), preferrably under the scene.
Using linkdata and refreshdata will refresh a graph plot as you have new data.
%some pretend data
pX1 = rand;
pX2 = 1-pX1;
p = [pX1,pX2];
bar(p)
%link the data to the plot
linkdata on
for i=1:100
pX1 = rand;
pX2 = 1-pX1;
p = [pX1,pX2];
%refresh the linked data and draw
refreshdata
drawnow
end
http://www.mathworks.co.uk/help/matlab/ref/linkdata.html
Hope it helps...