Finding green circle in image or video

8 views (last 30 days)
Zachary Steinwachs
Zachary Steinwachs on 14 May 2021
Edited: Image Analyst on 15 May 2021
EDIT: its searching for the green circle
Can someone check this to make sure it makes sense please?
Essentially I am trying to get real world locations of an object of a fixed size and shape given a single picture. There are some random numbers I have in there such as the "120" for calibrating because I did the math of how far my camera could pick up the objects as a singluar pixel and I estimated it to be around 120 feet. Obviously it couldn't in real life, but thats what I used just as a baseline. I was hoping someone could tell me if my code makes sense or it just happens to be somewhat accurate by mistake.
Most notes can be ignored due to me just using them cause I'm learning as I go, but I left them in for context just in case.
Thanks
==============================================================
I = imread('GreenCalibrationX.jpg'); %imagine upload and saves as RGB values
%I = imread('YellowPaperHD.jpg');
%%%%%%%%GreenCalibrationX has a Z direction of 4ft 8in, and an X direction of 2ft 2 in
%%%%%%%%The program outputs a Z distance of 4.3ft and X distance 2.02ft
%I = imrotate(J, -90);
imshow(I) %shows image
%W = imread('yellowdot.jpg'); %Testing Code
[rows, columns, numberOfColorChannels] = size(I); %registers the number of rows and colums of image
YellowPixCount = 0;
r = I(:,:,1); %Places RGB values into a matrix someone dumb like me can manipulate
g = I(:,:,2);
b = I(:,:,3);
%Red= r(58,198); Testing Code
%Gre= g(58,198);
%Blu= b(58,198);
for x = 1:rows; %runs for all rows
x = x+1;
for y = 1:columns; %runs for all column, meaning it hits every pixel
y = y+1;
Red = r(x-1,y-1); %Records RGB values for each pixel temperarily
Gre = g(x-1,y-1);
Blu = b(x-1,y-1);
if Red > 160 & Gre > 190 & Blu < 130 %if pixel matches color criteria
YellowPixCount = YellowPixCount +1; %counts how big the object, hopefully to translate to how far away
LastPixRow = x;
LastPixColumn = y;
if YellowPixCount == 1 %for first pixel that meets color criteria
X1 = x %writes the row and column values of the first succesful pixel
Y1 = y
else
end
else
end
end
end
YellowPixCount; %outputs yellow pixel count
LastPixRow
LastPixColumn
%Area = YellowPixCount %Just redefining; Area based on PixCount; inaccurate
%Dia = 2*(sqrt(YellowPixCount/ pi)) %Diameter based on area seems inaccurate
RowBasedDia = LastPixRow - X1
%NOTE FOR LATER 1/DISTANCE = SIZE, as distance is doubled, size is cut in
%half
ZCalibrationCoef = (columns/2016) %Calibrates pictures that have different Resolutions; Minimum (2016,1512)
DistanceZ = (120*ZCalibrationCoef)/RowBasedDia %CalibratedDistance Z, 190 WAS CHANGED FROM 121!!
XCalibrationCoef = (470/columns);
DistanceX = ((1/150)*((-columns/2)+Y1)*XCalibrationCoef*(DistanceZ/3.5)) %1 foot=150pixels 3.5feet from camera in calibration
YCalibrationCoef = (635/rows);
DistanceY = ((1/150)*((rows
/2)-X1)*YCalibrationCoef*(DistanceZ/3.5)) %1 foot=150pixels at 3.5feet from camera in calibration
===========================================================
These are the outputs it gives for this image. They aren't perfect but they seem rather close to the real life ones.
DistanceZ =
4.6154 ft
DistanceX =
1.5372 ft
DistanceY =
0.3101 ft
  1 Comment
per isakson
per isakson on 15 May 2021
To me the two statements
x = x+1;
y = y+1;
stand out as strange. Incrementing the loop index is done automatically by the for-loop.
Doc says: "Avoid assigning a value to the index variable within the loop statements. The for statement overrides any changes made to index within the loop."

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 15 May 2021
Try the camera calibration functionality in the Computer Vision Toolbox:

Categories

Find more on MATLAB Support Package for USB Webcams 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!