Basic: calling a function

1 回表示 (過去 30 日間)
Stina Ravdna Lorås
Stina Ravdna Lorås 2021 年 1 月 25 日
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
Stina Ravdna Lorås 2021 年 1 月 25 日
And so basic could it actually be :D
Obviously too tired. Thought I had misunderstod the consept of functions. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by