how to get the movement of an object with respect to time in MATLAB?
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I am working with a thing flexible structure, I have got images from experiments. I want to know how I can trace an edge and get its mpvement for example in terms of displacement and velocity verses time?
Image is attached.
Thanks
Tayyaba
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/841430/image.png)
1 个评论
yanqi liu
2021-12-23
yes,sir,may be upload the image list to detect object and trace the left edge as target
回答(1 个)
Image Analyst
2021-12-22
Do you have a method to locate the edge already? That would be the first step. Maybe try hough() or houghlines() if you don't have a method yet.
After that you can get the endpoints and centroid locations. Then make an array where you keep track of those 3 locations from frame to frame. Multiply by a spatial calibration factor to get distances in real world units such as millimeters. Velocity is the change in distance divided by the frame time.
7 个评论
Tayyaba Bano
2021-12-23
Thanks for your reply, I used the following code to generate the boundary. But the biundaries are not clear may be because the image has a lot of small particles. I am also attaching the image of boundary I have attained for the Im1.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/841960/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/841965/image.bmp)
grayImage = imread('Im1.bmp');
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Display the image.
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Get a binary image
mask = grayImage < 38; %imbinarize(grayImage);
% Display the mask.
subplot(2, 3, 4);
imshow(mask, []);
impixelinfo;
title('Initial Binary Image');
impixelinfo;
% Make a circle mask to get rid of corners
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
imageSizeX = columns;
imageSizeY = rows;
[columnsInImage, rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = columns/2;
centerY = rows/2;
% radius = max(centerX, centerY);
radius = 100;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
mask = mask & circlePixels;
% Fill interior holes.
mask = imfill(mask, 'holes');
props = regionprops(mask, 'Area');
allAreas = sort([props.Area], 'ascend')
mask = bwareaopen(mask,100);
subplot(2, 3, 5);
imshow(mask, []);
impixelinfo;
title('Final Binary Image');
impixelinfo;
boundaries = bwboundaries(mask);
subplot(2, 3, 6);
imshow(grayImage);
hold on;
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
plot(x, y, 'r-', 'LineWidth', 2);
end
title('Image With Boundaries')
Image Analyst
2021-12-23
编辑:Image Analyst
2021-12-23
Why are you creating a circular mask???
Does the spotty background also move or does it stay fixed?
Tayyaba Bano
2021-12-23
Because the small particles are of cicular cross section, therefore to hide them.
Image Analyst
2022-2-7
But there are small noise particles all over. You don't need a mask of that shape to get rid of them. You can use bwareafilt() and not use any mask. I still think my way is better but you're free to ignore it and use your own way.
% Demo by Image Analyst
% Initialization Steps.
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;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'slide.bmp';
grayImage = imread(fileName);
% 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.
% Extract the blue channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(3, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Original image is %d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
hold on
drawnow;
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
% Crop image
verticalProfile = mean(grayImage, 2);
subplot(3, 2, 2);
plot(verticalProfile, 1:length(verticalProfile), 'b-', 'LineWidth',2);
grid on;
title('Vertical Profile')
axis ij;
thresholdValue = 16;
row1 = find(verticalProfile > thresholdValue, 1, 'first');
row2 = find(verticalProfile > thresholdValue, 1, 'LAst');
grayImage = grayImage(row1:row2, :);
% Display the histogram.
subplot(3, 2, 2);
imhist(grayImage);
grid on;
impixelinfo;
title('Histogram', 'FontSize', fontSize);
drawnow; % Force screen to refresh immediately.
% Get a binary image
thresholdValue = 39;
xline(thresholdValue, 'Color', 'r', 'LineWidth', 2)
mask = grayImage > thresholdValue;
% For noise reduction, Fill any potential holes and take the largest blob.
mask = bwareafilt(imfill(mask, 'holes'), 1);
% grayImage = adapthisteq(grayImage, "NumTiles",[16, 16]);
% grayImage = medfilt2(grayImage, [27, 7]);
mask = edge(grayImage, "canny");
[filteredImage,estDoS] = imnlmfilt(grayImage, 'DegreeOfSmoothing', 15);
filteredImage = imtophat(filteredImage, true(3, 35));
% Display the image.
subplot(3, 2, 3);
imshow(filteredImage, []);
axis('on', 'image')
impixelinfo
lowThreshold = 17;
highThreshold = 255;
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, filteredImage)
% Get a mask for spots
spots = filteredImage > lowThreshold;
% props = regionprops(spots, 'Area');
% allAread = sort([props.Area])
% Get rid of any spots less than 1000 pixels.
spots = bwareafilt(spots, [1, 1000]);
% Erase those spots from the gray scale image.
filteredImage(spots) = 0;
imshow(filteredImage, []);
axis('on', 'image')
impixelinfo
subplot(3, 2, 4);
horizontalProfile = mean(grayImage, 1);
verticalProfile = mean(grayImage, 2);
subplot(3, 2, 4);
plot(verticalProfile, 1:length(verticalProfile), 'b-', 'LineWidth',2);
grid on;
title('Vertical Profile')
axis ij;
subplot(3, 2, 5);
plot(horizontalProfile, 'b-', 'LineWidth',2);
grid on;
title('Horizontal Profile')
xlim([1, length(horizontalProfile)])
% Display original image again.
subplot(3, 2, 6);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Outlined', 'FontSize', fontSize);
hold on
drawnow;
verticalThreshold = 20;
row1 = find(verticalProfile > verticalThreshold, 1, 'first');
row2 = find(verticalProfile > verticalThreshold, 1, 'last');
yline(row1, 'Color', 'r', 'LineWidth', 2);
yline(row2, 'Color', 'r', 'LineWidth', 2);
horizontalThreshold = 75;
col1 = find(horizontalProfile > horizontalThreshold, 1, 'first');
col2 = find(horizontalProfile > horizontalThreshold, 1, 'last');
xline(col1, 'Color', 'r', 'LineWidth', 2);
xline(col2, 'Color', 'r', 'LineWidth', 2);
% % Plot the borders of all the blobs in the overlay above the original grayscale image
% % using the coordinates returned by bwboundaries().
% % bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% subplot(3, 2, 4);
% imshow(grayImage); % Optional : show the original image again. Or you can leave the binary image showing if you want.
% hold on;
% % Here is where we actually get the boundaries for each blob.
% boundaries = bwboundaries(mask);
% % boundaries is a cell array - one cell for each blob.
% % In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% % Column 1 is rows, or y. Column 2 is columns, or x.
% numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/886930/image.png)
Tayyaba Bano
2022-2-15
Thank you very much for your kind and detailed reply.
I tried this with a litle bit change as I get error for 'imnImfilt' image filering and also with xline and yline. The error states:
"Undefined function or variable 'imnImfilt' and 'xline' or 'yline'. "
Also I tried to display the boundaries but unfortunately it did not display that. May be there is an error in the last part of the code.Moreover, the boundary needed is attached, which I have to trace over the images to get its velocity verses time.
The code is as follows:
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;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'slide1.bmp';
grayImage = imread(fileName);
% 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.
% Extract the blue channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(3, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Original image is %d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
hold on
drawnow;
% Crop image
verticalProfile = mean(grayImage, 2);
subplot(3, 2, 2);
plot(verticalProfile, 1:length(verticalProfile), 'b-', 'LineWidth',2);
grid on;
title('Vertical Profile')
axis ij;
thresholdValue = 15;
row1 = find(verticalProfile > thresholdValue, 1, 'first');
row2 = find(verticalProfile > thresholdValue, 1, 'LAst');
grayImage = grayImage(row1:row2, :);
% Get a binary image
thresholdValue = 50;
mask = grayImage > thresholdValue;
mask = bwareafilt(imfill(mask, 'holes'), 1);
% grayImage = adapthisteq(grayImage, "NumTiles",[16, 16]);
% grayImage = medfilt2(grayImage, [27, 7]);
mask = edge(grayImage, "canny");
% Display the image.
subplot(3, 2, 3);
imshow(mask, []);
axis('on', 'image')
impixelinfo
lowThreshold = 17;
highThreshold = 200;
% Get a mask for spots
spots = mask > lowThreshold;
% Get rid of any spots less than 1000 pixels.
spots = bwareafilt(spots, [1, 10000]);
% Erase those spots from the gray scale image.
filteredImage(spots) = 0;
imshow(filteredImage, []);
axis('on', 'image')
impixelinfo
subplot(3, 2, 4);
horizontalProfile = mean(grayImage, 1);
verticalProfile = mean(grayImage, 2);
subplot(3, 2, 4);
plot(verticalProfile, 1:length(verticalProfile), 'b-', 'LineWidth',2);
grid on;
title('Vertical Profile')
axis ij;
subplot(3, 2, 5);
plot(horizontalProfile, 'b-', 'LineWidth',2);
grid on;
title('Horizontal Profile')
xlim([1, length(horizontalProfile)])
%display the boundaries
boundaries = bwboundaries(filteredImage);
subplot(3, 2, 4);
imshow(grayImage);
hold on;
boundaries = bwboundaries (mask);
numberOfBoundaries = size(boundaries,1);
title('Image With Boundaries')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/895110/image.png)
Tayyaba Bano
2022-2-15
I tried some other way.But I am not getting the right boundary and also red spots.
Could you please suggest how to get rid of those spots? when I use the image filtering option and the mask for spots, as you suggested earlier. Unfortunately it gives me errors.
Thanks
grayImage = imread('slide1.bmp');
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Crop image
thresholdValue = 15;
row1 = find(verticalProfile > thresholdValue, 1, 'first');
row2 = find(verticalProfile > thresholdValue, 1, 'LAst');
grayImage = grayImage(row1:row2, :);
% Display the image.
subplot (3,2,1);
imshow(grayImage);
impixelinfo;
title('Original Image')
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Get a binary image
mask = grayImage > 110; %imbinarize(grayImage);
% Display the mask.
subplot(3, 2, 2);
imshow(mask, []);
impixelinfo;
title('Initial Binary Image');
%display the boundaries
boundaries = bwboundaries (mask);
numberOfBoundaries = size(boundaries,1);
subplot(3, 2, 3);
imshow(grayImage);
hold on;
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
plot(x, y, 'r-', 'LineWidth', 2);
end
title('Image With Boundaries')
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)