Method for calculating pixels in the distance varying image?
2 views (last 30 days)
Show older comments
NAVEEN MANGAL
on 2 Jul 2017
Commented: Image Analyst
on 2 Jul 2017
I am designing a rover for fields. The rover will move in a field and can turn it's direction once it detects the arrow signboard. For this, I am using a simple web camera and capturing the images in regular time intervals. Now after converting these images into binary (after converting into binary my background is black and foreground is white), I am calculating a number of white pixels in regular intervals, but I am unable to change my rover's direction from this method. Please tell is this approach is good or suggest other methods for this task. Thanks in advance.
0 Comments
Accepted Answer
Image Analyst
on 2 Jul 2017
Why can't you just send a turn right or left signal via the RF communication? Why does it have to look at signs with arrows on them and do image analysis of them?
If you did need to do image analysis, just compute the orientation and split the blob in two and find out which side has more pixels on it. That side will be the side with the arrowhead on it. By the way, you forgot to attach a picture of your arrow.
2 Comments
Image Analyst
on 2 Jul 2017
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = pwd;
baseFileName = 'arrow.jpg';
% Get the full filename, with path prepended.
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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
% 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')
% Compute the binary image.
binaryImage = grayImage < 254;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Label the image
[labeledImage, numBlobs] = bwlabel(binaryImage);
% If more than one blob, extract the largest one.
if numBlobs > 1
binaryImage = bwareafilt(binaryImage, 1);
% Label the image
[labeledImage, numBlobs] = bwlabel(binaryImage);
% If more than one blob, extract the largest one.
end
props = regionprops(labeledImage, 'Centroid', 'MajorAxisLength');
xCentroid = props.Centroid(1)
yCentroid = props.Centroid(2)
MajorAxisLength = props.MajorAxisLength
hold on;
plot(xCentroid, yCentroid, 'r+', 'MarkerSize', 30);
line([xCentroid - MajorAxisLength/2, xCentroid + MajorAxisLength/2], [yCentroid, yCentroid], ...
'LineWidth', 2, 'Color', 'r');
% To determine if the arrow is pointing reight or left,
% find the width of the blob a distance to the right or left,
% and see which is wider.
distanceRatio = 0.9;
x1 = round(xCentroid - (MajorAxisLength/2) * distanceRatio)
x2 = round(xCentroid + (MajorAxisLength/2) * distanceRatio)
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
line([x1, x1], [1, rows], 'LineWidth', 2, 'Color', 'r');
line([x2, x2], [1, rows], 'LineWidth', 2, 'Color', 'r');
% Get widths
y1Top = find(binaryImage(:, x1), 1, 'first');
y1Bottom = find(binaryImage(:, x1), 1, 'last');
leftWidth = abs(y1Bottom - y1Top)
y2Top = find(binaryImage(:, x2), 1, 'first');
y2Bottom = find(binaryImage(:, x2), 1, 'last');
rightWidth = abs(y2Bottom - y2Top)
if leftWidth < rightWidth
message = sprintf('Left width = %d, right width = %d,\nso the arrow is pointing left.', ...
leftWidth, rightWidth);
msgbox(message);
else
message = sprintf('Left width = %d, right width = %d,\nso the arrow is pointing right.', ...
leftWidth, rightWidth);
msgbox(message);
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/185077/image.png)
More Answers (0)
See Also
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!