I am confused what is wrong with my code

1 次查看(过去 30 天)
move = input('Enter the name of the image: ', 's');
function [board_game1] = AnalyzeScreenshot(name)
pic = imread(move);
board_game1 = zeros(4,4);
[p,q,r] = size(pic);
binary = false(p,q);
for ii = 1:p %locating board
for jj = 1:q
if pic(ii,jj,1) == 187&&pic(ii,jj,2) == 173&&pic(ii,jj,3) ==160
binary(ii,jj) =1;
x = ii;
y = jj;
break
end
end
if binary(ii, jj) == 1
break
end
end
play = imread(binary); %fill the matrix with the tile values
bhor = 1;
bver = 1;
for ii = 19:121:380
bhor = 1;
for jj = 10:121:373
if play(ii,jj,1) == 238 && game(ii,jj,2) == 228 && game(ii,jj,3) == 218
square = 2;
board_game1(bhor,bver) = 2;
elseif play(ii,jj,1) == 237 && game(ii,jj,2) == 224 && game(ii,jj,3) == 200
square = 4;
board_game1(bhor,bver) = 4;
elseif game(ii,jj,1) == 242 && game(ii,jj,2) == 177 && game(ii,jj,3) == 121
square = 8;
board_game1(bhor,bver) = 8;
elseif game(ii,jj,1) == 245 && game(ii,jj,2) == 149 && game(ii,jj,3) == 99
square = 16;
board_game1(bhor,bver) = 16;
elseif game(ii,jj,1) == 246 && game(ii,jj,2) == 124 && game(ii,jj,3) == 95
square = 32;
board_game1(bhor,bver) = 32;
elseif game(ii,jj+5,1) == 237 && game(ii,jj+5,2) == 207 && game(ii,jj+5,3) == 120
square = 128;
board_game1(bhor,bver) = 128;
elseif game(ii,jj+5,1) == 237 && game(ii,jj+5,2) == 204 && game(ii,jj+5,3) == 97
square = 256;
board_game1(bhor,bver) = 256;
elseif game(ii,jj+5,1) == 237 && game(ii,jj+5,2) == 200 && game(ii,jj+5,3) == 80
square = 512;
board_game1(bhor,bver) = 512;
elseif game(ii,jj+5,1) == 237 && game(ii,jj+5,2) == 197 && game(ii,jj+5,3) == 63
square = 1024;
board_game1(bhor,bver) = 1024;
else
board_game1(bhor,bver) = 0;
end
if bhor == 4 %input the correct places in matrix
bver = 3;
end
bhor = bhor+1;
end
if bver == 4
bver = 3;
end
bver = bver +1;
board_game1 = board_game1(bhor,bver);
end
disp(board_game1);
end
  1 个评论
Daniel
Daniel 2023-4-27
This question can't be answered without more information. For instance, what was the error? What was the command that invoked the error?
The code provided is also a mix of function definitions and script commands (since the first line is not in the form "function [outputs] = function(inputs)", but that syntax appears later). So it can't be pasted directly into a .m file and can't be executed directly from the console.

请先登录,再进行评论。

回答(2 个)

KSSV
KSSV 2023-4-27
You need to input the image move to the function.
move = input('Enter the name of the image: ', 's');
[board_game1] = AnalyzeScreenshot(move) ;
function [board_game1] = AnalyzeScreenshot(move) %<--- chage the variable name to move
pic = imread(move);
board_game1 = zeros(4,4);
[p,q,r] = size(pic);
binary = false(p,q);
for ii = 1:p %locating board
for jj = 1:q
if pic(ii,jj,1) == 187&&pic(ii,jj,2) == 173&&pic(ii,jj,3) ==160
binary(ii,jj) =1;
x = ii;
y = jj;
break
end
end
if binary(ii, jj) == 1
break
end
end
% play = imread(binary); %fill the matrix with the tile values %<-----
% what is this? Why you are reading a binary matrix which is intilaized?
play = binary ;
bhor = 1;
bver = 1;
% The variable game is not defined wny where....is it a function?
for ii = 19:121:380
bhor = 1;
for jj = 10:121:373
if play(ii,jj,1) == 238 && game(ii,jj,2) == 228 && game(ii,jj,3) == 218
square = 2;
board_game1(bhor,bver) = 2;
elseif play(ii,jj,1) == 237 && game(ii,jj,2) == 224 && game(ii,jj,3) == 200
square = 4;
board_game1(bhor,bver) = 4;
elseif game(ii,jj,1) == 242 && game(ii,jj,2) == 177 && game(ii,jj,3) == 121
square = 8;
board_game1(bhor,bver) = 8;
elseif game(ii,jj,1) == 245 && game(ii,jj,2) == 149 && game(ii,jj,3) == 99
square = 16;
board_game1(bhor,bver) = 16;
elseif game(ii,jj,1) == 246 && game(ii,jj,2) == 124 && game(ii,jj,3) == 95
square = 32;
board_game1(bhor,bver) = 32;
elseif game(ii,jj+5,1) == 237 && game(ii,jj+5,2) == 207 && game(ii,jj+5,3) == 120
square = 128;
board_game1(bhor,bver) = 128;
elseif game(ii,jj+5,1) == 237 && game(ii,jj+5,2) == 204 && game(ii,jj+5,3) == 97
square = 256;
board_game1(bhor,bver) = 256;
elseif game(ii,jj+5,1) == 237 && game(ii,jj+5,2) == 200 && game(ii,jj+5,3) == 80
square = 512;
board_game1(bhor,bver) = 512;
elseif game(ii,jj+5,1) == 237 && game(ii,jj+5,2) == 197 && game(ii,jj+5,3) == 63
square = 1024;
board_game1(bhor,bver) = 1024;
else
board_game1(bhor,bver) = 0;
end
if bhor == 4 %input the correct places in matrix
bver = 3;
end
bhor = bhor+1;
end
if bver == 4
bver = 3;
end
bver = bver +1;
board_game1 = board_game1(bhor,bver);
end
disp(board_game1);
end

Walter Roberson
Walter Roberson 2023-4-27
move = input('Enter the name of the image: ', 's');
That defines a variable inside the "base" workspace.
function [board_game1] = AnalyzeScreenshot(name)
That starts the definition of a function named AnalyzeScreenshot that accepts at most one input parameter, an which returns at most one output parameter.
This function line does not invoke the function, it only starts the definition of the function. It especially does not invoke the function passing in the value of move
pic = imread(move);
This is inside the function, and you are trying to use the value of move . But move has not been assigned a value inside the function, and move is not the name of any parameter passed into the function, and move is not the name of any shared variable (shared variables only apply to nested functions.) So move is not a variable known to the function. MATLAB will then look to see whether there is a function named move that should be invoked at this point; it will probably not find such a function. MATLAB would therefore complain about unknown function or variable move

类别

Help CenterFile Exchange 中查找有关 System Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by