Error when using imellipse to create mask - matlab

I try to create mask on a image, however I get the following error:
Undefined function 'createMask' for input arguments of type
'matlab.graphics.primitive.Image'.
I can't figure out what's wrong. Any help? Thanks in advance!
img = imread('pout.tif');
figure, h_im = imshow(img);
h = imellipse;
position = wait(h);
BW = createMask(position,h_im);

Solution
If I understand correctly, you are trying to get an ellipse input from the user and obtaining a mask of it.
There are two changes which needs to be done:
The wait function is not needed, since imellipse function waits until the user provide an ellipse.
First argument of createMask should be the ellipse h.
Updated code
img = imread('pout.tif');
figure, h_im = imshow(img);
h = imellipse;
BW = createMask(h,h_im);
Result

Related

Matlab template matching normxcorr2 error

I have an homework that i need to identify playing cards with matlab.
I decide to use TemplateMatching using normxcorr2 but i keep having this error:
Error in Proje_Deneme2 (line 9)
R = normxcorr2(T,I);
And i dont know why.
here is my code:
I = imread("2clubs.jpg");
[Ir, Ic]=size(I);
figure(1)
imshow(I);
T = imread("2clubsTemp.jpg");
figure(2)
imshow(T);
[Tr, Tc]=size(T);
R = normxcorr2(T,I);
R = imcrop(R,[Tc Tr Ic Ir]);
[r, c, v] = fin(R==(max(max(R))));
RGB = insertShape(I, "rectangle", [c r Tc Tr], "LineWidth", 3);
figure(3)
imshow(RGB);
I am new to matlab and image processing and this is my first time using normxcorr2 so if i miss something very dump please excuse me.
Thank you for your answers.
I think i solve the problem my images are colored so when i perform grayscale on them it works.But i still don't know why colored images wont work.

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:

A second matlab figure based on pixel coordinate data on the first figure

I have a imagesc image in which each pixel represent a data vector. The image itself is from a data cube squeezed into 2D matrix. I can use impixelinfo to navigate through the image and get pixel coordinates when inquiring the image. The code to execute this is below,
load data.mat; % data cube of size 512x256x12000
figure; imagesc(squeeze(mean(abs(data),3)))
axis equal; colormap jet;
impixelinfo
What I want to do is to be able to plot the underlying data vector (laying into the 3rd dimension) in a second figure using the pixel coordinates. This second figure should update automatically when I move the cursor in the image.
Any help is highly appreciated. Thank you in advance.
Thanks guys, I found a solution with ButtonDownFcn and posting it here for folks out there with a similar problem.
x = (-10:10); y = x; z = x;
[mx, my, mz] = ndgrid(x,y,z)
r = sqrt(mx.^2 + my.^2 + mz.^2);
figure;
imagesc(squeeze(r(:,:,1)),'ButtonDownFcn', {#test_func,r});
with the following function
function test_func(hObject, eventdata, r);
P = get(gca,'CurrentPoint');
X = round(P(1,1));Y = round(P(1,2));
figure;plot(squeeze(r(X,Y,:)));
end

plot3d of an image in SCILAB

I'm try to plot a gray image using plot3d in Scilab, here is my code:
clear;
clc;
direc='C:\Users\engine\Documents\EYE';
cd(direc)
stacksize('max');
fileName = '50.jpg';
rgb= ReadImage(fileName);
img = RGB2Gray(rgb);
Histogram = CreateHistogram(img);
figure(1); plot( 0:255,Histogram);
plot3d(1:length(img(:,1)),1:length(img(1,:)),img(:,:));
imshow(img);
When I run this code, I get this error message :
!--error 246
Function not defined for given argument type(s),
check arguments or define function %i_plot3d for overloading.
Any idea how can I solve this?
I found the error. I should convert the gray image to double values, the plot works fine then :
clear;
clc;
direc='C:\Users\engine\Documents\EYE';
cd(direc)
stacksize('max');
fileName = '50.jpg';
rgb= ReadImage(fileName);
img = RGB2Gray(rgb);
img2 = im2double(img);
img2 =img2*250;
plot3d(1:1024,1:1280,img2(:,:));

Saving output of Warp Command in MATLAB

I am using the command WARP in image processing in MATLAB.
[x,y,z] = cylinder;
I = imread('testpat1.png');
warp(x,y,z,I);
Above is the example code for using WARP given in MATLAB. But I am not able to save the output of this command. If I do imwrite, just 1 X 1 matrix is saved.
Can anyone help me this ?
Thanks in advance
You should be able to use the following line of code to get the handle to the resulting surface object:
h = warp(x, y, z, I);
You can then access properties of that surface using get(h, 'property')
A list of the available properties is here
For example if you want to get the X coordinates you would do: Xcoords = get(h, 'XData');
hope that helps!
This is based on the comments below.
fig = figure, warp(x, y, z, I);
print(fig, '-r80','-dtiff','image2.tif')