how to get matlab code to detect a changes from 0 to 1 in a binary mask?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have this code:
%% filametrics matlab data reader
clc;
clear;
close all;
[filename, pathname, filterindex] = uigetfile('*.txt');
pre_fullfilename = [pathname,filename];
Z1 = readmatrix(pre_fullfilename,'NumHeaderLines',3);
num_rows = size(Z1,1);
num_cols = size(Z1,2);
pixel_size = 0.49; % micron
x1 = linspace(0,num_cols*pixel_size,num_cols);
y1 = linspace(0,num_rows*pixel_size,num_rows);
[X1,Y1] = meshgrid(x1,y1);
hold on
figure(1)
h1 = surf(X1,Y1,Z1)
set(h1,'LineStyle','none')
hold off
I = mat2gray( h1.ZData , [0 1] ) ;
hold on
figure(2)
h2 = surf(I);
set(h2,'LineStyle','none');
hold off
it allows me to apply a binary mask onto a text file.
I'd like to find a way of going through that mask and plot on a graph the location where the mask goes from 1s to 0s.
I'm hoping with this plot I will be able to extract the curve of the image i am trying to analyse.
Couls anyone help me ?
1 commentaire
Jan
le 31 Mar 2022
What is the relation between the posted code and the question? Wouldn't this be an easier way to create some inputs:
x = rand(40, 40) > 0.5;
What do you want to do now?
Réponses (3)
Moksh
le 31 Oct 2023
Hi Lorenzo,
I understand that you have generated a binary mask and now you want to graph the locations where the values get changed from 1 to 0.
You can try using the “find” function in MATLAB, to get the required indexes. This function takes a Boolean condition as input and returns all the indexes satisfying the condition. You can then use the “scatter” function in MATLAB to get a scatter plot of these indexes.
Here is an example code for this on 2 randomly generated matrices:
% Generating 2 random matrices
initial_matrix = randi([0 1], 5, 5);
new_matrix = randi([0 1], 5, 5);
% Using the find function in matlab to get the required indexes
[row, col] = find(initial_matrix == 1 & new_matrix == 0);
% Displaying the changed positions
for i = 1:numel(row)
disp([num2str(row(i)) ',' num2str(col(i))]);
end
% Generating a scatter plot for these changed positions
scatter(col, row, 'filled');
title('Changed Positions');
xlabel('Column');
ylabel('Row');
For more information about the used functions please refer to the following documentation:
- "randi" - https://in.mathworks.com/help/matlab/ref/randi.html
- "find" - https://in.mathworks.com/help/matlab/ref/find.html
- "scatter" - https://in.mathworks.com/help/matlab/ref/scatter.html
I hope this information helps resolve the issue.
Best Regards,
Moksh Aggarwal
1 commentaire
DGM
le 1 Nov 2023
Modifié(e) : DGM
le 1 Nov 2023
That's not what the question was asking. There is one logical array as an input, not two. We're not looking for differences between two arrays, but transitions in one array in some undefined direction and according to some improbable rules. That's why the question is ambiguous and not really answerable, since OP never elaborated as requested.
Image Analyst
le 31 Oct 2023
If you have a single binary image, then to know where it goes to 1, from 0, simply use bwperim. If you want the rows and columns coordinates where those pixels are, you can use find.
perimImage = bwperim(binaryImage); % An image/map showing you where the change occurs.
[rows, columns] = find(perimImage); % Lists of the rows and corresponding columns where the change occurs.
0 commentaires
DGM
le 1 Nov 2023
Depends what you mean by "find pixels where the mask goes from 1 to 0"
% the mask
mask = imread('tinyblobs.png');
imshow(mask,'border','tight')
% blob pixels at the transition
ed1 = bwperim(mask);
imshow(imfuse(ed1,mask),'border','tight')
% background pixels at the transition
ed2 = imdilate(mask,strel('disk',1)) & ~mask;
imshow(imfuse(ed2,mask),'border','tight')
Maybe you literally mean "from 1 to 0"
% blob pixels leading the falling transition (vertical)
f = [1 0 -1].'; % transpose to flip orientation
ed3 = imfilter(double(mask),f)>0 & mask;
imshow(imfuse(ed3,mask),'border','tight')
% background pixels trailing the falling transition (vertical)
f = [1 0 -1].'; % transpose to flip orientation
ed4 = imfilter(double(mask),f)>0 & ~mask;
imshow(imfuse(ed4,mask),'border','tight')
... but note that it now depends what direction you're talking about.
In the latter cases, it depends how you want to handle cases where the object or gaps are 1px wide.
0 commentaires
Voir également
Catégories
En savoir plus sur Object Containers dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!