insert text into image without computer vision system toolbox - Matlab - matlab

In the Matlab central (http://www.mathworks.com/matlabcentral/answers/26033-how-to-insert-text-into-image) I found this code:
Text = sprintf('Create text inserter object \nwith same property values');
H = vision.TextInserter(Text);
H.Color = [1.0 1.0 0];
H.FontSize = 20;
H.Location = [25 25];
I = im2double((imread('football.jpg')));
InsertedImage = step(H, I);
imshow(InsertedImage);
How can I do the same without using the computer vision system toolbox?
Thanks

To follow up on my comment:
A = imread('football.jpg');
YourText = sprintf('Create text inserter object \nwith same property values');
figure;
imshow(A);
hText = text(25,25,YourText,'Color',[1 1 0],'FontSize',20);
Giving the following:
Look at the doc page to see all the options available to modify/customize your text. Notice that you don't have to use sprintf to generate the string, however the newline character (\n) must be used inside sprintf in order to work.
EDIT In response to your comment below, if you need to save the image with text embedded in it you can use getframe to get the content of the figure and then imwrite to save it:
hFrame = getframe(gca) %// Get content of figure
imwrite(hFrame.cdata,'MyImage.tif','tif') %// Save the actual data, contained in the cdata property of hFrame
EDIT #2 As alternatives to using getframe, look here as there are 2 nice ideas proposed, i.e. using saveas or avi files.

The answer above (Benoit_11) requires that you display the image first, write the text, then save the image. This becomes very slow if you are processing the frames of a video (not a single image, or a few of them). To get a much faster processing time, you have to write directly to the image matrix. One alternative I can think of (but that is not very elegant) is to create (or download) small templates for the alphabet characters (e.g. 20x20), then overwrite the image matrix in the required region with those templates. For example, for an RGB "true color" image, we'll have something like this:
im(y:y+19,x:x+19,:)=template;

Related

How do I save images with face detection bounding box using the Batch Image Processor app in Matlab

I'm a newbie to Matlab and I'm trying to perform face detection on a batch of images. I'm using this simple face detection code which works with the Image Processing Toolbox and the Computer Vision Toolbox. I'm using the Batch Image Processor App and wrote my code as a function that I apply to a batch of images. What I need as an outcome are the four values of each bounding box (this works), and I want to save each image with the bounding box drawn, in order to check if the face is detected correctly (which doesn't work). This is the function:
function results = facedetection(im)
FDetect = vision.CascadeObjectDetector('FrontalFaceLBP','MergeThreshold', 5);
BBface = step(FDetect,im);
figure,
imbb = imshow(im); hold on
for i = 1:size(BBface,1)
rectangle('Position',BBface(i,:),'LineWidth',2,'LineStyle','-
','EdgeColor','r');
end
hold off;
imwrite(imbb,'test.jpg'); %this is the line that doesn't work
results.face=BBface; %this gives me the values of each bounding box
end
When I apply this function it provides me with the values of the bounding box, and in the imshow it shows the image with the bounding box around the face. However, the imwrite function doesn't work and gives the following error:
Error using imwrite (line 420) Expected DATA to be one of these types:
numeric, logical
Instead its type was matlab.graphics.primitive.Image.
Error in facedetection (line 13) imwrite(imbb,'test.jpg');
Does anyone know how to solve this issue? Is it possible to save all images with the bounding box using the Batch Image Processor app and if so, how? Is it also possible to save the images without using imshow? I don't have to see the results directly, as long as I can save them.
I'm sorry if this question is too vague, but I hope someone can help me a bit further.
EDIT: I found out that the Batch Image Processor can be used to save the images, as long as the image is put into a results. string in the function (see documentation), but I don't know how to do this. I changed my code as followed:
function results = facedetection(im)
FDetect = vision.CascadeObjectDetector('FrontalFaceLBP','MergeThreshold', 5);
BBface = step(FDetect,im);
figure,
imbb = imshow(im); hold on
for i = 1:size(BBface,1)
rectangle('Position',BBface(i,:),'LineWidth',2,'LineStyle','-
','EdgeColor','r');
end
hold off;
BBimage = imfuse (im, BBface); %this doesn't work
results.BBValues=BBface;
results.BBimage=BBimage;
end
This code returns a complete green hued image (?!). So probably something goes wrong in the imfuse part. How do I put the im and the drawn rectangle together in the BBimage?
I solved the problem using the insertShape function. The complete function becomes as follows:
function results = facedetection(im)
FDetect = vision.CascadeObjectDetector('FrontalFaceLBP','MergeThreshold', 3);
BBvalues = step(FDetect,im);
hold on
for i = 1:size(BBvalues,1)
BBimage = insertShape (im,'rectangle',BBvalues(i,:),'Color','yellow');
end
hold off;
results.Values3=BBvalues;
results.Image3=BBimage;
end
When this function is applied to a batch of images in the Batch Image Processor app, the 'export results of all processed images to files'-button allows you to save the images including the bounding box rectangle. It saves each image with its original filename and an additional '_Image3'.
I'm using this code now to test the most effective threshold, which is why I named them Values3 and Image3 refering to the threshold 3. It works fine!
This means that imbb that you are trying to write out to a file is not the proper type to write out to a file.
Can you try something like this after your for loop and see what happens?
frame = getframe(figure);
image = frame2im(frame);
imwrite(image, 'test.jpg');
Eventually, you want to move your imwrite inside your for loop so you can batch process it.
You would declare a file name before the for loop like :
basename = 'MyFileName-';
Then inside the for loop, you would do something like
filename = [basename, num2str(i)];
imwrite(image, filename);

Save image from imshow

I want to save the image to a file after doing imshow(im,[]); to display it later in GUI. I am trying the following code, but it doesn't work.
New= imshow(uint8(MHI{t}),[]);
imwrite(New,'TMHI.jpg','jpg')
Any help will be appreciated. Thank you.
The imshow function is only used to show the image in MATLAB. If you want to save it, you don't need the imshow at all. And: the value (New) returned by imshow() is the handle to the figure. You need that handle if you want to modify how the figure is shown on the screen.
To write the image to the disk, you only need the imwrite function, which has the syntax:
imwrite(A,filename)
where A is the image array.
If the file name ends with .jpg, then MATLAB will create a JPEG image by default, so you don't need to specify that. (But of course, you still can.)
But before saving: you have a problem with the normalization of the image. MATLAB assumes that a double image is scaled to [0,1] and that a uint8 image is scaled to [0,255]. With imshow(im,[]) you override these defaults and make MATLAB calculate new values. You will experience the same problem when saving. The solution is to normalize the image properly. This can be done using the im2uint8 function, which scales the input to a maximum value of 255, and converts it to uint8. Note that you'll have to remove the minimal value manually, if that is needed:
newImage = im2uint8(MHI{t} - min(MHI{t}(:)));
imwrite(newImage,'TMHI.jpg')
In case you really need to save the contents of the displayed figure in matlab (sometimes also useful when you use imagesc for display as it has some smart logic for properly scaling your value ranges) you might be interested in the savefig and saveas which lets you save the contents of a figure. Its also possible to save graphs or figures with subfigures like that.
In that case, you would use something like:
F = imshow(uint8(MHI{t}),[]);
saveas('MHI.png');
In case you really just need to save the image stored in MHI{t}, hbaderts 's answer is the way to go...
Just use my NormalizeImage function and save the image normaly:
img = NormalizeImage(imgDouble);
imwrite(img ,'MyImage.png');
My NormalizeImage function:
function img8bpp = NormalizeImage(imgDouble)
minImgDouble = min(imgDouble(:));
factor = (255-0)/(max(imgDouble(:)) - minImgDouble);
%img8bppB = im2uint8(imgDouble-minImgDouble);
img8bpp = uint8((imgDouble-minImgDouble).*factor);
%im2uint8 does not work, duno y
%imgDif = img8bppB - img8bpp;
end

Image registration in imageJ or matlab

I am looking for advice on how to automatically register image stacks acquired at different magnifications. Specially we need to align a small z-stack (~100um) taken of several brain cells in the live brain to a large z stack(~2mm) taken of the fixed brain. We want to be able to find back the cells that were previously imaged and take high resolution images of the staining to identify pre-synaptic inputs. Both the difference in magnification and rotation need to be taken into account as well as possible shrinkage or swelling. We would like an advice on the best way to do this using ImageJ or matlab.
With ImageJ you might want to download the plugin StackReg:
http://bigwww.epfl.ch/thevenaz/stackreg/
or this nice macro code freely available:
http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0053942
With matlab you are looking for imregister, imregtform and/or imwarp. Also look at the optimizer, which is used to define the type of transformation you wish to apply, for example translation, rigid body, etc. Examples on the matlab website are quite helpful. Please ask for more details if it's not clear.
EDIT:
Here is a simple code to register images stored in a sequence in a cell array with Matlab and the functions I mentionned:
clear
clc
dialog_title = 'Select the directory containing the images to be processed'; % select images
folder_name = uigetdir('',dialog_title);
addpath(folder_name);
% select current folder
cd(folder_name);
ImagesToRead = dir('*.tif');
%preallocation
ImageCell = cell(1,length(ImagesToRead));
for i=1 : length(ImagesToRead)
ImageCell{i} = imread(ImagesToRead(i).name);
end
optimizer = registration.optimizer.RegularStepGradientDescent; % here you can modify the default properties of the optimizer to suit your need/to adjust the parameters of registration.
[optimizer, metric] = imregconfig('monomodal'); % for optical microscopy you need the 'monomodal' configuration.
RegisteredCell = cell(1,length(ImagesToRead));
for p = 2:length(ImagesToRead)
moving = ImageCell{p}; % the image you want to register
fixed = ImageCell{p-1}; % the image you are registering with
movingONE = rgb2gray(moving(:,:,:)); % imregtform needs grayscale images
fixedONE = rgb2gray(fixed(:,:,:));
tform = imregtform(movingONE,fixedONE,'translation',optimizer,metric,'DisplayOptimization',true,'PyramidLevels',5);
tform = affine2d(tform.T);
RegisteredCell{p} = imwarp(moving,tform,'OutputView',imref2d(size(fixedONE))); %
end
Now all your images are stored in the cell array 'RegisteredCell' and you can access each of them individually using RegisteredCell{YouImage} for example. Hope that helps!

Image processing Error in MATLAB

I was given a defined set of images (.png), I am supposed to detect each images Edges, then apply some image processing, but I have a problem.
First I an image array as follows :
imgArray = {'image_1.png','image_2.png','image_3.png'}
Then applied edging (sobel), using the MATLAB built in function edge so :
for i = 1:3
image=imread(imgArray{i});
image = edge(image,'sobel');
imgArray{i} = image;
end
based on that prvious code and my understanding, that the imageArray, now contains all 3 edged images.
Later on, I need to use the Edged images using that command image=imread(imgArray{i}); in a different place in the code, but it gives me an Error, I dont understand why does that happen ??
EDIT:
Here's the error I'm getting:
Error in ==> ImageCompare at 43 image=imread(imgArray{i});
imgArray = {'image_1.png','image_2.png','image_3.png'};
imgArrayEdged = strrep(imgArray, '.png', '_edged.png');
for i = 1 : length(imgArray)
image = imread(imgArray{i});
image = edge(image,'sobel');
imwrite(image, imgArrayEdged{i});
end
% later...
for i = 1 : length(imgArray)
if (your_condition)
image = imread(imgArray{i});
else
image = imread(imgArrayEdged{i});
end
end
Your imgArray contains file names as strings. In your loop, you are reading the image files and replacing each string in the cell array with image data.
If you absolutely require the file name strings later, you must create a second variable to hold the image data itself. If you only require the original images, just don't use imread later in the code!
Having read in an image file with imread once, there is no reason to waste time by reading the files again. It seems like you aren't quite aware of what state your data is in as it moves through your code. I suggest you use MATLAB's excellent debugger to step through and examine the type and content of the variables - you will quickly see where imread, which needs a file name, is inappropriate.

Saving contents of figure to a matrix in Matlab ( whithout using getframe, nor with saving to file)

The function getframe captures whatever is visible on the screen. However in my application I want the figure to be invisible when storing its content in a matrix. So what the getframe does is that, for a short period, it makes the figure visible and captures its contents and then sets the 'visibile' property back to what it was before screen capture.
I do not want that flash happening on the screen. As well, saving in file and reading it back reduces speed. There has got to be a way to get around this.
hFig=figure('Visible','off'')
text ('String','ABC','fontsize',300)
imageData = getframe(hFig);
img = imageData.cdata; % img is what I am interested in
The only way I know to do this is to print the figure to a temporary file. For example:
%Create a figure
hFig=figure('Visible','off')
text ('String','ABC','fontsize',300)
%Save the figure to a tiff file using a salted name
tmpName = sprintf('Temp_Figure_%04d.tiff', floor(rand*1000));
print(['-f' num2str(hFig)], '-dtiff', tmpName)
%Read the data
img = imread(tmpName);
%Delete the temporary figure
delete(tmpName);
Not the prettiest thing, but it seems to work.