getting error while executing matlab code?

I will be using this above photo in my code . I have saved it in my computer with name "toys.jpg"
i have also attached error snaphot
My code is as below
clc;clear all;close all;
%Making matrix for 4 colors
MatrixR=[0,0,0,0,1,1,1,1];
MatrixG=[0,0,1,1,0,0,1,1];
MatrixB=[0,1,0,1,0,1,0,1];
%B,Blue,G,C,R,P,Y,W
RGBMatrix=uint8(255*((cat(3,MatrixR,MatrixG,MatrixB))));
%Converting these matrix to Gray
GrayVersion=((rgb2gray(RGBMatrix)));
%Plotting
subplot 211
imshow(RGBMatrix,[0 255]);
title('RGB matrix of toys color');
subplot 212
imshow(GrayVersion,[0 255]);
title('Grayscale mapping of RGB matrix');
%Reading ToyImage
ToyImage=uint8(imread('toys.jpg'));
%Placing Cursor
dcm_obj1 = datacursormode(figure);
imshow(ToyImage,[]);
datacursormode on
fprintf('Select the toy from the Image Displayed\n');
waitforbuttonpress;
c_info = getCursorInfo(dcm_obj1);
position=c_info.Position; %extracting position for finding toy
grayValueFromImage=double(ToyImage(position(2),position(1)));
%Formula
red=abs(double(GrayVersion(5))-grayValueFromImage); green=abs(double(GrayVersion(3))-grayValueFromImage);
blue=abs(double(GrayVersion(2))-grayValueFromImage); yellow=abs(double(GrayVersion(7))-grayValueFromImage);
%Condition
minimumAmongArray=min([red,blue,green,yellow]);
fprintf('At Position \n');
disp(position);
if(minimumAmongArray==red)
fprintf('The selected toy has Red Color\n');
end
if(minimumAmongArray==green)
fprintf('The selected toy has Green Color\n');
end
if(minimumAmongArray==blue)
fprintf('The selected toy has Blue Color\n');
end
if(minimumAmongArray==yellow)
fprintf('The selected toy has Yellow Color\n');
end

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 5월 16일

0 개 추천

The line
dcm_obj1 = datacursormode(figure);
will only return a struct if you have selected a datatip. However, in the case of your image, there is no datatip. If you are trying to find the position of last mouse click, then you can do it like this
ax = gca;
ax.CurrentPoint

댓글 수: 5

ABTJ
ABTJ 2020년 5월 16일
Thanks for your kind answer but I am again getting error
Attempt to reference field of non-structure array.
Error in Untitled (line 21)
ax.CurrentPoint
Ameer Hamza
Ameer Hamza 2020년 5월 16일
편집: Ameer Hamza 2020년 5월 16일
Oh! I just noticed that you are using R2011b. I think you can try this line
ax = gca;
get(ax, 'CurrentPoint')
ABTJ
ABTJ 2020년 5월 16일
Now i am getting error
Undefined function 'getCursorInfo' for input arguments of type
'double'.
Error in Untitled4 (line 26)
c_info = getCursorInfo(ax);
Where initially at start of this question,there was dcm_obj1, but i changed that to ax on your advice
You don't need those lines. Replace the lines of code to find the position of the cursor in your current code with the lines I suggested. Specifically delete these lines
c_info = getCursorInfo(dcm_obj1);
position=c_info.Position; %extracting position for finding toy
and replace it with
ax = gca;
cp = get(ax, 'CurrentPoint')
position = cp(1, 1:2)
ABTJ
ABTJ 2020년 5월 16일
thank you so much dear

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Red에 대해 자세히 알아보기

제품

릴리스

R2011b

질문:

2020년 5월 16일

댓글:

2020년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by