I want to add grid on my image in matlab GUI
2 views (last 30 days)
Show older comments
I have a 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);
2 Comments
Answers (2)
David Sanchez
on 16 May 2013
Image Analyst is right. I simplified the code to this:
I = imread('cameraman.tif');
imshow(I)
hold on
M = size(I,1);
N = size(I,2);
a=5;
b=10;
for k = 1:a:M
x = [1 N];
y = [k k];
plot(x,y,'Color','black','LineStyle','-');
set(findobj('Tag','MyGrid'),'Visible','on')
end
for k = 1:b:N
x = [k k];
y = [1 M];
plot(x,y,'Color','black','LineStyle','-');
set(findobj('Tag','MyGrid'),'Visible','on')
end
hold off
And the desired grid shows up. The problem might be in the figure handle.
0 Comments
David Sanchez
on 16 May 2013
You get a vertical line because you are plotting a vertical line. The values of both elements of your x array are the same
x=[k k];
which will end up in a vertical line whatever the values of y are.
... ... 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
3 Comments
Image Analyst
on 16 May 2013
But he didn't always have the same x. k went from 1 to N in steps of b and he had hold on so it should have drawn and retained lots of vertical lines, not just one. Just off the top of my head it looks like he should have had vertical lines at 1, (1+b), (1+2*b), .... etc.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!