Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

overlap grid on an image with checkbox

Asked by Nuno

Nuno

on 27 Jul 2011
Accepted Answer by Walter Roberson

Walter Roberson

Hi,

I have this Gui that displays two images and now i want to overlap one of the images with a grid by using a checkbox so that the user can turn the grid on or off. i have already managed to create a grid over the image but now i need to have this feature on my GUI but i'm kind of stuck here.

any help?

This is the code i have for reading the image and overlap the mesh:

I = (imread([pathname, filename]));
%Normalizes light with Otsu's criteria
level = graythresh(I);
%RGB to binary
BW = im2bw(I,level);
handles.I = I;
axes(handles.axes1)
imshow(handles.I)
hold on
%mesh Grid
M = size(BW,1);
N = size(BW,2);
for k = 1:468:M
x = [1 N];
y = [k k];
plot(x,y,'LineWidth',2,'Color','w','LineStyle','-');
plot(x,y,'LineWidth',2,'Color','k','LineStyle',':');
end
for k = 1:340:N
x = [k k];
y = [1 M];
plot(x,y,'LineWidth',2,'Color','w','LineStyle','-');
plot(x,y,'LineWidth',2,'Color','k','LineStyle',':');
hold off

Thank you

Nuno

0 Comments

Nuno

Nuno

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson

Walter Roberson

on 27 Jul 2011
Accepted answer

To each plot() command add the parameter pair 'Tag', 'MyGrid'

Then

set(findobj('Tag','MyGrid'),'Visible','off')

would set it to be invisible

4 Comments

Walter Roberson

Walter Roberson

on 28 Jul 2011

Let hObject here represent the handle of the checkbox.

states = {'off', 'on'};
newstate = states{get(hObject, 'Value')+1};
set(findobj('Tag','MyGrid'),'Visible',newstate)

This will turn the grid on if the box is checked, and off if it is clear.

Nuno

Nuno

on 2 Aug 2011

Thank you that works fine!

Uzair

Uzair

on 15 May 2013
 I have similar problem. Below is my code...when ever i check the box, the image is replaced with blank screen with a vertical line in middle..Help me please

function show_grid_Callback(hObject, eventdata, handles) % hObject handle to show_grid (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) if (get(handles.show_grid, 'Value')) % rgb = imread('IK.jpg');

 imshow(handles.img,'Parent', handles.axes3);
hold on
M = size(handles.img,1);
N = size(handles.img,2);

a=str2double(get(handles.edit3, 'String')); b=str2double(get(handles.edit5, 'String'));

for k = 1:a:M x = [1 N]; y = [k k];

    plot(x,y,'Color','black','LineStyle','-','Parent', handles.axes3);
    set(findobj('Tag','MyGrid'),'Visible','on')
%   plot(x,y,'Color','k','LineStyle',':');
end

for k = 1:b:N x = [k k]; y = [1 M];

    plot(x,y,'Color','black','LineStyle','-','Parent', handles.axes3);
    set(findobj('Tag','MyGrid'),'Visible','on')
%     plot(x,y,'Color','k','LineStyle',':');
end

hold off else imshow(handles.img,'Parent', handles.axes3); end % Hint: get(hObject,'Value') returns toggle state of show_grid guidata(hObject, handles);

Walter Roberson

Walter Roberson

Contact us