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.
MATLAB: Method for calculating pixels in the distance varying image
image analysisimage processingImage Processing Toolboximage segmentation
Related Solutions
It's not clear exactly what you're calling an "element". I'll use more precise terminology. If your Iregion structure array is just one structure, then you have just one blob. The Centroid field of that structure will be a 1-by-2 array with the x and y locations of the centroid of that blob.
If you have multiple blobs, then you need to add an index after the structure variable name to get just one of the blobs, try this:
props = regionprops(bw2,'centroid');hold on;for k = 1 : length(props) % Get x and y centroid of the k'th blob.
xCentroid = props(k).Centroid(1); yCentroid = props(k).Centroid(2); plot(xCentroid, yCentroid, 'r+', 'MarkerSize', 40, 'LineWidth', 2); % Let user know what they are
message = sprintf('The largest blob has area %d and a centroid at (x,y) = (%f, %f)',... props.Area, xCentroid, yCentroid) uiwait(msgbox(message));end
You can also get all the x and y centroids into arrays doing this:
centroids = [props.Centroid];xCentroids = centroids(1:2:end);yCentroids = centroids(2:2:end);
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;%===============================================================================
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwdif ~exist(startingFolder, 'dir') % If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;end% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.jpg');[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');if baseFileName == 0 % User clicked the Cancel button.
return;end% 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; endendgrayImage = 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(1, 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.04, 1, 0.96]);drawnow;% Threshold the image.
binaryImage = grayImage > 50; % Hgh enough to get rid of bad JPEG artifacts. Never use JPEG for image analysis!
% Get rid of blobs rouching the border, like that white frame.
binaryImage = imclearborder(binaryImage);% Take largest remaining blob.
binaryImage = bwareafilt(binaryImage, 1);% Display the image.subplot(1, 2, 2);imshow(binaryImage, []);title('Binary Image with line down it', 'FontSize', fontSize, 'Interpreter', 'None');% Go down image finding first and last points
hold on;minRequiredDistance = 10; % Whatever....
x = NaN(rows, 1);y = NaN(rows, 1);for row = 1: rows col1 = find(binaryImage(row,:), 1, 'first'); col2 = find(binaryImage(row,:), 1, 'last'); if col2-col1 > minRequiredDistance x(row) = (col1 + col2) / 2; y(row) = row; endendplot(x, y, 'r-', 'MarkerSize', 8, 'LineWidth', 2);helpdlg('Done!');
Best Answer