Looking at data from image using Datacursormode and ROI (drawrectangle)

14 views (last 30 days)
Hi there,
I am trying to make use of datacursormode to select points/regions I am interested in and displaying the x, y, and rgb values at that point. This is the code I have used and the function to generate the position at the location I have selected. However, I can't seem to be able to generate the RGB values when I umcomment the line CData and it shows "unable to update datatip using custom update function".
figure
imagesc; axis image;
datacursormode on
dcm = datacursormode(gcf);
set(dcm,'UpdateFcn',@myupdatefcn_example)
function txt = myupdatefcn_example(trash,event)
pos = get(event,'Position');
I = get(event, 'DataIndex');
% CData = get(event,'CData');
txt = {[num2str(pos(1))],...
[num2str(pos(2))],...
[num2str(I)]};
end
Next, I would also like to draw a rectangle and extract the mean RGB values within that rectangle using the datacursormode as well so that it is interactive. I found this drawrectangle function that allows me to draw the rectangle I want on the image, and also generates the position of the box. However, I am not quite sure how to link this up with the datacursormode.
figure
imagesc; axis image;
roi = drawrectangle('Color',[1 0 0]);
addlistener(roi,'MovingROI',@allevents);
function allevents(src,evt)
evname = evt.EventName;
switch(evname)
case{'MovingROI'}
disp(['ROI moving previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moving current position: ' mat2str(round(evt.CurrentPosition))]);
case{'ROIMoved'}
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
end
end
Could anyone please advise? Thank you for your time.
Best Regards,
Wayne

Accepted Answer

Kiran Felix Robert
Kiran Felix Robert on 14 Aug 2020
Hi Wayne,
It is my understanding that you wish to know why
CData = get(event,'CData');
affects the operation of your custom update function. This is because the CData field does not belong to the events (info) object of UpdateFcn . The image pixel values in the UpdateFcn can be accessed by passing arguments to the callback function as shown below,
f = figure;
imagesc(I);
handles.I = I;
dcm = datacursormode;
dcm.Enable = 'on';
dcm.UpdateFcn = {@coordinates,handles};
function txt = coordinates(~,info,handles)
x = info.Position(1);
y = info.Position(2);
R = handles.I(x,y,1);
B = handles.I(x,y,2);
G = handles.I(x,y,3);
txt = ['(' num2str(x) ', ' num2str(y) ')'':(' num2str(R) ', ' num2str(G) ', ' num2str(B) ')'];
end
The datacursormode is only used to put up datatip in specific points in a figure or a plot. To access values from the ROI, the listener function can be used. Your ROI Listener can be modified as shown below to get the mean RGB values,
function allevents(src,evt)
evname = evt.EventName;
him = findobj(src.Parent,'Type','image');
switch(evname)
case{'MovingROI'}
mask = src.createMask(him(1).CData);
[r,c] = find(mask);
P = impixel(him.CData,r,c);
RGBmean = mean(P,1)
end
end
The mean value of RGB is printed to command line for every ROI update.
Hope this Helps.
Kiran Felix Robert

More Answers (0)

Community Treasure Hunt

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

Start Hunting!