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 个)

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 个评论

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
Oh! I just noticed that you are using R2011b. I think you can try this line
ax = gca;
get(ax, 'CurrentPoint')
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)
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