필터 지우기
필터 지우기

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

조회 수: 13 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by