MATLAB Answers

How to get all yellow piexels that interact with the blue piexels?

1 view (last 30 days)
MCC on 1 Apr 2021
Commented: MCC on 6 Apr 2021
Hello everybody,
I have one image that was plotted by imagesc. I need to get all yellow piexels that close to the blue piexels. I have indicated them in the figure. The value of blue piexel is 0. I can use it to filter the piexels. But I am not sure how to write this code. Any ideas? I have attched the code and data.
data = load('myFile.txt');
xlabel('X (\mum)','FontName','Times New Roman','FontSize',14);
ylabel('Y (\mum)','FontName','Times New Roman','FontSize',14);
caxis([min1 max1]);

Accepted Answer

Image Analyst
Image Analyst on 5 Apr 2021
Try this, using bwboundaries() from the Image Processing Toolbox:
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 14;
data = load('myFile.txt');
subplot(3, 1, 1);
hold on;
xlabel('X (\mum)','FontName','Times New Roman','FontSize',fontSize);
ylabel('Y (\mum)','FontName','Times New Roman','FontSize',fontSize);
caxis([min1 max1]);
axis('on', 'image');
subplot(3, 1, 2);
histogram(data, 32);
grid on;
xlabel('Data Value','FontSize',fontSize);
% Create a mask so we can get the boundaries of it.
mask = data > 50;
subplot(3, 1, 3);
% 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.
imshow(data); % Optional : show the original image again. Or you can leave the binary image showing if you want.
% 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
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is x.
% First plot over original.
subplot(3, 1, 1);
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
% Next plot over bottom mask image.
subplot(3, 1, 3);
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
% Maximize window
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m\n', mfilename);fprintf('Done running %s.m\n', mfilename);

More Answers (1)

darova on 2 Apr 2021
What about contour?
[m,n] = size(data);
[x,y] = meshgrid(1:m,1:n);
[C,h] = contour(x,y,data,[0.9 0.9]);
h1 = get(h,'children');
x1 = get(h1(1),'xdata');
y1 = get(h1(1),'ydata');
MCC on 5 Apr 2021
Index exceeds array bounds.
Error in MarginCapture (line 5)
x1 = get(h1(1),'xdata');

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!