How to detect the area of the Ellipse using imellipse in MATLAB - matlab

I would be very grateful If anyone could help me to solve this problem.
Initially an Image to be loaded to the MATLAB. Image must contain an oval or circular shape object. I choose and draw the circle/ellipse there.
I have completed the above mentioned job. I have attached the MATLAB code and for figure just get full mooon image or egg for ellipse.
Now I want to calculate the area of that ellipse. Anybody there to sort me out from this situation.
Thank you
Here is the Code
function varargout = stack_overflow(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #stack_overflow_OpeningFcn, ...
'gui_OutputFcn', #stack_overflow_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
%%
function stack_overflow_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
%%
function varargout = stack_overflow_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
%%
function load_image_Callback(hObject, eventdata, handles)
axes(handles.view);
global im
[path, user_cancel] = imgetfile();
if user_cancel
msgbox(sprintf('Error !!! \n Please upload the image'),'Error','Error');
return
end
im = imread(path);
imshow(im);
%%
function draw_circle_Callback(hObject, eventdata, handles)
axes(handles.view);
global temp;
global fcn;
temp = imellipse(gca, []);
fcn = makeConstrainToRectFcn('imellipse',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(temp,fcn);
%%
function result_Callback(hObject, eventdata, handles)
%%
function result_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

You could use the createMask method of the imroi class to create a logical mask with the ellipse just drawn, and then sum all the pixels whose value is 1 (or 0...) in order to get the area. You can add something like this in your draw_circle_Callback:
Simple example which you can modify to adapt with your current code:
%// Create dummy image with logical false values.
Im = false(500,500);
imshow(Im);
hold on
%// Draw ellipse
hEllipse = imellipse
%// Create a logical mask
roi = createMask(hEllipse);
%// Sum the values equal to 1;
Area = sum(roi(:))

Related

Matlab-Arduino live plotting

I am trying to send data from my arduino to matlab and use the GUI. I want to read continously the data even when no button is pressed. In order to so, i have to use the fscanf function but i don't know where to put it. There is definitely a while loop that waits the events(such as a pushed button) in which this function should be placed. I am just a beginner so this might be a silly question for you.
Thank you in advance!
function varargout = UltraPlot(varargin)
global a;
global k;
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #UltraPlot_OpeningFcn, ...
'gui_OutputFcn', #UltraPlot_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
disp('Ultraplot');
function UltraPlot_OpeningFcn(hObject, eventdata, handles, varargin)
global a;
global k;
a = serial('COM3');
fopen(a);
handles.output = hObject;
guidata(hObject, handles);
disp('Opening');
function varargout = UltraPlot_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
global a;
global k;
disp('varargout');
function Start_Callback(hObject, eventdata, handles)
global a;
global k;
fprintf(a,'%d',1);
disp('Button pressed');
You have to set Matlab to wait for any data from Arduino in a while loop, check this sample code :
clear;clc;
S=serial('com18'); % Create an S Object
data=0;
set(S,'inputbuffersize',4096,'timeout',20); % Set serial communication parameter
fopen(S); % Open serial communcation
while (1)
if s.bytesavailable>0 % If data from Arduino is available
data=fscanf(S);
data = str2num(data);
% Do whatever you want with data here...
end
data=0;
end

Matlab GUI: Problems while using imfreehand()

I have applied Canny edge detection on am image and now want to crop a part of it for further procession. I have created 4 axes with the tags axes1, axes2, axes3 and axes4. I want to display the image first in axes1, then the edge detected again in axes1. Finally the cropped image is to be displayed in axes3 and the image after removal of holes in axes4.
I am including a sample code which recreates the problem:
function varargout = samp_GUI(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, 'gui_Singleton', gui_Singleton, 'gui_OpeningFcn', #samp_GUI_OpeningFcn, 'gui_OutputFcn', #samp_GUI_OutputFcn, 'gui_LayoutFcn', [] , 'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before samp_GUI is made visible.
function samp_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = samp_GUI_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in browse.
function browse_Callback(hObject, eventdata, handles)
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
handles.myImage = strcat(pathname, filename);
axes(handles.axes1);
imshow(handles.myImage)
im=imread(handles.myImage);
bw3=im2bw(im,0.3); %threshold value taken as 0.3
BW3=edge(bw3,'canny');
edge_im = BW3;
axes(handles.axes1);
imshow(BW3);
h=imfreehand(handles.axes1);
M=~h.createMask();
I(M) = 0;
axes(handles.axes2);
imshow(I);title('Cropped Image')
I = bwareaopen(I, 50);
axes(handles.axes3);
imshow(I),title('Removed Holes');
% save the updated handles object
guidata(hObject,handles);
When i execute the code in a .m file, it runs perfectly with different images opening in different windows but when the same code is run for the GUI, blank images are problems. I do not seem understand where the problem lies.
I think that you're suffering from some form of copy/paste issue. If you look at the line in your code where I(M) = 0 this doesn't really make sense (given your example) since I is not defined.
MATLAB will not throw any errors at this point though, because it's perfectly valid to create a matrix in this way; however, doing so actually produces a row vector rather than the image you're wanting.
>> M = logical(eye(100));
>> size(M)
ans =
100 100
>> clear I
>> I(M) = 0;
>> size(I)
ans =
1 10000
I imagine what you wanted to do was to apply the mask to your image (im) in which case you should get what you want. I have provided an example below (not sure what you're trying to display in axes1).
load mri
img = double(D(:,:,12));
im = img ./ max(img(:));
figure
handles.axes1 = subplot(2,2,1);
handles.axes2 = subplot(2,2,2);
handles.axes3 = subplot(2,2,3);
handles.axes4 = subplot(2,2,4);
bw3=im2bw(im,0.3); %threshold value taken as 0.3
BW3=edge(bw3,'canny');
edge_im = BW3;
axes(handles.axes1);
imshow(BW3);
h=imfreehand(handles.axes1);
M=~h.createMask();
% Assign im to I to ensure proper cropping.
I = im;
I(M) = 0;
axes(handles.axes3);
imshow(I);title('Cropped Image')
M_noholes = bwareaopen(M, 50);
I(M_noholes) = 0;
axes(handles.axes4);
imshow(I),title('Removed Holes');
If you provide more information it will be easier to help you figure out what you need.

Matlab - Defining the maximum value of a slider with a pushbutton_callback

I have a GUI. Suppose I want to display a certain number of images. I created a slider to display all the images in one window when you scroll down. The problem occurrs when you scroll down and exceed the number of images, so I need something to control the maximum value of the slider
One solution to this is:
I can create a slider in certain position and with certain max depending on images I selected .
My code right now (does not have the solution I spoke about) :
function varargout = panel(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #panel_OpeningFcn, ...
'gui_OutputFcn', #panel_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function panel_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = panel_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
global celldata3;
k = 1;
[filename pathname] = uigetfile({'*.*'},'File Selector','MultiSelect', 'on')
iscellstr(filename)
celldata1 = cellstr(pathname)
celldata2 = cellstr(filename)
celldata3 = strcat(celldata1,celldata2)
subplot(3,4,1),imshow(celldata3{1})
subplot(3,4,2),imshow(celldata3{2})
subplot(3,4,3),imshow(celldata3{3})
subplot(3,4,4),imshow(celldata3{4})
subplot(3,4,5),imshow(celldata3{5})
subplot(3,4,6),imshow(celldata3{6})
subplot(3,4,7),imshow(celldata3{7})
subplot(3,4,8),imshow(celldata3{8})
subplot(3,4,9),imshow(celldata3{9})
subplot(3,4,10),imshow(celldata3{10})
subplot(3,4,11),imshow(celldata3{11})
subplot(3,4,12),imshow(celldata3{12})
function slider1_Callback(hObject, eventdata, handles)
global celldata3;
a = get(hObject,'Value')
b = get(hObject,'Max')
[n max] = size(celldata3)
%set(handles.text2,'string',a)
i = (b-a)*4
if i+12 >= max
display ('STOP!');
end
subplot(3,4,1),imshow(celldata3{i+1})
subplot(3,4,2),imshow(celldata3{i+2})
subplot(3,4,3),imshow(celldata3{i+3})
subplot(3,4,4),imshow(celldata3{i+4})
subplot(3,4,5),imshow(celldata3{i+5})
subplot(3,4,6),imshow(celldata3{i+6})
subplot(3,4,7),imshow(celldata3{i+7})
subplot(3,4,8),imshow(celldata3{i+8})
subplot(3,4,9),imshow(celldata3{i+9})
subplot(3,4,10),imshow(celldata3{i+10})
subplot(3,4,11),imshow(celldata3{i+11})
subplot(3,4,12),imshow(celldata3{i+12})
display (i+12)
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function slider1_ButtonDownFcn(hObject, eventdata, handles)

how to send data from a function out GUI matlab to function GUI.m

How can I pass values (x_locate, y_locate) for a static text in my GUI?
Because the function is out functions generated by the GUI. I'm not achieving to configure the set() function.
I believe it is using handles, but I've tried everything and failed.
to simplify I rewrote the code:
![enter image description here][1]
The "Locations.fig" have: 1 axes, 1 pushbutton and 2 static text.
ctrl+C
function varargout = Locations(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #Locations_OpeningFcn, ...
'gui_OutputFcn', #Locations_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function Locations_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = Locations_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
cla(handles.axes1,'reset');
axes(handles.axes1);
image = imread('eight.tif');
im = imagesc(image);
set(im,'ButtonDownFcn', #clique);
function clique (gcbo,eventdata,handles)
pos = get(gca, 'currentpoint');
x_locate = round(pos(1))
y_locate = round(pos(3)) % until here working!!!
set(handles.text1, 'string', ['x loc:' num2str(x_locate)]); %don´t working
set(handles.text2, 'string', ['y loc:' num2str(y_locate)]); %don´t working
Only callbacks set through GUIDE (or some other custom means) receive the extra handles argument. Otherwise, you'll need to retrieve the handles structure manually within the clique function:
handles = guidata(<object>);
The question of what <object> is depends on your GUI setup. If im is a child of the GUI figure, then gcbo1 will do. If it's in a separate figure, then you'll need to get a handle to the GUI figure. Using findobj to either enumerate all figures or search for some specific property of your GUI figure is the straightforward way to do it.
For example, all Handle Graphics objects have a 'Tag' property that you are free to use, which would be helpful in this case. In GUIDE, set the Tag property on your GUI figure to 'my GUI', then you can retrieve the data from anywhere like so:
hfig = findobj('Tag','my GUI');
handles = guidata(hfig);
[1] Incidentally, giving variables the same name as built-in functions isn't a great idea
Set the ButtonDownFcn to be:
set(im, 'ButtonDownFcn', #(src,evt)clique(src,evt,handles))
The Notlikethat resolved the problem! Thanks!!!
so:
x_locate = round(pos(1));
y_locate = round(pos(3)); % until here working!!!
hfig1 = findobj('Tag','text1');
handles = guidata(hfig1);
hfig2 = findobj('tag','text2');
handles = guidata(hfig2);
set(handles.text1, 'string', ['x loc:' num2str(x_locate)]);
set(handles.text2, 'string', ['y loc:' num2str(y_locate)]);
finalized

Use multiple gui and get data from another gui function in matlab

I want to create a gui program with matlab but i want to use multiple gui. for example I have the main gui function and I want to get data from another gui with edit textbox. In the example below, I want to return the p variable to the main gui.
THE MAIN GUI:
function varargout = FoProgram(varargin)
gui_Singleton = 0;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #FoProgram_OpeningFcn, ...
'gui_OutputFcn', #FoProgram_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function FoProgram_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = FoProgram_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function labor_2_Callback(hObject, eventdata, handles)
function fel1_Callback(hObject, eventdata, handles)
cla reset;
clc;
clear all;
n = guzu() %Here I call the second Gui function with edit textbox
uiwait(gcf);
x=linspace(-3*pi,3*pi,1000);
y=sin(x);
plot(x,y,'k','LineWidth',4)
sz='ymcrgbkymcrgbkymcrgbkymcrgbk';
hold on
title('Sin(x) Taylor sora')
%n = str2num(N);
f=zeros(size(x));
for i=1:n
t=(-1)^(i-1)*x.^(2*i-1)/factorial(2*i-1);
f=f+t;
plot(x,f,sz(i),'LineWidth',2)
axis([-10 10 -10 10])
pause(1.5)
hold on
n=n+1;
end
function exit_Callback(hObject, eventdata, handles)
close
THE SECOND GUI
function varargout = guzu(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #guzu_OpeningFcn, ...
'gui_OutputFcn', #guzu_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function guzu_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = guzu_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
p = str2double(get(hObject,'String')) %I want to return this 'p' to the main gui
close
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
This kind of simple user input is easy to accomplish using the inputdlg function, without the need to create a separate GUI - here's an example
inputTitle = 'Input Required';
inputPrompt = 'Enter a value for ''p'':';
userInput = inputdlg(inputTitle, inputPrompt);
if isempty(userInput)
% User cancelled
return;
else
p = userInput{1}; % userInput is a cell array
% Do something with p
end
It may be that the example you provided is a very minimal version of your end-goal for the second UI, so this may not be appropriate (though note that the inputdlg function is capable of some more complex behaviour - see the documentation). If you wish to continue using your separate UI, then you have to make a couple of modifications
%%% Add this line to the end of OpeningFcn
uiwait;
%%% Modify OutputFcn to have the following code:
varargout{1} = str2double(get(handles.edit1, 'String'));
% The figure can be deleted now
% NOTE: You have to change this to the name of your figure,
% if it's not called figure1
delete(handles.figure1);
%%% Add the CloseRequestFcn callback and put this code in it
if isequal(get(hObject, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, us UIRESUME
uiresume(hObject);
else
% The GUI is no longer waiting, just close it
delete(hObject);
end
Most of the above is directly copied from here. You can also remove the call to uiwait from FoProgram.