How can I measure the distance between two pixels manually in a zoomed image with a scrollbar

1 view (last 30 days)
I wrote a code that asks the user to select different pairs of pixels of an image to measure the distances between them. The image is zoomed, thus it has scroll bars for the user to move around the image. The problem with my code is that when the user clicks on the scroll bar, the code considers it as a pixel from which distance has to me measured. How to I stop it from doing that?
Here is the code:
%Image with grid
Ilines=imshow(I);
%Image zoom
zoom(4);
%axis grid
axis on %off on
%Horizontal slider gui & constants
h=uicontrol('style','slider','units','normalized','callback',@sliderld,'Value',0,'position',[axes1.Position(1)+0.1,axes1.Position(2)-0.09,axes1.Position(3)-.18,0.035],'min',0,'max',100);
h.BackgroundColor=[0.5 0.5 0.5];
%Proportional displacement on image, x axis
ax=Isize(1,2);
bx=axes1.XLim(2)-axes1.XLim(1);
fx=(ax-bx)/100;
set(axes1,'XLim',[0 bx]);
%Vertical slider gui & constants
j=uicontrol('style','slider','units','normalized','callback',@sliderup,'Value',50,'position',[axes1.Position(1)+axes1.Position(3)-0.07,axes1.Position(2),0.02,axes1.Position(4)],'min',0,'max',100);
j.BackgroundColor=[0.5 0.5 0.5];
%Proportional displacement on image, y axis
ay=Isize(1,1);
by=axes1.YLim(2)-axes1.YLim(1);
fy=(ay-by)/100;
%Mouse function
set(fig1,'WindowButtonMotionFcn',@mousemotion);
%functions
function sliderld (varargin)%horizontal slider actions
set(axes1,'Xlim',[(get(h,'Value')*fx), (get(h,'Value')*fx+bx)]);
end
function sliderup (varargin) %vertical slider actions
set(axes1,'Ylim',[(ay-((get(j,'Value')*fy)+by)),(ay-(get(j,'Value')*fy))]);
end
function mousemotion (varargin)
motionposition = get(axes1, 'CurrentPoint');
valx=get(axes1,'Xlim');
valy=get(axes1,'Ylim');
if (motionposition(1,1)>valx(1,1)) && ... %Set image's area
(motionposition(1,1)<valx(1,2))...
&& (motionposition(1,2)<valy(1,2)) ...
&&(motionposition(1,2)>valy(1,1))
set(fig1, 'Pointer', 'crosshair');
else
set(fig1, 'Pointer', 'arrow'); %normal cursor
end
end
[dx,dy]=ginput(2);
Line1 = imline(gca,[dx(1,1) dx(2,1)],[dy(1,1) dy(2,1)]);
setColor(Line1,'b');
[dx2,dy2]=ginput(2);
Line2 = imline(gca,[dx2(1,1) dx2(2,1)],[dy2(1,1) dy2(2,1)]);
setColor(Line2,'b');
[dx3,dy3]=ginput(2);
Line3 = imline(gca,[dx3(1,1) dx3(2,1)],[dy3(1,1) dy3(2,1)]);
setColor(Line3,'b');
[dx4,dy4]=ginput(2);
Line4 = imline(gca,[dx4(1,1) dx4(2,1)],[dy4(1,1) dy4(2,1)]);
setColor(Line4,'b');
[dx5,dy5]=ginput(2);
Line5 = imline(gca,[dx5(1,1) dx5(2,1)],[dy5(1,1) dy5(2,1)]);
setColor(Line5,'b');
[dx6,dy6]=ginput(2);
Line6 = imline(gca,[dx6(1,1) dx6(2,1)],[dy6(1,1) dy6(2,1)]);
setColor(Line6,'b');
[dx7,dy7]=ginput(2);
Line7 = imline(gca,[dx7(1,1) dx7(2,1)],[dy7(1,1) dy7(2,1)]);
setColor(Line7,'b');
[dx8,dy8]=ginput(2);
Line8 = imline(gca,[dx8(1,1) dx8(2,1)],[dy8(1,1) dy8(2,1)]);
setColor(Line8,'b');
[dx9,dy9]=ginput(2);
Line9 = imline(gca,[dx9(1,1) dx9(2,1)],[dy9(1,1) dy9(2,1)]);
setColor(Line9,'b');
[dx10,dy10]=ginput(2);
Line10 = imline(gca,[dx10(1,1) dx10(2,1)],[dy10(1,1) dy10(2,1)]);
setColor(Line10,'b');
dist=[dy(2)-dy(1) dy2(2)-dy2(1) dy3(2)-dy3(1) dy4(2)-dy4(1) dy5(2)-dy5(1) dy6(2)-dy6(1) dy7(2)-dy7(1) dy8(2)-dy8(1) dy9(2)-dy9(1) dy10(2)-dy10(1)];
dist=dist/conv_factor;
meandist=mean(dist);
end

Answers (0)

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!