This is a fairly entry-level question but I could not find the answer to it here on SO or on Mathworks help.
I want to add a color bar to an image I am loading and then save the image along with the created color bar.
The result I am looking for is like figure 1 on this page except that there is an image instead of the plot and the color bar shows the range of intensities in the image.
The page made me think that running
colorbar(<trarget matrix>);
Would do the trick but that throws an error when I try to apply it to an image.
So my question is, how do I make a color bar for my image and once made what would be a good way to concatenate the two for saving.
I am loading the image using the following snippet
IMGpath = 'barbaraSmall.png';
im = imread(IMGpath, 'png');
%
%colorbar(im); >>Throws an error
imshow(im);
imwrite(im, 'barabara_withMAP.png', 'png');
The syntax that you're using is colorbar(target). According to the documentation,
target is:
"Target for the colorbar, specified as an Axes object, a PolarAxes object, or a graphics object that has a ColorbarVisible property".
You're inputting an image matrix (im) as target which is none of the above mentioned objects. You can simply just enter:
colorbar;
or if you want to give an axes' handle then:
colorbar(gca);
You can save the result using saveas.
saveas(gca, 'barabara_withMAP.png')
Related
I have two time-lapse images of a membrane surface. Both images were supposed to show the same region. But while adjusting focus, captured field might have drifted a bit. I used two routes to visualize the amount of drift - MATLAB v 2021a and ImageJ. With MATLAB, first I tried superimposing two images using imshowpair. Original grayscale images are fix
and mov. imshowpair(mov,fix) yields imshowpair_comp. It clearly shows possible drift. Then I tried using imfuse function as follows:
RF = imref2d(size(fix));
RM = imref2d(size(mov));
RM.XWorldLimits = RF.XWorldLimits;
RM.YWorldLimits = RF.YWorldLimits;
comp = imfuse(fix,RF,mov,RM,'falsecolor','Scaling','joint','ColorChannels',[1 2 0]);
It gave the composite image imfuse_comp. Next, I carried out image registration and I got imregcorr_comp.
tForm = imregcorr(mov,fix,"similarity");
movTransform = imwarp(mov,tForm,"OutputView",RF);
imshowpair(movTransform,fix)
This image shows properly aligned composite image. I tried doing the same using ImageJ.
Open fix.tiff in ImageJ. Image->Colors->Channels Tool->Composite->Red.
Open mov.tiff in ImageJ. Image->Colors->Channels Tool->Composite->Green.
Image->Colors->Merge Channels
This gave the composite image imagej_comp. This composite image obtained from ImageJ clearly shows that there was no misalignment in the two images to begin with!
I am unable to figure out where I went wrong. Now I am really confused between two routes - and which route to believe in. Can someone please help me out?
Thanks!
I am currently working on a program involving shop routing, involving an image map obtained from google maps in the background.
When I try to use the data cursor (to show some irrelevant information), I need to click exactly the right pixel to get the data assigned to the plotted points, else I get a data tip error. Is there any way to disable data cursor mode for the background image while enabling it for the scattered points?
My figure looks like this:
edit: In case this helps, the background is defined as an image with following properties:
CData: [1280×1280×3 double]
CDataMapping: 'direct'
You should be able to set the HitTest property of your background image to off - that way the datatip will only show when you click on your data of interest.
I want to programmatically 'Print to figure' a Simulink scope and save the resulting figure to a folder.
Consider following Simulink model and select the scope:
I run following code (inspired by this question):
scopeName=get_param(gcb,'Name');
hs=findall(0,'Name',scopeName);
hf=figure(1);
hp=findobj(hs.UserData.Parent,'Tag','VisualizationPanel');
copyobj(hp,hf)
filename='test.tiff';
print('-dtiff',filename);
Although both the scope and the figure have a black background
the saved file has a white background
Is there something wrong with the print command or with something else?
By default, MATLAB inverts the background colors when printing to a figure. To get around this, you can set the InvertHardCopy to 'off'
set(gcf, 'InvertHardCopy', 'off')
Doing this (as opposed to using getframe) results in a much higher resolution image as getframe simply saves the figure at screen resolution (72dpi).
Another option is to use export_fig from the MATLAB file exchange to save the figure which will more reliably reproduce the image that is on your screen.
You can get the same view as you see:
img = getframe(gcf);
imwrite(img.cdata,'test.tiff');
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
I have RGB museum JPG Images. most of them have image footnotes on one or more sides, and I'd like to remove them. I do that manually using paint software. now I applied the following matlab code to remove the image footnotes automatically. I get a good result for some images but for others it not remove any border. Please, can any one help me by update this code to apply it for all images?
'rgbIm = im2double(imread('A3.JPG'));
hsv=rgb2hsv(rgbIm);
m = hsv(:,:,2);
foreground = m > 0.06; % value of background
foreground = bwareaopen(foreground, 1000); % or whatever.
labeledImage = bwlabel(foreground);
measurements = regionprops(labeledImage, 'BoundingBox');
ww = measurements.BoundingBox;
croppedImage = imcrop(rgbImage, ww);'
In order to remove the boundaries you could use "imclearborder", where it checks for labelled components at boundaries and clears them. Caution! if the ROI touches the boundary, it may remove. To avoid such instance you can use "imerode" with desired "strel" -( a line or disc) before clearing the borders. The accuracy or generalizing the method to work for all images depends entirely on "threshold" which separates the foreground and background.
More generic method could be - try to extract the properties of footnotes. For instance, If they are just some texts, you can easily remove them by using a edge detection and morphology opening with line structuring element along the cols. (basic property for text detection)
Hope it helps.
I could give you a clear idea or method if you upload the image.