MATLAB - error using imtransform() - matlab

I got an error when trying to run the imtransform() line in my code. I got:
Error using imtransform>parse_inputs (line 438)
XData and YData could not be automatically determined.
Try specifying XData and YData explicitly in the call to
IMTRANSFORM.
Error in imtransform (line 265)
args = parse_inputs(varargin{:});
Error in main (line 9)
newImage = imtransform(rgbImage,tf); %# Transform the image
I used the function tf = maketform() to generate the transformation itself.
So, in order to figure out what wrong with my code i simplified as much as I can the function:
function U = fisheye_inverse(X)
U = X;
end
and my code looked like this:
rgbImage = imread('IMG-20150622-WA0046.jpg'); %# Read the image
tf = maketform('custom',2,2,[],... %# Make the transformation structure
#fisheye_inverse,options);
newImage = imtransform(rgbImage,tf); %# Transform the image
imshow(newImage);
and still the code yell at me this error again.
I looked at a similar problem Here that posted here in the past, but no one answer the question in the end.
Thanks in advance, Gal :)

Related

How to make a GIF from a set of MATLAB images

I have an array of values which I turn into a color image like so:
my_img = imagesc(my_data(:,:,tidx)); axis off; colormap('default'); c=colorbar; c.FontSize=26; c.FontWeight='bold';
I want to know if it's possible to loop through all the index values in my_data and turn them into a GIF.
I tried this:
for tidx = 5:64
my_img = imagesc(my_data(:,:,tidx)); axis off; colormap('default'); c=colorbar; c.FontSize=26; c.FontWeight='bold';
imwrite (my_img, int2str(tidx)+"_cspattern.png");
end
Which gave me this error:
Error using imwrite (line 427)
Expected DATA to be one of these types:
numeric, logical
Instead its type was matlab.graphics.primitive.Image.
I looked it up and tried this solution out:
for tidx = 5:64
my_img = imagesc(my_data(:,:,tidx)); axis off; colormap('default'); c=colorbar; c.FontSize=26; c.FontWeight='bold';
IMG_to_write = uint8(normalize(my_img, 'range', [0 255]));
imwrite(IMG_to_write, int2str(tidx)+"_cspattern.png");
end
But that just gave me this error:
Error using normalize>checkSupportedArray (line 159)
Invalid data type. First argument must be a real numeric or logical array, a table or a timetable.
Error in normalize (line 86)
checkSupportedArray(A,method,methodType,false);
Error in GenerateFigures (line 18)
IMG_to_write = uint8(normalize(SavePattern, 'range', [0 255]));
I feel as if I am overthinking this because there must be a simple way to load the data I want and save it as an image or series of images.
imagesc produces a graphics object, not a data array. It’s meant for displaying images into figures, not saving them into files. You need to hand imwrite your image data directly, along with your desired color map.
for tidx = 5:64
imwrite(my_data(:,:,tidx),parula(),int2str(tidx)+"_cspattern.png");
end
If you want to scale each image so that the full color range is used, the way imagesc does, you can do this manually.
for tidx = 5:64
my_data_slice = double(my_data(:,:,tidx));
my_data_slice = my_data_slice - min(my_data_slice,[],'all');
my_data_slice = my_data_slice / max(my_data_slice,[],'all')*255;
my_data_slice = uint8(my_data_slice);
imwrite(my_data_slice,parula(),int2str(tidx)+"_cspattern.png");
end

How do I consider an cell array image as an input of blockproc?

function block = block_partion(image, size_of_theblock)
image = cellfun(#(x)((x-mean(x(:)))./std(double(x(:)))),image, 'UniformOutput',false);
fun = #(block_struct) std2(block_struct.data) * ones(size(block_struct.data));
block = blockproc(image, [size_of_theblock size_of_theblock],fun);
end
This is the error that i get:
Error using blockproc>parse_inputs (line 997)
Invalid input image. The input image to BLOCKPROC should be either a numeric matrix, a string filename, or an
ImageAdapter object. See the documentation for BLOCKPROC for a list of readable file formats and their
extensions. See the documentation for ImageAdapter for information on making ImageAdapter objects.
blockfun = #(x) blockproc(x, [size_of_theblock size_of_theblock], fun);
block = cellfun(blockfun,image, 'UniformOutput', false);
Thanx, replacing this lines in the code it works well!

Series of dicom images into specific format in matlab

I have series of matlab images which belong to a single patient. I found some code online, but it is sowing some error. I want something like this, Image
Here is the code I have.
% Preallocate the 256-by-256-by-1-by-20 image array.
X = repmat(int16(0), [256 256 1 20]);
% Read the series of images.
for p=1:20
filename = sprintf('brain_%03d.dcm', p);
X(:,:,1,p) = dicomread(filename);
end
% Display the image stack.
montage(X,[])
I found this code from here:
https://www.mathworks.com/company/newsletters/articles/accessing-data-in-dicom-files.html
Error using montage>validateColormapSyntax (line 339)
An indexed image can be uint8, uint16, double, single, or logical.
Error in montage>parse_inputs (line 259)
cmap = validateColormapSyntax(I,varargin{2});
Error in montage (line 114)
[I,cmap,mSize,indices,displayRange,parent] = parse_inputs(varargin{:});
Error in Untitled2 (line 9)
montage(X,[]);
The syntax for calling the montage function has changed since that code sample was written (back in 2002!). As noted in the comments section of the File Exchange submission for the sample DICOM data files, the new correct syntax is this:
montage(X, 'DisplayRange', []);
You were getting that error since the new syntax interprets montage(X, []); as if X were an indexed color image (which isn't allowed to be a signed int16 type, according to the error) with an empty color map [].

Error creating a plot in MATLAB using values I got from excel using actxserver.

Hello so I am attempting to write a MATLAB script that reads multiple excel files at ones and gathers values from the worksheet. through some research I found that using actxserver is a very quick way to get the data. The values are stored in the out and out1 variables and by using vertcat function I create an array that has all the 300,000 values that I need to store. This seems to work. However when I try to plot the values in a simple x vs y graph using plot I get an error that states:
Error using plot
Not enough input arguments.
Error in opener (line 36)
plot(out0,out1)
Below is the code. I am sure it is a simple fix but I have looked and not found anything helpful.
clc;
filename = 'C:\Users\Public\Documents\DASYLab\13.0.0\eng\data\TRIAL\';
D = dir([filename, '\*.csv']);
Num = length(D(not([D.isdir])));
f = Num;
ex = actxserver('excel.application');
o = 0;
k = 0;
for i=1:f
if(i<=10)
ex.Workbooks.Open([filename,'mydataIII_0',num2str(i-1)]);
else
ex.Workbooks.Open([filename,'mydataIII_',num2str(i-1)]);
end
out0 = get(ex.Range('A8:A40967'),'Value');
out = vertcat(o,out0);
o = out;
out1 = get(ex.Range('C8:C40967'),'Value');
outone = vertcat(k,out1);
k = outone;
end
figure;
plot(out0,out1)
ex.Quit
Hello sorry I but I figured it out. The issue was that when the get function was used to get the value it created a cell array.
http://www.mathworks.com/help/matlab/cell-arrays.html
if you want to convert the data to a ordinary array one can use the cell2mat() function

Using imfreehand within your program

For instance, I start my program as follows reading some image:
I=input('image name: ','s');
img=double(imread(I));
I'm planning to work only on some portion of that image. So, I noticed that I may need h=imfreehand for this purpose.
Thus, I have inserted h=imfreehand under the two lines above. What I got is a white screen. So, how can I get the image and select the region I want? The other thing is, how can I tell my program to work only on that region I have selected?
Thanks.
UPDATE
I did the following in a portion of my code:
figure, imshow(img);
h = imfreehand;
position = wait(h);
% post processing
se=strel('disk',3);
erosion=imerode(h,se);
result_image=imsubtract(h,erosion);.
But, I got the following error:
Error using imerode
Expected input number 1, IM, to be one of these types:
numeric, logical
Instead its type was imfreehand.
Error in morphop>CheckInputImage (line 335)
validateattributes(A, {'numeric' 'logical'}, {'real' 'nonsparse'}, ...
Error in morphop>ParseInputs (line 166)
A = CheckInputImage(A, func_name);
Error in morphop (line 14)
[A,se,pre_pad,...
Error in imerode (line 123)
B = morphop(A,se,'erode',mfilename,varargin{:});
Error in program (line 155)
erosion=imerode(h,se);
Is it to do with erosion? What can be done in this case?
`
Following the advice in the matlab documentation try this:
I=input('image name: ','s');
img=imread(I);
figure, imshow(img);
h = imfreehand;
position = wait(h);
Edit:
The documentation suggests as an alternative
figure, imshow(img);
h = imfreehand(gca);
Try passing the handle of the plot to imfreehand()
I=input('image name: ','s');
img=double(imread(I));
figure;
ih=image(img);
h=imfreehand(ih)
Sorry, I don't have the image processing toolbox to test this.
Seems like converting your image (possibly uint8) to double causes a problem.
I would either do:
use the original coding of the image (for example img_uint8 = imread('coins.png') is coded using integers). The problem is that you won't probably be able to use imfreehand as it needs to read double or single precision float.
convert the image using img_double = double(imread('coins.png')) so that imfreehand will work. The conversion however causes a display problem that you can bypass with imshow(img_double,[]) that is an analogue of imshow(img_double, [min(min(img_double)), max(max(img_double))]). It forces imshow to correctly use the full range of data (255 Vs. 255.0)
The best option is therefore the second one.
To use imfreehand, #Try Hard gave a nice code h = imfreehand or h = imfreehand(gca)