Undefined function or variable 'map' Error. How to fix?

24 views (last 30 days)
Michael
Michael on 17 Oct 2013
Commented: Michael on 18 Oct 2013
After working on this for a few days. This is the code I've written thus far:
if true
% code
end
% Clear the workspace
clc
clear all;
% Read the image 'Proj2.tif'
Proj2Image = 'Proj2.tif';
img = imread(Proj2Image);
% Stores the image as an array
img = img(:,:,1);
% I then take the Fast Fourier Transform (FFT) of the Image
imgfft = fft2(img);
% Next, I create a Butterworth Filter. A Low Pass Filter
% could probably also be performed, and work just as well.
% Note: C is a constant
% X is the size of the dimension of array X
% Y is the size of the dimension of array Y
c = 2;
X = size(imgfft, 1);
Y = size(imgfft, 2);
% A returns an array of ones in the X and Y direction
A = ones(X,Y);
XX = [204 182 191 196 214 219 228];
YY = [273 275 267 282 264 279 271];
% L is the max number of pixels in the image (i.e. 255)
L = 255;
for i=1:length(L)
for x = 1:X
for y = 1:Y
%Compute the distance between the points.
Lxy = sqrt((x-XX(i))^2 + (y-YY(i))^2);
A(x,y) = A(x,y) + 1/(1+(Lxy/L(i)^2))^(2*c);
end
end;
end;
% Subtract the array of ones
A = 1 - A;
% Next, I apply the filter by shifting the FFT of the image
% and multiplying it by the array of ones.
FilterImage = fftshift(imgfft).*A;
% Here, I shift back and perform the Inverse FFT
FilterImage2 = ifft2(fftshift(FilterImage));
% Next, I display FilterImage2 with the colormap defined as 'map'
imshow(abs(FilterImage2),map);
% Then I resize and display the 'periodicpattern.tif' image
% And compare it to the 'Proj2.tif' image that I filtered,
% shifted, and performed the FFT and Inverse FFT on.
[img,map] = imread('periodicpattern.tif');
figure('Name','Patterns/comparison');
subplot(1,2,1), imshow(abs(FilterImage2),map);
title('Patterns');
subplot(1,2,2), imshow(img,map);
title('Periodic Pattern of Image');
The problem I have with this code though is that when I run it, I get the following error message on Line 63: Undefined function or variable 'map'.
Do you have any other method I could use to fix this error, other than:
imshow(abs(FilterImage2),map);
I'm not quite too sure why it is doing this since Matlab ought to accept:
imshow(X,map)
which I don't think is all that much different. I tried replacing the abs(FilterImage2) by assigning it to a variable called I:
I = abs(FilterImage2);
But Matlab doesn't like this either.
Again, my input image is attached and all I am trying to do with it is get the mesh periodic cross-pattern out of it.
  1 Comment
Jan
Jan on 17 Oct 2013
As usual I recommend to omit the clear all, such that you can use the debugger to find the reasons of problems by your own.

Sign in to comment.

Accepted Answer

Vivek Selvam
Vivek Selvam on 17 Oct 2013
Michael, you need to change line 9 from
img = imread(Proj2Image);
to
[img,map] = imread(Proj2Image);
  3 Comments
Michael
Michael on 18 Oct 2013
Well here's the .jpg version of it. This forum board won't accept .tif files for some reason.
It's basically the same picture that is displayed on the right-hand side in Figure 4. But I'm trying to equate the left-hand side with the right-hand side by applying the fft, shifting it, multiplying it by A, and doing the inverse FFT of it to extract the mesh periodic pattern out of the original image 'Proj2.jpg'.
I may be approaching to solve this problem the wrong way, and if so, I would like to know where I am wrong, and how I could fix it. I'm sure it's not anything too complex. I don't know. I sort of feel like maybe I should apply the log function somewhere in my coding to get that periodic pattern out of the original 'Proj2.jpg' image.
Not too sure about that though. I'm just stuck about how I should proceed from here.

Sign in to comment.

More Answers (2)

Yatin
Yatin on 17 Oct 2013
Hi,
I notice that while reading the image with the " imread " function you have not read the map. The syntax for the imread function that you need to use in this case is
[img, map] = imread(Proj2Image);
The map obtained above can then possibly be used in the imshow function for displaying FilterImage2. Since the variable map is not available before, MATLAB is returning an error. Hope this solves your problem.

Jan
Jan on 17 Oct 2013
Edited: Jan on 17 Oct 2013
This is a strange question. You did not define a variable called "map" but want to use it. The explanation, that "Matlab ought to accept imshow(X,map)" is very weak, because this argument is simply wrong. Try it:
clear all
imshow(X, map)
Now beside "map" even "X" is undefined.
It is impossible to recommend a workaround, because the definition of "map" is missing. But we cannot guess, what this should be, because the definition is missing...
Notice, that you define "map" in the following command:
[img,map] = imread('periodicpattern.tif');
So perhaps you simply forgot to catch the second output in the imread command before?
[img, map] = imread(Proj2Image);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by