Camera-to-circled object distance finder

2 views (last 30 days)
Hi, I have just wanted to detect the radius of the circle but I have only blue value and it is not clearly removing the other rgb colors. How may I detect the specified filter for it to detect the color that I wanted in attached image. Any help would be appreciated.
rgb = imread('A2.jpeg');
J = imrotate(rgb,180);
subplot(2, 2, 1);
imshow(J);
impixelinfo;
title('Original RGB Image')
[r, g, b] = imsplit(J);
subplot(2, 2, 2);
imshow(r);
impixelinfo;
title('Blue Channel')
mask =b >140 ;
subplot(2, 2, 3);
imshow(mask);
impixelinfo;
title('Initial Mask Image')
mask = imclearborder(mask);
mask = bwareafilt(mask, 1);
subplot(2, 2, 4);
imshow(mask);
impixelinfo;
title('Final Mask Image')
props = regionprops(mask, 'Area', 'EquivDiameter', 'BoundingBox', 'Centroid');
area = props.Area
diameter = props.EquivDiameter
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(props)
bb = props(object).BoundingBox;
bc = props(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
Also when I wanted to go with IMG-0134.jpg., it gives me perfect solution. But I wanted to continue with the A2.jpeg.

Accepted Answer

Image Analyst
Image Analyst on 5 Feb 2023
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'A2.jpeg';
folder = pwd;
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
g = gcf;
g.WindowState = "maximized";
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%--------------------------------------------------------------------------------------------------
% Mask the image
[mask,maskedRGBImage] = createMask(rgbImage)
% Take the largest blob.
mask = bwareafilt(mask, 1);
% Fill any holes.
mask = imfill(mask, 'holes');
% Display image.
subplot(2, 2, 2);
imshow(mask, []);
impixelinfo;
axis on;
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
drawnow;
%--------------------------------------------------------------------------------------------------
% Get diameter
props = regionprops(mask, 'EquivDiameter', 'Centroid');
diameter = props.EquivDiameter
xCentroid = props.Centroid(1);
yCentroid = props.Centroid(2);
% Display circle over image.
subplot(2, 2, 3);
imshow(rgbImage, []);
impixelinfo;
axis on;
viscircles([xCentroid, yCentroid], diameter/2)
caption = sprintf('Equivalent Circular Diameter = %.1f pixels', diameter);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%==========================================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 05-Feb-2023
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.520;
channel1Max = 0.889;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.597;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
  3 Comments
Image Analyst
Image Analyst on 5 Feb 2023
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
Batuhan Istanbullu
Batuhan Istanbullu on 5 Feb 2023
I did it, when you come to Istanbul. Please let me know, I need to buy you some coffee!

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!