someone please check my error
Show older comments
%This Function is used to detect the objects from the lower plateform
%And also identifies its location and orientation measured from the right
%axis
clear all;
close all;
clc;
%connecting with the camera
vid=webcam;
%you can change the snapshot() with imread if you don't have a camera and want to %test your code
a=snapshot(vid);
%a=imread('testNow.jpg');
sum=0;
s1=floor(size(a(:,:,1),1)/2);
s2=floor(size(a(:,:,1),2)/2);
M=[s1 s1 s1 s1 s1];
K=[s2 s2 s2 s2 s2];
%Subtracting the green matrix from the rgb image
imgred = imsubtract(1.2*a(:,:,1),rgb2gray(a));
%median filter to remove the noise if existed
imgred = medfilt2(imgred,[3,3]);
%transforming te image into binary
imgred = im2bw(imgred, 0.18);
%removing areas below 300 pixels
imgred = bwareaopen(imgred,300);
figure,imshow(imgred)
Spacing_cols=floor(size(imgred,2)/6);
Spacing_rows=floor(size(imgred,1)/6);
for(i=1:5)
sum = sum + Spacing_cols;
N = find(imgred(:,sum));
if(size(N,1)~=0)
M(i)=N(1); % to get the numbers of rows of 1
end
end
sum=0;
for(j=1:5)
sum = sum + Spacing_rows;
L = find(imgred(sum,:));
if(size(L,2)~=0)
K(j)=L(1); % to get the numbers of Col.s of 1
if(j==3)
v = L(size(L,2))-L(1)+2.5
end
end
end
M_Avg=floor((M(1)+M(2)+M(3)+M(4)+M(5))/5);
K_Avg=floor((K(1)+K(2)+K(3)+K(4)+K(5))/5);
Coordinates=zeros(size(imgred,1),size(imgred,2));
Coordinates(M_Avg,:)=1;
Coordinates(:,K_Avg)=1;
[x,y,d,RotAngle]=Test(a,M_Avg,K_Avg,v);
figure,imshow(Coordinates | d)
hold on
text(K_Avg,10,['+ve Y-axis'],'BackgroundColor',[0 1 1]);
text(0,M_Avg,['+ve X-axis'],'BackgroundColor',[0 1 1]);
%Fuction File
function[x, y, A, angle] = Test(rgbImage, x_zero, y_zero, scale)
prod_image = rgbImage;
% Thresholding the image channel by channel to get the best result than...
%...thresholding the image at one time.
red = prod_image(:,:,1);
green = prod_image(:,:,2);
blue = prod_image(:,:,3);
I1 = im2bw(red, 0.6);
I2 = im2bw(green, 0.6);
I3 = im2bw(blue, 0.6);
% Anding the channels again
m = I1&I2&I3;
figure, imshow(m)
% Apply Median Filter to remove the noise.
A = medfilt2(m);
A = imerode(A, strel('square', 30));
% Remove all areas below 500 Pixels.
A = bwareaopen(A, 500);
% Filling the holes in the white areas
A = imfill(A, 'holes');
figure, imshow(A)
labelled = bwlabel(A);
props = regionprops(labelled, 'Centroid','Orientation');
figure, imshow(prod_image)
hold on
center=props(1).Centroid;
% Plotting the Centroid
plot(center(1), center(2), '-m+')
x = (y_zero - center(1))/(scale*10/12);
y = (x_zero - center(2))/(scale*10/12);
angle = props(1).Orientation
text(round(center(1))-50, round(center(2))+20, ['The Cenroid','(',...
num2str(round(x)), ',' , num2str(round(y)), ')'], 'BackgroundColor', [0 1 1]);
text(round(center(1))-50, round(center(2))+45, ['Rotation Angle from right horizontal',' ', num2str(angle)], 'BackgroundColor', [0 1 1]);
end
i have a problem when i start the program it gives me
Error in Imageprocessing (line 63)
[x,y,d,RotAngle]=Test(a,M_Avg,K_Avg,v);
any solution please
8 Comments
Adam Danz
on 8 Apr 2019
The full error message, copy-pasted from the command window, will be useful.
saeed ahmed
on 8 Apr 2019
madhan ravi
on 8 Apr 2019
No! that's not the complete error message.
saeed ahmed
on 8 Apr 2019
saeed ahmed
on 8 Apr 2019
Image Analyst
on 9 Apr 2019
Attach an image you snapped from your webcam so we can replace the webcam part with imread() and run the rest of the program. It's easier to debug if we have a sample image that you're having trouble with.
saeed ahmed
on 10 Apr 2019
Answers (1)
Adam Danz
on 8 Apr 2019
This line is producing the error:
center=props(1).Centroid;
and for whatever reason, I think props is empty.
>> props = [];
>> props(1).Centroid
Index exceeds the number of array elements (0). %matlab 2019a error message
Please test this by printing out the value of props. If it's not empty, please share its content here in the comments below.
16 Comments
saeed ahmed
on 8 Apr 2019
Adam Danz
on 8 Apr 2019
Maybe you didn't understand. What I'd like to see is the output of this line of code.
props = regionprops(labelled, 'Centroid','Orientation');
What is the value of props?
saeed ahmed
on 8 Apr 2019
In your function Test(), there is a line of code that looks like this
props = regionprops(labelled, 'Centroid','Orientation');
I'd like to see the value of props. 

saeed ahmed
on 8 Apr 2019
Adam Danz
on 8 Apr 2019
I want to help you but the next step is for me to understand what "props" is. I need to know it's value.
In debug mode, pause the code at that line, then evaluate that line and copy-paste its value from the command window into the comment section here. I can't help any further without that information.
Walter Roberson
on 8 Apr 2019
Also show us min(labelled(:)) and max(labelled(:))
I speculate that you have no areas with area above 500 pixels and so that the bwareaopen is removing everything.
Note that the value stored in m will be logical() data type. Are you sure you want to do a median filter on logical data?
saeed ahmed
on 8 Apr 2019
Walter Roberson
on 8 Apr 2019
And what are min(labelled(:)) and max(labelled(:)) ?
saeed ahmed
on 8 Apr 2019
Walter Roberson
on 8 Apr 2019
Take one particular input that leads you to the "Index exceeds the number of array elements (0)" problem, and run with that input. When the program stops because of the indexing error, at the command line tell us what output you get from the following commands:
size(props)
min(labelled(:))
max(labelled(:))
nnz(A)
nnz(m)
saeed ahmed
on 9 Apr 2019
saeed ahmed
on 9 Apr 2019
Adding the commands Walter listed would not have solved the problem. Do the "ans = " correspond to the questions Walter asked? Or has the problem been solved some other way? Anyway, if you have any further follow-up questions, feel free to ask them here.
saeed ahmed
on 9 Apr 2019
saeed ahmed
on 10 Apr 2019
Categories
Find more on Image Acquisition Support Packages for Hardware Adaptors (Generic Video Interface) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!