How to extract a vector from an image?
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2017-2-21
Do you mean that you want to do optical character recognition to read the '43.6406' ?
采纳的回答
Image Analyst
2017-3-10
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;
更多回答(2 个)
Walter Roberson
2017-2-21
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 个评论
Walter Roberson
2017-3-10
Try thresholding in the bounding box of the text and inverting to turn the text from black to white.
Image Analyst
2017-3-10
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!