In matlab, I have this .mat file. If you look inside and double click the cells with vlues 1.000, you will find that the value that appears is 0.999999999259113 . That's fine. But, when I use for instance the following command:
>> sel = find (u2 == 0.999999999259113 );
The answer I get is as follows:
>> sel
sel =
5
We have more than one 0.999999999259113 value. Don't we? Where are they? What is 5 supposed to be?
Now, when I come to the following function since the .mat file is related to this image:
function s = pixel(x, y)
pixels = [];
index = 1;
for i=1:length(y)
for j=1:length(y)
if y(i,j) == 0.999999999259113;
pixels(index) = x(i,j);
index = index+1;
end
end
end
pixels
end
And, when I run the function I get the following (I: image, u2: the .mat file):
pixel(I,u2);
pixels =
[]
Empty?! What is this supposed to mean?
Any ideas why I'm not getting the expected results, although the commands seem logically working?
Thanks.
Instead of checking for exact floating point equality, can you set a tolerance? Find values close to 1.0 like this:
tolerance = 0.01;
sel = find(abs(u2 - 1.0) < tolerance);
Try using
if abs(y(i,j)-)<1e-6
instead of
if y(i,j) == 0.999999999259113;
Related
I'm running a while loop and I am running in to some problems.
I have the following piece of code:
Angle_int = 0.5; % Initial interpolation angle of attack
Clmax2d(1,1:length(Yle_wing)) = 3; % Dummy value
diff = 0; % Dummy value
while sum(diff < 0) > fix(tol*length(Yle_wing))
Angle_int = Angle_int + 0.5; % Interpolation angle increases with 0.5 with every iteration
for j = 1:length(Yle_wing)
CL3d = interp1(Angle,[cl_matrix(1,j) cl_matrix(2,j) cl_matrix(3,j)],Angle_int,'linear');
CL_3DD(:,j) = CL3d;
end
diff = (Clmax2d - CL_3DD); % Difference between Cl2d and Cl3d
Angle_stall = Angle_int;
CL_3D = CL_3DD;
end
For some reason, CL_3D = CL_3DD; and Angle_stall = Angle_int; seem to disappear when the while loop finishes. Hence, I cannot use their converged values ahead of the while loop since these variables are not recognized. I get the following error:
Undefined function or variable "CL_3D".
Hence, does someone knows what I am doing wrong? or any tips would be welcome as well.
Thanks in advance,
cheers
The error message:
Undefined function or variable "CL_3D".
is always because you're trying to use a variable or function that you haven't initialized yet. Often this happens in loops where you want to increment a counter, or compare values etc.
A common error is doing something like this without writing ii = 0 in front of the loop:
while ii < some_num
ii = ii + 1;
some_function();
end
With your dummy variables, you never enter the loop (unless tol < 0, which seems like an odd choice). You probably want to initialize diff = Inf or something like that. However, using diff as a variable name is not a good idea, since it's a builtin function.
You probably try to use CL_3D, when it's not yet initialized (somewhere else in your code, not in the part you posted). MATLAB tells you which line the error appears in, you should try using it!
Maybe initializing it like zeros(size(Clmax2d)); could work (it will definitely remove your error, but it might not give the desired behavior).
PS!
Using i and j as variables are not recommended as they represent the imaginary unit in MATLAB.
I found following code on internet. I am new to matlab. Now the problem whenever i copy-paste this code then it shows me error message.
function[p] = lagrange_interpolation(X,Y)
|
Error: Function definitions are not permitted in this context.
The code snippet is:
function[p] = lagrange_interpolation(X,Y)
L = zeros(n);
p = zeros(1,n);
% computing L matrice, so that each row i holds the polynom L_i
% Now we compute li(x) for i=0....n ,and we build the polynomial
for k=1:n
multiplier = 1;
outputConv = ones(1,1);
for index = 1:n
if(index ~= k && X(index) ~= X(k))
outputConv = conv(outputConv,[1,-X(index)]);
multiplier = multiplier * ((X(k) - X(index))^-1);
end
end
polynimialSize = length(outputConv);
for index = 1:polynimialSize
L(k,n - index + 1) = outputConv(polynimialSize - index + 1);
end
L(k,:) = multiplier .* L(k,:);
end
% continues
end
In all likelihood, you are probably attempting to mix random code along with your function. There are two types of M files:
scripts - have "random" code that is executed independent of anything else
functions - are the "classic" definition of functions
You cannot mix the two (that's a lie, but for now a good one). So if you are defining a function, that should be the only code in your .m file.
You should later use this function in either the command window or another function or a script by calling it via p = blahblah(bleaurgh);.
TL;DR: Make sure the function code is the only code in the script file, save it with the same name.m, call the function from somewhere else.
I'm having a kind of trouble since I'm new the concept Image Analysis and the tool Matlab.
What I have in my mind does not work well as lines of codes.
I'm trying to dilation function for binary images. It has to widen the given binary images.
This is my main page:
I = imread('logo_XXXX.png');
binaryImage = im2bw(I, 0.4);
s = ones(3,3,'int8');
i = dilate(binaryImage,s);
figure, imshow(i);
This is dilate.m function:
function [i] = dilate(I,s)
[Irows,Icols] = size(I);
i=I;
Itemp = I;
for row=1:Irows
for col=1:Icols
x = intersectAt(Itemp,s,row,col);
if x == 1
i(row,col)=1;
else
i(row,col)=0;
end
end
end
And this is istersectAt.m function:
function [i] = intersectAt(I,s,row,col)
[Srows,Scols] = size(s);
[Irows,Icols] = size(I);
i=0;
rowx = row - int8(Srows/2);
colx = col - int8(Scols/2);
for r=1:Srows
for c=1:Scols
if rowx+r <= 0 || rowx+r > Irows || colx+c <= 0 || colx+c > Icols
continue;
elseif I(rowx+r,colx+c) == 1 && s(r,c)==1
i = 1;
end
end
end
These codes must be widen this image:
however, in some point it doesn't work properly s.t:
If you help me fix my code I would be appriciated. If you want to know about dilation you can follow this url: http://www.mathworks.com/help/toolbox/images/f18-12508.html
Matlab has this function in its library but i need to implement my own function.
You should avoid loops as much as possible in matlab.
If you need to write your own function just do:
s=ones(3);
i=(conv2(double(binaryImage),s,'same')>0)
From your example:
I can obtain:
I will give a hint then. Ask yourself what int8() exactly does for a number larger then 127. Incidentally the column index number after which your algorithm start behaving weird.
Edit to clarify
If you subtract a int8 type number from another one, a double in this case, Matlab will automatically cast to int8. For example:
test = double(140) - int8(3)
Gives a 127.
I assume imdilate is implemented with conv2, but your code would be more readable if you used this:
b = imdilate(bwImage,ones(3));
So I have one problem with my code, which is that one variable is not working as it's supposed to do. Here are the codes I'm using:
format long
f = inline('-x.^2');
for i = 0:10
[I(i+1) h(i+1) tid(i+1)] = trapets(f,0,1,2^i);
end
for i = 0:10
trunk(i+1) = I(i+1) - log(2);
end
hold on
grid on
plot(log(h),log(trunk),'r+')
t = -7:0;
c = polyfit(log(h),log(trunk),1);
yy = polyval(c,t);
plot(t,yy)
grid off
hold off
koefficienter = real(c)
and also this:
function [ I,h,tid ] = trapets(f,a,b,n )
h=(b-a)/n;
tic;
I=(f(a)+f(b));
for k=2:2:n-2
I = I+2*f(a+k*h);
end
for k = 1:2:n-1
I = I + 4*f(a+k*h);
end
I = I * h/3;
tid = toc;
end
So the problem here is that the variable I is not changing value. It gets 11 values when I run the first code (I don't run the last code I wrote, it's only used by the first one), but the values are all the same. I don't know if the problem is that the variable n, which I use in the the second code, never change value, although I'm trying to do that with the "2^i" part in "trapets(f,0,1,2^i)". If the case is that n never change value, is there a solution to do that?
And if the problem is not the variable n, why doesn't the variable I change value in the code?
After running your program I found out the I always equals -1/h after the for loops, which makes I = I * h/3; always give you the same result.
I have an extremely bizzare situation: I have a function in MATLAB which calls three other main functions and produces two figures for me. The function reads in an input jpeg image, crops it, segments it using kmeans clustering, and outputs 2 figures to the screen - the original image and the clustered image with the cluster centers indicated. Here is the function in MATLAB:
function [textured_avg_x photo_avg_x] = process_database_images()
clear all
warning off %#ok
type_num_max = 3; % type is 1='texture', 2='graph', or 3='photo'
type_num_max = 1;
img_max_num_photo = 100; % 400 photo images
img_max_num_other = 100; % 100 textured, and graph images
for type_num = 1:2:type_num_max
if(type_num == 3)
img_num_max = img_max_num_photo;
else
img_num_max = img_max_num_other;
end
img_num_max = 1;
for img_num = 1:img_num_max
[type img] = load_image(type_num, img_num);
%img = imread('..\images\445.jpg');
img = crop_image(img);
[IDX k block_bounds features] = segment_image(img);
end
end
end
The function segment_image first shows me the color image that was passed in, performs kmeans clustering, and outputs the clustered image. When I run this function on a particular image, I get 3 clusters (which is not what I expect to get).
When I run the following commands from the MATLAB command prompt:
>> img = imread('..\images\texture\1.jpg');
>> img = crop_image(img);
>> segment_image(img);
then the first image that is displayed by segment_image is the same as when I run the function (so I know that the clustering is done on the same image) but the number of clusters is 16 (which is what I expect).
In fact, when I run my process_database_images() function on my entire image database, EVERY image is evaluated to have 3 clusters (this is a problem), whereas when I test some images individually, I get in the range of 12-16 clusters, which is what I prefer and expect.
Why is there such a discrepancy? Am I having some syntax bug in my process_database_images() function? If more code is required from me (i.e. segment_images function, or crop_image function), please let me know.
Thanks.
EDIT:
I found the source of the problem. In my load_image function, after I call img = imread(filename), I convert the image to double: `img = im2double(img);'. When I comment this line out, I get the desired result. Anyone know why this happens? (and also how I can 'close' this question since I have located the problem).
clear all at the top of your function is unnecessary and may be the source of your trouble.
Also, turning off all warnings is a bad idea since it may mask other problems.
Let's look at this code, simplified by removing redundant code or unused code:
function [textured_avg_x photo_avg_x] = process_database_images()
type_num_max = 1;
img_max_num_photo = 100; % 400 photo images
img_max_num_other = 100; % 100 textured, and graph images
for type_num = 1:2:type_num_max %% 1:2:1 => 1
img_num_max = 1; %This nullfiies everything in the if block above anyways
for img_num = 1:img_num_max %% 1:1 => 1
[type img] = load_image(type_num, img_num); %% Input (1,1)
img = crop_image(img);
[IDX k block_bounds features] = segment_image(img);
end
end
end
It looks like this code runs through the double nested for loop exactly once, maybe that is why you get only one answer, three clusters.
Try calling your function on the command line with the same amount of return values as in the function you wrote. Instead of
>> segment_image(img);
Try:
>> [IDX k block_bounds features] = segment_image(img);
Functions in Matlab check how many return values are expected, and may behave differently depending on that.