%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

Adam Danz
Adam Danz 2019년 4월 8일
The full error message, copy-pasted from the command window, will be useful.
saeed ahmed
saeed ahmed 2019년 4월 8일
Error in Imageprocessing (line 63)
[x,y,d,RotAngle]=Test(a,M_Avg,K_Avg,v);
This is the error
madhan ravi
madhan ravi 2019년 4월 8일
No! that's not the complete error message.
saeed ahmed
saeed ahmed 2019년 4월 8일
v =
453.5000
Index exceeds array bounds.
Error in Test (line 26)
center=props(1).Centroid;
Error in Imageprocessing (line 63)
[x,y,d,RotAngle]=Test(a,M_Avg,K_Avg,v);
This is all the command window
Adam Danz
Adam Danz 2019년 4월 8일
편집: Adam Danz 2019년 4월 8일
There we go. This is the clue we needed: Index exceeds array bounds. And now we know the line that is producing the error, too.
saeed ahmed
saeed ahmed 2019년 4월 8일
how can i solve it please
Image Analyst
Image Analyst 2019년 4월 9일
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
saeed ahmed 2019년 4월 10일
sorry for late reply , this is the result and what i need is to put a red tap on table and detect the object place over this tap untitled3.jpg

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

답변 (1개)

Adam Danz
Adam Danz 2019년 4월 8일

0 개 추천

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

saeed ahmed
saeed ahmed 2019년 4월 8일
v =
351.5000
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Error in Test (line 26)
center=props().Centroid;
Error in Imageprocessing (line 63)
[x,y,d,RotAngle]=Test(a,M_Avg,K_Avg,v);
still the same
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
saeed ahmed 2019년 4월 8일
more explanation please
Adam Danz
Adam Danz 2019년 4월 8일
편집: Adam Danz 2019년 4월 8일
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. 190408 161657-someone please check my error - MATLAB Answers - MATLAB Central.jpg
saeed ahmed
saeed ahmed 2019년 4월 8일
Yes because i want to detect the centroid and orientation of object , so how can i fix this command ?
Adam Danz
Adam Danz 2019년 4월 8일
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
Walter Roberson 2019년 4월 8일
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
saeed ahmed 2019년 4월 8일
Yes i use to detect object on a circle workspace and i have to get its coordinates using a camera and next step is to give this X and Y and orientation to motors to have an action
Walter Roberson
Walter Roberson 2019년 4월 8일
And what are min(labelled(:)) and max(labelled(:)) ?
saeed ahmed
saeed ahmed 2019년 4월 8일
Depends on the object with the red color since it will make a rectangle boundary
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
saeed ahmed 2019년 4월 9일
Ok let me try it now and send back the window command message
saeed ahmed
saeed ahmed 2019년 4월 9일
after adding this commands
it work
v =
527.5000
ans =
2 1
ans =
0
ans =
2
ans =
42336
ans =
85763
angle =
4.9003
Adam Danz
Adam Danz 2019년 4월 9일
편집: Adam Danz 2019년 4월 9일
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
saeed ahmed 2019년 4월 9일
i think the problem depends on the light intesity since i increase light to object the code doesnt bug or any error appear but when i decrease light intensity the problem appears
saeed ahmed
saeed ahmed 2019년 4월 10일
any suggestion for solving this problem ??

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

질문:

2019년 4월 8일

댓글:

2019년 4월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by