Image analysis - line to value data plotting

5 ビュー (過去 30 日間)
BG Kim
BG Kim 2016 年 5 月 9 日
コメント済み: Image Analyst 2016 年 5 月 11 日
How can I open the image (jpg, etc) at the MATLAB? How can I get the line data value from the image like below? I want to plot the data of the line in the image. Normalized data is okay.

回答 (2 件)

Alessandro Masullo
Alessandro Masullo 2016 年 5 月 9 日
You can use imread to read the image. If your image is already normalized, you can use find to get the elements of the line. Being the line black on a white background, you need to invert it by using ~.
im = imread(...);
f = find(~im);
[y,x] = sub2ind(...)
  2 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 10 日
You can do
[rows, columns] = find(im < 128);
and skip the unneeded sub2ind() function (which was wrong anyway because it should have been ind2sub), but you accepted this Answer so did you finally get it working?


Image Analyst
Image Analyst 2016 年 5 月 10 日
Is this really what you want:
% Get coordinates of a black line in a white image.
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;
% Get the base filename.
baseFileName = 'data.jpg'; % Assign the one on the button that they clicked on.
% Get the full filename, with path prepended.
folder = pwd; % Current folder
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
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);
% Read in image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = min(grayImage, [], 3); % Take green channel.
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
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')
% Let's compute and display the histogram.
subplot(2, 2, 2);
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
% Threshold to find dark.
binaryImage = grayImage < 128;
% Blobs must be at least a certain number of pixels.
binaryImage = bwareaopen(binaryImage, 30);
% Display the binary image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
hold on; % So we can draw boxes.
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Go across column by column, getting the average row value.
rowValues = zeros(1, columns); % Initialize
for col = 301 : columns
thisColumn = binaryImage(:, col);
rows = find(thisColumn);
if ~isempty(rows)
rowValues(col) = mean(rows);
subplot(2, 2, 4);
plot(rowValues, 'b-', 'LineWidth', 2);
grid on;
  2 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 11 日
I give you that. They're in the variable rowValues. The elements (index) of rowValues are the column numbers, and rowValues is the row in the image where the line is for that column. Like for column 123, the line is at rowValues(123).



Find more on Interactive Control and Callbacks 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!

Translated by