How to extract a vector from an image?

Hello,
I have this image
and I want to extract only the velocity vector from the image. Is there a code to do so?
Thank you in advance

2 Comments

Do you mean that you want to do optical character recognition to read the '43.6406' ?
Firstly I want to obtain just the vector(blue). Then yes I would like to do the optical recognition to read the value of the velocity vector.

Sign in to comment.

 Accepted Answer

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 = 15;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = '1.png';
% Get the full filename, with path prepended.
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 demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[imageRows, imageColumns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(1, 3, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, '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')
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Threshold to get the blue mask.
yellowMask = redChannel > 150 & greenChannel > 150 & blueChannel < 150;
% Fill it.
yellowMask = imfill(yellowMask, 'holes');
% Display the image.
subplot(1, 3, 2);
imshow(yellowMask);
grid on;
axis on;
title('Yellow Mask Image', 'FontSize', fontSize);
% Find boundingBox
props = regionprops(yellowMask, 'BoundingBox');
croppedImage = imcrop(rgbImage, props.BoundingBox);
% Display the image.
subplot(1, 3, 3);
imshow(croppedImage);
axis on;
caption = sprintf('Final, Cropped Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;

More Answers (2)

The blue vector is the only area in which:
  • blue is notably greater than 0;
  • and red is low
Red is low in the black sections near the edges, but blue is low for those.
Blue is notably greater than 0 for the grey and white portions, but red is not low for those.

9 Comments

@Walter Roberson Thanks for the answer. I am not familiar with working in RGB image. Do you have a code for that? Thanks a lot in advance.
img = imread('1.png');
R = img(:,:,1);
B = img(:,:,3);
r_thresh = 20; %a guess
b_thresh = 50; %a guess
mask = R < r_thresh & B > b_thresh;
mask3 = mask(:,:,[1 1 1]);
masked_image = zeros(size(img), class(img));
masked_image(mask3) = img(mask3);
Thank you very much, that worked. And what if I want to read the '43.6406'?
Thanks a lot but if I only want the numeric part?
... then you throw away everything that is not a digit or '.' and you convert the string that remains to a double?
Or you could
velocity = sscanf(TheString, 'v=%f')
which would do the conversion for you, assuming it really was able to figure out the literal 'v' and literal '=' and did not add any spacing. It is safer to assume it might have gotten those wrong.
Who created that image? Was it your code? Or someone else's? Why not just ask them to output the vector endpoints and number? If you're stuck with the annotated RGB image then you'll need to do as Walter says but only if you have hundreds of images, otherwise you'll spend less time just using ginput(2) to get the endpoints and typing the number you see into inputdlg(), unless you want to write the code just for the fun of it Actually getting the all the points along the blue line is easy with Walter's code - it's the OCR that is the hard part if you don't have the Computer Vision System Toolbox.
I have the Computer Vision System Toolbox. Also I have produced these images manually but unfortunately I didn't think of what I will do next with the text. How can I output the vector endpoints and the number? Unfortunately the ocr doesn't work it gives me completely blank char.
Try thresholding in the bounding box of the text and inverting to turn the text from black to white.

Sign in to comment.

You started with just an image, then you found the outline somehow, and used plot(x,y,'g-') to plot the green outline, and you probably used annotation() to draw the blue arrow. Then you used
vText = sprintf('v=%.4fm/s', v);
text(x, y, vText, 'Color', 'k', 'BackgroundColor', 'y');
You had to do that because the text won't just magically appear in the image. So you already know what v is. If you don't, then explain how the yellow text got there.

Categories

Find more on Image Processing and Computer Vision 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!