Matlab Imread resizes tif file - matlab

so I'm using the imread function in matlab and when I save the TIFF file and open it in photoshop, it has a white border and I can't understand why. I want to maintain its resolution as a 512 by 512 image. Any ideas why? And how I can fix that?
Here's a sample code:
B = imread('W_noise1.tif');
for n = 1:5,
B = medfilt2(B);
end
B = filter2(fspecial('average',3),B)/255;
imshow(B)

Are you sure it's an issue with imread? I'd be surprised if it is.
See this link about medfilt2 where it explains that "medfilt2 pads the image with 0s on the edges, so the median values for the points within [m n]/2 of the edges might appear distorted."
EDIT: I tried to replicate your problem. This is an issue with print where it puts a white frame around the image after you save it. This functionality, print is made for printing plots. If you want to save the image, you should use imwrite.

Related

How to generate proper labelled image from MATLAB (Image Labeler) for image segmentation

I get black image whenever exporting labels to file in the Image Labeler App in MATLAB R2019a
Here is what i do:
Export Labels > To File
Also, I know that PNG file's image value is composed as 0 1 2 and this is the reason image apperas black.
I tried this piece of code and I get the result with distingished but the dimensions of the image are changed.
figure;
[i, m] = imread('PixelLabelData/Label_1.png');
imshow(i,m);
I need is Labelled png image with proper dimensions. How can I do this ?
I am using https://supervise.ly instead of MATLAB. Development is much easier and faster.

Convolving an image with a mask displays an all white image

Very new to MatLab, just figuring some things out and had a question. I am basically trying to filter/blur an image using conv2() but I am getting an all white image when I am using imshow()
I am reading the image in with
testImage = imread('test.bmp');
This is a uint8 image of a grayscale.
I am trying to convolve the image with a 4 x 4 matrix.
fourByFour = ones(4);
When I actually execute, I am getting all white with imshow()
convolvedImage = conv2(testImage, fourByFour);
I should expect a filter placed on the image, not an entirely white one.
Any help would be appreciated.
I don't have your test image so I explain on an image. As the definition of conv2 it returns the two-dimensional convolution.
So please look at this little code:
clc;% clear the screen
clear all;% clear everything
close all;% close all figures
test = imread('test.bmp');
% read test image that is .bmp format and size :294x294x3 and uint8.
fourByFour = ones(4); % define 4 * 4 matrix all ones
convolvedImage = conv2(test(:,:,1), fourByFour);
%apply the ones matrix on image but to use conv2 on this image we apply on one of channels
figure, imshow(convolvedImage,[])
This is my command window, out put:
I'm using MAtlab 2017a, and if I use conv2(test, fourByFour); instead of conv2(test(:,:,1), fourByFour); ,the error is :
Error using conv2
N-D arrays are not supported.
So we should attention to class type and dimensions. And one more thing, in your command window please type edit conv2 you can read the details of this function and how to use it, but never edit it:). Thanks
test = imread('test.bmp');
% read test image that is .bmp format and size :294x294x3 and uint8.
fourByFour = ones(4);
% define 4 * 4 matrix all ones
test=rgb2gray(test);
% convert colorimage into gray
convolvedImage = conv2(double(test), double(fourByFour));
%perform the convolution
figure, imshow(convolvedImage,[])
% display the image

MATLAB how to get rid from whitespace when saving figures as JPG

I am writing a code for flowers recognition in MATLAB
but whenever I want to save a figure
it is saved with a white border - margin
how can I remove it
and save it as the exact size of the original image?
any help would be much appreciated
You can use getframe(gca) to grab the axes and not the entire figure (which is what would happen with simply getframe alone or getframe(gcf)). Then convert that frame to an image with frame2im. Then you can write that image to file with imwrite.
figure;
plot(x,y);
axis off
img = frame2im(getframe(gca));
imwrite(img,'myImage.png');
This might help:
figure('Color','none');
plot(1:10,1:10)
axis off
set(gca,'Color','none');
print ('-djpeg', 'no_background.jpg')

MATLAB's 'saveas' saves my figure as squares. But I want them to be more rectangular

I am using MATLABs 'saveas' to save off one of my figures as a .png or .jpg or whatever.
So I just do:
y = randn(1,00);
plot(y); grid on;
saveas(gcf,'y','png');
Now the problem is that the png or final picture comes out as a perfect square - even if I manually stretch the figure before I use the 'saveas' command.
How do I get it so save something more rectangular?
Thanks!
Here's a short sample taken from a mathworks discussion
figure('units','pix','pos',[100 100 200 400]) % create a 200x400 image
>> imagesc(rand(10,10)) % put some random data in it
>> print(gcf,'-dbitmap','test.bmp') % save to bmp
Using print like this saves an image in the desired resolution.

MATLAB - How to avoid a jagged image?

How do I avoid a jagged image in MATLAB?
I have a 600 x 600 pixels image opened in MATLAB and do some processing on the image. However, when I save it, it looks so blurred and jagged. What should I do?
(This question is related to my previous question, MATLAB - How to plot x,y on an image and save?)
fid = fopen(datafile.txt);
A = textscan(fid,'%f%f%f'); %read data from the file
code = A{1};
xfix = A{2};
yfix = A{3};
for k=1:length(code)
imagefile=code(k);
rgb = imread([num2str(imagefile) '.jpg']);
imshow(rgb);
hold on;
x = xfix2(k);
y = yfix2(k);
plot(x,y,'-+ b'); % plot x,y on the
saveas(([num2str(imagefile) '.jpg'])) % Save the image with the same name as it open.
end
hold off
If it is just a resolution issue, perhaps using the print command (as listed below) with an explicit resolution option may fix it.
print(gcf,'-djpeg','-r600',[num2str(imagefile)])
My guess would be JPEG compression artifacts. JPEG isn't a great format for data with a lot of high frequency components. Have you tried turning the compression down? Like so:
imwrite(f.cdata,([num2str(imagefile) '.jpg']),'Quality',100);
The default for the quality parameter is only 75. That's plenty for a lot of cases, but you might need more.