필터 지우기
필터 지우기

Basic: calling a function

조회 수: 2 (최근 30일)
Stina Ravdna Lorås
Stina Ravdna Lorås 2021년 1월 25일
댓글: 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 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