mat2gray with structure fields - matlab

I'm trying to display a matrix as an image with mat2gray and imshow. The data is saved as part of a structure. When I call the field separately I can display the image fine, but if I try to do it inside of a for loop it doesn't work. Does any one have an idea why this might be happening?
Example:
imshow(mat2gray(MyVals(5).Data))
Result: Success!
for L=1:numel(MyVals)
imshow(mat2gray(MyVals(L).Data))
end
Result:
Attempted to access limits(2); index out of bounds because
numel(limits)=0.
Error in mat2gray (line 38) if limits(2)==limits(1) % Constant Image

Related

How do I save a 2D-matrix as (bmp) image such that it looks similar to what image() produces?

I have a 2D uint16 matrix where the values range from 0 to 10424. I would like the result to be a saved monochrome image that looks similar to what image() produces (+ different color plate). I tried using imwrite and mat2gray with different bounds and shifting of the values, but failed until now.
What kind of translation does image() do that I would need do manually do that I can call imwrite(matrix, colorplate, file_type) or something similar?
Thanks in advance
Use the ind2rgb function
f = randi([0 10424],500,500); % generate random data
rgbImage = ind2rgb(f, jet(10424)); % apply color
imshow(rgbImage); % display (or use imwrite here)
View this page for a list of available colormaps (other than jet).

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.

Problems Applying MATLAB Filters to Dicom Images

I am working with dicom chest PA's. I have no problem loading the images and doing basic thresholding work, however I need to apply some filters. I will be designing my own filters in the future but for now I want to use the MATLAB ones. The images are 16bit and in MATLAB docs it says that fspecial works fine with 16bit images.
When I try using fspecial or the edge functions i get similar results to the images as shown below.
My question: why is this happening and how do i fix it?
Your help is appreciated.
Regards,
full_path = 'I:\Find and Treat\Anonymized_Data\000026_20050607112512_1.dcm';
dicominfo_image = dicominfo(full_path);
dicomread_image = dicomread(dicominfo_image);
dicomread_image = mat2gray(dicomread_image);
c = imcomplement(dicomread_image);
figure,
subplot(1,3,1)
imshow(c)
f = fspecial('laplacian');
cf = filter2(f,c);
subplot(1,3,2)
imshow(cf)
f1 = fspecial('log');
cf1 = filter2(f1,c);
subplot(1,3,3)
imshow(cf1)
EDIT
I added and modified the following code to take into account the dynamic range and plotted a mesh plot i obtained the following result and error message.
Warning: Error updating LineStrip.
Update failed for unknown reason
Warning: Error updating LineStrip.
Update failed for unknown reason
figure,
subplot(2,3,1)
imshow(c, [])
f=fspecial('laplacian');
cf=filter2(f,c);
subplot(2,3,2)
imshow(cf, [])
f1=fspecial('log');
cf1=filter2(f1,c);
subplot(2,3,3)
imshow(cf1, [])
subplot(2,3,4)
mesh(c)
subplot(2,3,5)
mesh(cf)
subplot(2,3,6)
mesh(cf1)
EDIT
LINK TO IMAGE FILE
Your images are just too big.
The Laplacian operator is a 3x3 matrix that approximates the second derivative of the images. Your image is 2700x2200 which means that the variability of the pixels in that small size(comparing to the whole image) is neglectable. You can see it better if you plot your Laplacian filter image as imshow(cf(300:end-300,300:end-300), []) removing the boundaries (where you actually have a big gap). To solve this you'll need to or redefine a Laplacian filter with a higher dimension or resize the image you have to a lower size (imresize is your friend).
The same happens with the log filter, but in this case you can do something about it. The default log filter returned by fspecial is 5x5, but you can actually ask fspecial to create bigger filters.
If you call it the following way:
f1=fspecial('log',50,0.3);
cf1=filter2(f1,c);
imshow(cf1, [])
You get the next figure, that looks kind of a good log filter result.

Matlab: How can I get an image handle for a matrix to be used in createMask?

I'm working with netCDF matrices representing satellite imagery. I'm trying to make a binary mask so I can analyze ROIs within the matrix. I've made an imellipse to use as the mask.
Now, the function createMask requires an image handle for the underlying data. How can I get an image handle for my data matrix? I've displayed in as a contourf plot.
The createMask() function works without an image handle as well. To get the binary mask just do like this:
imshow(Img,[]);
e = imellipse();
mask = createMask(e);
If you want to do it using an image handle, you can create an image handle by doing:
h_img = imshow(Img,[]);
You can then use this handle in the createMask() function. (But it is not really neccesary)

imshow() produces different output to 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?
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);