I want to add grid on my image in matlab GUI

2 次查看(过去 30 天)
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 个评论
Uzair
Uzair 2013-5-16
I was trying something i found in another question...anyway...I commented it out later

请先登录,再进行评论。

回答(2 个)

David Sanchez
David Sanchez 2013-5-16
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.

David Sanchez
David Sanchez 2013-5-16
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 个评论
Image Analyst
Image Analyst 2013-5-16
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.
Uzair
Uzair 2013-5-16
Al tough code works perfectly fine when i run it seperately...but when i modify this code for my GUI ...it's give me wrong results

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Language Support 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by