imshow() produces different output to imwrite() - matlab

I was wondering how it is possible to save an image created by imshow() in matlab. The code below uses the imshow() function with the min and max arguments specified - How can I apply this directly to the image itself instead of just specifying Matlab to show it?
imshow(img4fft, [1 300000]);
imwrite(img4fft, 'img.png');
Writing to the file produces a different output to what is shown via imshow().
Can anyone suggest how to get the output from imshow() saved as an image?
Many thanks MatLab is an alien language to me!

I obtain the same result by doing:
img4fft2=min(double(img4fft),300000)/300000;
imwrite(img4fft2,'img.png');

You could apply saturation to the image manually:
I2 = imadjust(I, [0,30000],[0,2^{resolution}-1]);
imwrite('out.png', I2);

Related

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

imshow shows different output than imwrite

I was wondering how it is possible to save an image created by imshow in matlab. The code below uses the imshow function with the min and max arguments specified - How can I apply this directly to the image itself instead of just specifying Matlab to show it?
maxBlur=3;
a = imshow(fDmap,[0 maxBlur]);
imwrite(a, 'img.png');
Writing to the file produces a different output to what is shown via imshow.
Can anyone suggest how to get the output from imshow saved as an image?
To specify upper and lower intensity limits for imwrite (similar to what you've done for imshow), you will want to use the second inputs to mat2gray to adjust the contrast of your image prior to saving it with imwrite.
imwrite(mat2gray(fDmap, [0 maxBlur]), 'img.png');
If you literally want an image of what you're seeing with imshow, you can use saveas to take a screenshot. This will potentially be lower resolution than the previous approach and will also include whitespace around the image.
imshow(fDmap, [0 maxBlur]);
saveas(gcf, 'img.png');
NOTE: The a variable that you passed to imwrite in your post is a MATLAB graphics handle to an image object that is used to manipulate the rendered image. imwrite expects image data in matrix form not as a graphics handle.

YCbCr to RGB conversion MATLAB using ycbr2rgb results in pink picture

I'm trying to convert an YCbCr image to RGB ysing MATLAB's function ycbcr2rgb. My resulting picture ends up being pink, and converting back again afterwards (should give me the original picture?) creates yet another image mostly grey.
For reference I tried to convert each channel individually by formula and it ends up the same.
I'm using a bigtiff format because of large filesize and if any help the imfinfo shows compression using JPEG.
Here is my code:
x=imread('picture.tiff','Index',9); %(9 subresolutions)
rgb=ycbcr2rgb(x);
imshow(rgb);
Can it be because of MATLABs function using the originial definition of YCbCr using ranges from 16-235 while my image is ranging from 0-255? If so is there any means of correcting this using the inbuild function?
I have added the pictures here, first image is showing imshow(rgb), while the second image is the original ycbcr. What I noticed is that in the Windows image viewer it actually shows it correct, it's just MATLAB's imshow that displays it pink after conversion.
Is there any chance you could point me in the right direction?
Thanks
Sonny
Apparently imread reads YCbCr images as RGB when loading it, which is why the problem occured.
Thanks for the help to all of you.
imread documentation
This link gives all the conversion formulae:
http://www.easyrgb.com/index.php?X=MATH&H=11
The below code converts image from RGB space to YCbCr space and back.
rgb = imread('board.tif');
imshow(rgb);
figure;
ycbcr = rgb2ycbcr(rgb);
imshow(ycbcr);
figure;
rgb2 = ycbcr2rgb(ycbcr);
imshow(rgb2);
Use MATLABs built in functions only. Also, if you're facing issues while converting from ycbcr to rgb you should probably try to convert the image to other form and then convert that form to RGB. (a dirty hack)
Just divide the image by 256 before converting it back to RGB.
y = ycbcr2rgb(z/256); % z holds the YCbCr image.
Worked for me.
Hope that helps :)

How do I display a surf of an image in MATLAB?

I have an image that shows depth of the image using colors where warmer colors represent the closer parts of the image and cooler colors represent objects further away. I want to represent this image as a surf plot showing the depth. I have to do this in java but I think its easier to understand the process in Matlab first before moving on. I tried using the size of the image and plotting that but it kept giving me errors. Any help would be much appreciated.
I tried the surf function:
`img = imread('sample.png');
grayImage = rgb2gray(img);
surf(double(img))`
and got this error:
>> surf
Attempt to execute SCRIPT surf as a function:
C:\Users\kuchin\Documents\MATLAB\surf.m
Error in surf (line 3)
surf(double(img))
EDIT
As seen in your comment in this own post, your problem is that you are overriding the surf function by another surf.m file you have. Dont name your matlab files with the same name that Matlab built functions. Go to C:\Users\kuchin\Documents\MATLAB\surf.m and name it mysurf.m. It will resolve your problem.
ORIGINAL POST
If you have a depth image (MxN) of doubles just
surf(img);
if they are uint8
surf(double(img));
Will do the trick.
Test code:
img=imread( 'coins.png' );
surf(double(img))
Outputs:

Performing mean filtering on an image matlab

i'm trying to perform mean filtering on an image in Matlab, i have to make it modular so i have another function for my averaging and then my script calls the function. I run the script and there are no errors but it doesn't seem to do the filtering as the output of the image is no different to the original. Can anyone see where i am going wrong?
%input image
image1 = imread('moon.jpg');
%convert to grayscale
%mean filtering
mean = averagefilter2(image1);
image_grey = rgb2gray(mean);
figure;
imshow(image_grey);
%my average filter function%
function img=averagefilter2(image1)
meanFilter = fspecial('average',[3 3]);
img = imfilter (image1,meanFilter);
end
Thanks!
Can you show your figures of image1 and image_grey? You can also try to imagesc(abs(image1-image_grey)) to see the difference between the original image and the averaged one. I ran your code and didn't see problems. I can observed the smooth effect in my sample image.
Remember to apply rgb2gray to your image1 as well.