📄 pixval.m
字号:
end
end
end
set(displayBar, 'UserData', dbud);
%%%
%%% Sub-function - OverImage
%%%
function [imageHandle,imageType,img,x,y] = OverImage(figHandle)
% Return the index of which image we are over, and return a 0 if we
% aren't above an image.
images = findobj(figHandle, 'type', 'image');
if isempty(images)
imageHandle=0; imageType=''; img=[]; x=0; y=0;
return
end
% Make sure that the Image's Button Down & Up functions will queue
set(images, 'ButtonDownFcn', 'pixval(''ButtonDownOnImage'')', ...
'Interruptible', 'off', 'BusyAction', 'Queue');
axHandles = get(images, {'Parent'});
oldAxesUnits = get([axHandles{:}], {'Units'});
set([axHandles{:}], 'Units', 'pixels');
axPositions = get([axHandles{:}], {'Position'});
axCurPt = get([axHandles{:}], {'CurrentPoint'});
set([axHandles{:}], {'Units'}, oldAxesUnits);
% Loop over the axes, see if we are above any of them
imageHandle = 0;
for k=1:length(axHandles)
XLim = get(axHandles{k}, 'XLim');
YLim = get(axHandles{k}, 'YLim');
pt = axCurPt{k};
x = pt(1,1); y = pt(1,2);
if x>=XLim(1) & x<=XLim(2) & y>=YLim(1) & y<=YLim(2)
imageHandle = images(k);
break;
end
end
% Figure out image type
if imageHandle ~= 0
[img,flag] = getimage(imageHandle);
[rows,cols,colors] = size(img);
x_pix = axes2pix(cols, get(imageHandle,'XData'),pt(1,1));
y_pix = axes2pix(rows, get(imageHandle,'YData'),pt(1,2));
x = min(cols, max(1, round(x_pix)));
y = min(rows, max(1, round(y_pix)));
if x_pix<0.5 | y_pix<0.5 | x_pix>cols+.5 | y_pix>rows+.5
% We're above the axes but not the image
imageHandle=0; imageType=''; img=[]; x=0; y=0;
return
end
switch flag
case 1
imageType = 'indexed';
case {2,3} % Grayscale or binary
imageType = 'intensity';
case 4
imageType = 'rgb';
otherwise
error(['Invalid image, GETIMAGE returned flag = ' flag '.']);
end
else
imageHandle=0; imageType=''; img=[]; x=0; y=0;
end
%%%
%%% Sub-function - UpdatePixelValues
%%%
function UpdatePixelValues(imageHandle, imageType, displayBar,img,x,y)
% This is the motion callback for when we are displaying
% pixels. Either we are in automatic display mode and the
% mouse pointer is moving around or we are in normal mode
% and there has been a button-down but not yet a button up.
% I get the current point and update the string.
dbud = get(displayBar, 'UserData');
if strcmp(imageType,'indexed')
map=get(dbud.figureHandle, 'Colormap');
idx = img(y,x);
if isa(idx, 'uint8'),
idx = double(idx)+1;
end
idx = round(idx);
if idx<=size(map,1)
pixel = map(idx,:);
else
pixel = map(end,:);
end
else
pixel = double(img(y,x,:));
end
% figure out the new string
switch dbud.displayMode
case 'normal' % Just display intensity information
if strcmp(imageType,'rgb') | strcmp(imageType,'indexed')
if isa(img, 'uint8') & strcmp(imageType,'rgb')
pixval_str = sprintf(' %3d, %3d = %3d,%3d,%3d', ...
x,y,pixel(1),pixel(2),pixel(3));
else % all indexed images use double precision colormaps
pixval_str = sprintf(' %3d, %3d = %6.4f,%6.4f,%6.4f', ...
x,y,pixel(1),pixel(2),pixel(3));
end
else % intensity
if isa(img, 'uint8')
pixval_str = sprintf(' %3d, %3d = %3d',x,y,pixel(1));
else
pixval_str = sprintf(' %3d, %3d = %6.4f',x,y,pixel(1));
end
end
case 'distance'
delta_x = (x - dbud.x0);
delta_y = (y - dbud.y0);
dist = sqrt(delta_x^2 + delta_y^2);
set(dbud.line, 'XData', [dbud.x0 x], 'YData', [dbud.y0 y]);
if strcmp(imageType,'rgb') | strcmp(imageType,'indexed')
if isa(img, 'uint8') & strcmp(imageType,'rgb')
pixval_str = sprintf(' %3d, %3d = %3d,%3d,%3d dist = %3.3f', ...
x,y,pixel(1),pixel(2),pixel(3),dist);
else % all indexed images use double precision colormaps
pixval_str = sprintf(' %3d, %3d = %6.4f,%6.4f,%6.4f dist = %3.3f', ...
x,y,pixel(1),pixel(2),pixel(3),dist);
end
else % intensity
if isa(img, 'uint8')
pixval_str = sprintf(' %3d, %3d = %3d dist = %3.3f', ...
x,y,pixel(1),dist);
else
pixval_str = sprintf(' %3d, %3d = %6.4f dist = %3.3f', ...
x,y,pixel(1),dist);
end
end
end
set(displayBar, 'String', pixval_str, 'UserData', dbud);
%%%
%%% Sub-function - ButtonDownOnImage
%%%
function ButtonDownOnImage
imageHandle = gcbo;
figureHandle = get(get(imageHandle,'Parent'),'Parent');
displayBar = findobj(figureHandle, 'Tag', 'pixel value display bar');
dbud = get(displayBar, 'UserData');
axesHandle = get(imageHandle, 'Parent');
% Set the initial point (x0,y0)
pt = get(axesHandle, 'CurrentPoint');
[rows,cols,colors] = size(get(imageHandle, 'Cdata'));
x_pix = axes2pix(cols, get(imageHandle,'XData'),pt(1,1) );
y_pix = axes2pix(rows, get(imageHandle,'YData'),pt(1,2) );
dbud.x0 = min(cols, max(1, round(x_pix)));
dbud.y0 = min(rows, max(1, round(y_pix)));
dbud.line = line('Parent', axesHandle, ...
'erasemode', 'xor', ...
'color', [1 0 0], ...
'Xdata', [dbud.x0 dbud.x0], ...
'Ydata', [dbud.y0 dbud.y0]);
dbud.displayMode = 'distance';
dbud.buttonDownImage = imageHandle;
set(displayBar, 'UserData', dbud);
set(dbud.figureHandle, 'WindowButtonUpFcn', 'pixval(''BackToNormalPixvalDisplay'')');
PixvalMotionFcn(displayBar);
%%%
%%% Sub-function - BackToNormalPixvalDisplay
%%%
function BackToNormalPixvalDisplay
displayBar = findobj(gcbo, 'Tag', 'pixel value display bar');
dbud = get(displayBar, 'UserData');
imageHandle = dbud.buttonDownImage;
delete(dbud.line);
dbud.line = [];
dbud.x0 = []; dbud.y0 = [];
set(dbud.figureHandle, 'WindowButtonUpFcn', '');
dbud.displayMode = 'normal';
dbud.buttonDownImage = 0;
set(displayBar, 'UserData', dbud);
PixvalMotionFcn(displayBar);
%%%
%%% Sub-function - MoveDisplayBar
%%%
function MoveDisplayBar
displayBar = gcbo;
dbud = get(displayBar, 'UserData');
oldWindowMotionFcn = get(dbud.figureHandle, 'WindowButtonMotionFcn');
set(dbud.figureHandle, 'WindowButtonMotionFcn', '');
oldPointer = get(dbud.figureHandle, 'Pointer');
oldPointerShapeCData = get(dbud.figureHandle, 'PointerShapeCData');
oldPointerShapeHotSpot = get(dbud.figureHandle, 'PointerShapeHotSpot');
setptr(dbud.figureHandle,'closedhand');
txtpos = get(displayBar, 'Position');
frmPos = get(dbud.DisplayFrame, 'Position');
position = txtpos + [0 0 frmPos(3) 0];
position2 = dragrect(position);
dx = position2(1)-position(1);
dy = position2(2)-position(2);
txtpos = txtpos + [dx dy 0 0];
set(displayBar, 'Position', txtpos);
frmPos = frmPos + [dx dy 0 0];
set(dbud.DisplayFrame, 'Position', frmPos);
btnPos = get(dbud.CloseButton, 'Position');
btnPos = btnPos + [dx dy 0 0];
set(dbud.CloseButton, 'Position', btnPos);
set(dbud.figureHandle, 'Pointer', oldPointer, ...
'PointerShapeCData', oldPointerShapeCData, ...
'PointerShapeHotSpot', oldPointerShapeHotSpot)
set(dbud.figureHandle, 'WindowButtonMotionFcn', oldWindowMotionFcn);
%%%
%%% Sub-function - DeleteDisplayBar
%%%
function DeleteDisplayBar(displayBar)
% Take apart the pixel value display - get rid of the text
% uicontrol and clean up the image objects (remove their
% UserData's and get rid of their ButtonDown & Delete Fcn's)
if nargin<1
displayBar = findobj(gcbf, 'Tag', 'pixel value display bar');
end
if ~isempty(displayBar) % if it's empty, there's no work to do.
dbud = get(displayBar, 'UserData');
set(displayBar, 'DeleteFcn', ''); % Make sure there's no recursion
if ishandle(displayBar)
delete(displayBar);
end
if ishandle(dbud.CloseButton)
delete(dbud.CloseButton);
end
if ishandle(dbud.DisplayFrame)
delete(dbud.DisplayFrame);
end
% We no longer restore the interactive state of the figure.
% uirestore(dbud.oldFigureUIState);
uisuspend(dbud.figureHandle);
end
%%%
%%% Sub-function - CreatePointer
%%%
function [pointerShape, pointerHotSpot] = CreatePointer
pointerHotSpot = [8 8];
pointerShape = [ ...
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
1 1 1 1 1 1 2 NaN 2 1 1 1 1 1 1 1
2 2 2 2 2 2 2 NaN 2 2 2 2 2 2 2 2
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 2 2 2 2 2 2 NaN 2 2 2 2 2 2 2 2
1 1 1 1 1 1 2 NaN 2 1 1 1 1 1 1 1
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 1 2 NaN 2 1 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -