How to add scale bar on microscopy images?

I have the following code for making movie out of my microscopy images. I would like to add a Scale Bar to it. How do I do that?
writerObj = VideoWriter([pName '20141209_Ch1_5_big.avi']);
open(writerObj)
for i=1:500
close
imcomp(CoreimgRed(:,:,i),correctNEWgreenImg(:,:,i),'y');
set(gcf,'Color',[1 1 1])
axis off
text(12,154,[num2str(i*0.2) 's'],'Color',[1 1 0.99],'FontSize',20)
frame = getframe;
writeVideo(writerObj,frame);
end
close(writerObj)

 Accepted Answer

What does imcomp() do? That function should return an image that you can then write some color in there, like black or white or red. Then pass to imshow() and then call drawnow and write the frame out to your video.
thisImage = imcomp(........
thisImage(row1:row2, column1:column2, :) = [255, 0, 0]; % Write red bar into image.
imshow(thisImage);
drawnow;
You have to figure out what rows and columns correspond to 1 micron. Attached is my spatial calibration demo if you want the user to do it by manually specifying the length.

8 Comments

Thank you for your help. imcomp will return an image (:,:,:)double as you see in the code I added, i can put text on the image that I open with imcomp.
I am looking for a easy way to add Scale bar as well, I know that each pixel is 92 nm,
what do you use the calibration for?
So 1 micron is 11 pixels long. So, pick row1 and column1. Then row2 = row1+barThickness and column2 = column1 + 10. You still have to pick row1 and column1 because this is the upper left corner of where you want to place the bar. It can't be selected automatically - you need to pick the location of the bar.
did not work :(, I am a beginner in the field and need more info to be able to get it to work,
Try this:
clc;
close all;
workspace; % Make sure the workspace panel with all the variables is showing.
format longg;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a standard MATLAB color demo image.
folder = fileparts(which('peppers.png')); % Determine where demo folder is (works with all versions).
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Put a micron bar up
barThickness = 3;
row1 = 20;
column1 = 40;
row2 = row1+barThickness;
column2 = column1 + 10;
rgbImage(row1:row2, column1:column2, :) = 255; % Write white bar into image.
imshow(rgbImage);
drawnow;
Peyman, did that work?
Thank you , yes it does. However before I see this, I used the following code;
load the image
hold on
p1 = [1.3,7];
p2 = [1.3,9];%add21.8
%# plot the points.
%# Note that depending on the definition of the points,
%# you may have to swap x and y
plot([p1(2),p2(2)],[p1(1),p2(1)],'Color','w','LineWidth',5);
text(7.5,1, '184 nm','Color',[1 1 0.99],'FontSize',20);
cheers Peyman
That will give a one pixel high line - might be hard to see. My code gives you control over the bar thickness.
of course your is fantastically done, I can see that you put effort in it for me to to be able to use this. I appreciate it :)

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!