Unable to save iverseftt image and image K-Space in matlab - matlab

I have a brain MRi image in .png format. I read the image and extract the K-Space and set some of the K-Space as 0
img_fft = fftshift(fft2(img));
sizes = size(img_fft);
row_half = sizes(1)/2;
flag = true;
for r = row_half:sizes(1)
for c = 1:sizes(2)
img_fft(r,c) = 0+1i*0;
end
end
After this I change the image back to image space using
img_back = ifft2(ifftshift(img_fft));
and after this I cast the image to uint8 as that was the original image format.
When I try to plot my image using imshow() I get a different output compared to when I write the image using imwrite. Also if I use abs(img_back) in imwrite I get an error.
Error using abs: Complex integers are not supported.
My plotting code is below:
img_back = ifft2(ifftshift(img_fft));
img_back = cast(img_back,'uint8');
subplot(1,3,1), imshow(img)
subplot(1,3,2), imshow(img_back)
subplot(1,3,3), imshow(abs(img_fft),[])
imwrite(abs(img_back),'back_img.png','png')
Can someone tell me what I am doing wrong here?

Take absolute value after inverse Fourier transform and then cast the result to uint8 type:
img_back = abs(ifft2(ifftshift(img_fft)));
img_back = cast(img_back,'uint8');

Related

Save concatenated images in MATLAB

I am trying to concatenate two 256x256 images and save them using imwrite. The saved image is supposed to be 256x512, but when I load the saved image, the size shows to be 343x434x3. How do I solve this?
the code I am using is:
new_name3 = strcat(f_name_image, '\', kk, '_', num2str(ff), '_pair.png');
pair = [orig_im noisy_image]; %concatenating two 256x256 images
imagesc(pair)
f = getframe(gca);
im = frame2im(f);
imwrite(im, new_name3);
Saving the image from the frame can be lossy without configuring additional options. To retain the pixel information save the concatenated image directly from the pair (here Image_Pair) array. Also, the third dimension in 343x434x3 represents the RGB colour channels of the image.
%Grabbing the two images%
Image_1 = imread('Image_1.jpeg');
Image_2 = imread('Image_2.jpeg');
%The file name for the concantenated images%
New_File_Name = "Image_3.jpeg";
%Concatenating the images%
Image_Pair = [Image_1 Image_2];
%Displaying the image pair%
imshow(Image_Pair);
%Saving the image to the "New_File_Name"%
imwrite(Image_Pair, New_File_Name);
%Loading the saved image to see if dimensions are consistent%
Saved_Image = imread('Image_3.jpeg');

How to convert Matlab's Tiff function in Octave?

I'm trying to convert a function I use in MATLAB to merge an RGB image with an IR image (both in JPG formats) to create a Tiff image with 4 bands (R, G, B, IR). However, I have some problem implementing the Tiff function, as it looks like that Tiff is not implemented in Octave.
Someone know how can I convert the Tiff part of following function to Octave?
for fidx = 1:numel(filelist1)
rgb = imread(fullfile(folder1, filelist1{fidx}));
numrows = size(rgb,1);
numcols = size(rgb,2);
ir = imread(fullfile(folder2, filelist2{fidx}));
data = cat(3,rgb,ir);
t = Tiff(fullfile(folderout, sprintf(outfilepattern, fidx))','w');
setTag(t,'Photometric',Tiff.Photometric.RGB);
setTag(t,'Compression',Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',4);
setTag(t,'SampleFormat',Tiff.SampleFormat.UInt);
setTag(t,'ExtraSamples',Tiff.ExtraSamples.Unspecified);
setTag(t,'ImageLength',numrows);
setTag(t,'ImageWidth',numcols);
setTag(t,'TileLength',32);
setTag(t,'TileWidth',32);
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t,data);
close(t);

DIPimage measure missing argument

I am trying to use DIPimage to get some measurements of each object in an image and I get this error:
Error using dip_measure
DIPlib Error in function dip_Measure.
DIPlib Error in function dip_ImageCheck: Data type not supported
Error in measure (line 209)
data = dip_measure(object_in,gray_in,measurementID,objectIDs,connectivity);
Error in Untitled (line 13)
msr = measure(b, [], ({'size', 'perimeter','podczeckShapes'}))
How can I solve it?
Code:
Image = rgb2gray(imread('pillsetc.png'));
BW = imbinarize(Image);
BW = imfill(BW,'holes');
imshow(BW);
[B,L] = bwboundaries(BW,'noholes');
k = 1;
b = B{k};
y = b(:,2);
x = b(:,1);
msr(k) = measure(BW, [], ({'size', 'perimeter','podczeckShapes'}))
sz = msr.size;
podczeckShapes = podczeckShapes;
One problem with your code is the call to imfill. Because the image has bright values all around the image, it is considered that there's a large object with a hole, and your actual objects are inside this hole. imfill fills the hole, leaving the whole image white.
Instead, I suggest the following code to remove the frame:
Image = rgb2gray(imread('https://i.stack.imgur.com/fmqAF.jpg'));
BW = imbinarize(Image);
BW = BW - bpropagation(false(size(BW)), BW);
Because we used a filter in DIPimage, the BW variable now contains a dip_image object, not a normal MATLAB array. dip_array(BW) extracts the normal MATLAB array that is inside. The dip_image object behaves differently from a MATLAB array. For example, you can display it to an interactive figure window by just typing its name:
BW
Next, we apply labeling so that we know which object ID in the measurement data corresponds to which object:
lab = label(BW);
dipshow(lab,'labels')
Now we can apply the measurement function. If we use BW as input, label will be called on it. Since we already have that result, let's use it directly:
msr = measure(lab, [], {'size', 'perimeter','podczeckShapes'});
Let's examine results for object ID 8, which is the large square:
sz = msr(8).size
square = msr(8).podczeckShapes(1)
triangle = msr(8).podczeckShapes(3)
There are other things you can do with the measurement structure, I suggest you read the documentation. For example, we can remove from it the measurement for the littlest objects, which to me look like noise:
msr = msr(msr.size>100); % remove measurement for noise

read and show raw depth image in matlab

I have a set of .raw depth images. The image format is 500X290 with 32 bytes per pixel. When I open them with IrfanView image viewer I see the depth image correctly like this:
displayed image in IrfanView
Now I want to read and display the same depth image in Matlab. I do like this:
FID=fopen('depthImage.raw','r');
DepthImage = fread(FID,[290,500],'bit32');
fclose(FID);
colormap winter;
imshow(DepthImage);
DepthImage is a 290X500 type double matrix.
what I get from this code is this image:
displayed image in Matlab viewer
when I change fread parameter from 'bit32' to 'bit24' I get this:
displayed image in Matlab with bit24
I guess each element in DepthImage contains 32 bits where each 8 bits corresponds to R,G,B and D values. but how can I read the image correctly and display it like the one in IrfanView?
the raw file: https://drive.google.com/file/d/1aHcRmMKvi5gtodahR5l_Dx8SbK_920c5/view?usp=sharing
There might be an issue with image metadata header, like "date and time of the shot", "camera type". Open your image with notepad++ to check for "date and time". If you upload your original raw image, it will be easier to try things.
Upd: Ok, this is something. Check if it helps
FID=fopen('camera00000000000014167000.raw','r');
DepthImage = fread(FID,290*500*4,'int8');
DepthImageR = DepthImage(1:4:end);
DepthImageG = DepthImage(2:4:end);
DepthImageB = DepthImage(3:4:end);
DepthImageD = DepthImage(4:4:end);
dataR = reshape(DepthImageR, 500,290);
dataG = reshape(DepthImageG, 500,290);
dataB = reshape(DepthImageB, 500,290);
dataD = reshape(DepthImageD, 500,290); % all equal to 64 - useless
figure()
subplot(2,2,1)
image(dataR)
subplot(2,2,2)
image(dataG)
subplot(2,2,3)
image(dataB)
subplot(2,2,4)
data = zeros(500,290,3);
data(:,:,1) = dataR;
data(:,:,2) = dataG;
data(:,:,3) = dataB;
image(data)

Storing two variables image in one variable in matlab

I have an RGB image which I converted into index image using rgb2index. The resultant image is stored in two variable (as matlab requirement). But I want to have it in one variable for further processing. This is what I have tried. The resultant is black.
clc
clear all
close all
%%
I = imread ('Daniel1_SRGB.png');
[in_I,map] = rgb2ind(I,3,'nodither');
imshow (in_I,map)
imwrite (in_I,map,'new_image.PNG','png')
new_I = imread ('new_image.png');
imshow((new_image));
But if do imshow((new_image,map)) it gives me the correct answer. I want it to be independent of variable map.
To convert a indexed image to RGB use:
new_I = ind2rgb(in_I,map)
Not the most elegant solution but this works.
resR = reshape(map(in_I(:)+1,1), size(in_I));
resG = reshape(map(in_I(:)+1,2), size(in_I));
resB = reshape(map(in_I(:)+1,3), size(in_I));
res = cat(3, resR, resG, resB);
imshow(res);
Edit: Modified answer to include rayryeng's improvement.