how to get matlab code to detect a changes from 0 to 1 in a binary mask?

9 ビュー (過去 30 日間)
Lorenzo Calonghi
Lorenzo Calonghi 2022 年 3 月 31 日
回答済み: DGM 2023 年 11 月 1 日
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 件のコメント
Jan
Jan 2022 年 3 月 31 日
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?

サインインしてコメントする。

回答 (3 件)

Moksh
Moksh 2023 年 10 月 31 日
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
4,1 1,2 3,2 2,4 5,5
% 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:
I hope this information helps resolve the issue.
Best Regards,
Moksh Aggarwal
  1 件のコメント
DGM
DGM 2023 年 11 月 1 日
編集済み: DGM 2023 年 11 月 1 日
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
Image Analyst 2023 年 10 月 31 日
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.

DGM
DGM 2023 年 11 月 1 日
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.

カテゴリ

Help Center および File ExchangeObject Containers についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by