Basic: calling a function

2 次查看(过去 30 天)
I have a probably very basic question:
I have made a function file in matlab and want to call it within my script file. The script is not finished, so I just need to consentrate to get the function "central_difference" working for now. I get an error message saying that the variable 'greyimg' is not defined when I call central_difference. I don´t understand why, since it is defined....? And when the function works, should it not also be able to plot the subplots 152-154
Code here:
Script file:
clc;
clear all;
gridimg = im2double(imread('grid.jpg'));
grayimg = rgb2gray(gridimg);
[Ix,Iy,Im] = central_difference(greyimg);
[x,y,theta] = extract_edges(Ix, Iy, Im, edge_threshold);
figure(6);
set(gcf,'Position',[100 100 1000 300])
subplot(151); imshow(img_blur); xlim([300, 500]); title('Blurred input');
subplot(152); imshow(Ix, [-0.05, 0.05]); xlim([300, 500]); title('Gradient in x');
subplot(153); imshow(Iy, [-0.05, 0.05]); xlim([300, 500]); title('Gradient in y');
subplot(154); imshow(Im, [ 0.00, 0.05]); xlim([300, 500]); title('Gradient magnitude');
Function file:
function [Ix, Iy, Im] = central_difference(Img)
Ix = zeros(size(Img)); % Placeholder
Iy = zeros(size(Img)); % Placeholder
Im = zeros(size(Img)); % Placeholder
kernel = [1/2 0 -1/2];
for i=1: size(Img, 1) %alle rader i grayimg
Ix(i, :) = conv(Img(i,:), kernel, "same"); %conv med kernel
end
for j=1: size(Img, 2) %alle kolonner i grayimg
Iy(:, j) = conv(Img(:,j), kernel, "same");
end
Im = sqrt(Ix.^2 + Iy.^2);
end

采纳的回答

Cris LaPierre
Cris LaPierre 2021-1-25
编辑:Cris LaPierre 2021-1-25
The error is telling you that greyimg does not exist. When you create the variable, you call it grayimg (with an a instead of e)
grayimg = rgb2gray(gridimg);
^ % gray
[Ix,Iy,Im] = central_difference(greyimg);
^ % grey
  1 个评论
Stina Ravdna Lorås
And so basic could it actually be :D
Obviously too tired. Thought I had misunderstod the consept of functions. Thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by