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

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 Commento

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.

Accedi per commentare.

 Risposta accettata

Michael, you need to change line 9 from
img = imread(Proj2Image);
to
[img,map] = imread(Proj2Image);

3 Commenti

if true
% code
end
%Jon Michael Gresham
% ECE4367 Image Processing
% Undergraduate, Sr.
% 10/17/2013
%
% Project 2: Implement a method in Matlab for extracting
% the periodic pattern that is vaguely visible in the image
% 'Proj.tif'
% Clear the workspace
clc
% Read the image 'Proj2.tif'
Proj2Image = 'Proj2.tif';
[img,map] = imread(Proj2Image);
figure
imshow(img,map);
title('Original Image');
% I store the original 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 Low-Pass Filter.
% 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 X and Y.
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;
% Next, I apply the filter by shifting the FFT of the image
% and multiplying it by the array of ones.
FilterImage = fftshift(imgfft).*A;
figure
imshow(abs(FilterImage),[]);
title('Image After Shifting by FFT and Multiplying By A');
% Here, I shift back and perform the Inverse FFT
FilterImage2 = ifft2(fftshift(FilterImage));
figure
imshow(abs(FilterImage2),[]);
title('Shifting Back and Performing Inverse FFT');
% 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','Periodic Pattern Comparison');
subplot(1,2,1), imshow(abs(FilterImage2),map);
title('Patterns');
subplot(1,2,2), imshow(img,map);
title('Periodic Pattern of Image');
Okay, so after making those changes I'm now getting four figures like I want it to. However, I'm trying to make Figure 3 look like the image on the right-hand side of Figure 4 after applying the fft, shifting it, and performing the inverse fft.
And it's not doing what I want it to do. Could you explain to me what I am doing wrong here, and what I need to do coding-wise to change Figure 3 to look like the mesh periodic pattern displaying in the right-hand image in Figure?
What I'm trying to accomplish with this m.file is take the original image, extract the periodic mesh pattern out of it, display that periodic pattern in Figure 3, and compare it to the 'periodicpattern.tif' image shown on the right-hand side of Figure 4.
If it helps any further, I believe it's line 64 where I feel like I am doing something wrong.
imshow(abs(FilterImage2),[]);
I'm just not too sure what I need to change it to so that output can look exactly like the 'periodicpattern.tif' image displayed on the right-hand side of Figure 4.
Can you upload 'periodicpattern.tif'?
Michael
Michael il 18 Ott 2013
Modificato: Michael il 18 Ott 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.

Accedi per commentare.

Più risposte (2)

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 il 17 Ott 2013
Modificato: Jan il 17 Ott 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);

Categorie

Scopri di più su Images in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by