Elena Novoselova asked . 2023-02-06

How to select one area from several in a logical array (mask)?

I have a 3D logical array where is several spatial areas marked as "true" or "1". 1st dimension is latitude, 2nd – longitude, 3d – depth. I need to choose only one area which contains a preassigned point in space. This point is always inside one of the areas. As a result, I should get the similar logical array, which has only one area with values "1".
 
I think the first problem is to find the border for each area. I tried to use "alphaShape" for it but I’m not sure that I actually need it. Besides, I had a problem with it because my areas stick together into one. I have added a data file in the hope that it will make my question clearer.
 
 
shp = alphaShape(lon3(mask), lat3(mask), z3(mask), 1);
plot(shp)

% lat3, lon3, z3 - 3d array of latitudes, longitudes and depths
% mask - logical 3d array with several spatial areas
% core_lat, core_lon, core_z - coordinates of preassigned point (red cross
% in picture) (in data)
An example of data is shown in the figure below. The figure above shows a vertical section along the latitude of 69.63 N, the figure below shows the distribution at the depth of 417 meters. Yellow is several areas, from which I need to select one. Red cross is preassigned point.

several-in-a-logical-array

 

 

alphashape , poly2mask , Image Processing and Computer Vision , Image Processing Toolbox , Image Seg

Expert Answer

Neeta Dsouza answered . 2024-12-21 15:05:57

The demo uses an image from the Image Processing Toolbox. You can replace the mask variable with your binary matrix and define the target with your (x,y) target value.
 
Generate inputs
  • mask is a 2D binary matrix (true|false values)
  • target is the (x,y) coordinates of the target that lies within one of the objects in the image.
RGB = imread('pillsetc.png'); % image processing toolbox
mask = imbinarize(rgb2gray(RGB));
target = [430,170];
Find object closest to the target
  1. The closest true pixel in the image to the target is identified using pdist2.
  2. Each group of true values in the binary image is assigned a group number using bwlabel.
  3. The group number that belongs to the group closest to the target is stored in targetGroup.
  4. All other groups of true values in the binary matrix are removed.

 

[rownum, colnum] = find(mask); 
[~,I] = pdist2([colnum,rownum],target,'cityblock','smallest',1);

maskLbl = bwlabel(mask);
targetGroup = maskLbl(rownum(I),colnum(I));

maskClean = mask;
maskClean(maskLbl ~= targetGroup) = false;
Plot results
 
The original image and the cleaned image are plotted along with the target location (x) and the closest true pixel (o).
 
 
figure()
tiledlayout(1,2)
nexttile
imagesc(mask)
colormap(parula(2))
hold on
plot(target(1), target(2), 'rx', 'DisplayName', 'target')
plot(colnum(I),rownum(I), 'ro', 'DisplayName', 'Nearest true value')
title('full binary image')
axis tight
axis equal

nexttile
imagesc(maskClean)
colormap(parula(2))
hold on
plot(target(1), target(2), 'rx', 'DisplayName', 'target')
plot(colnum(I),rownum(I), 'ro', 'DisplayName', 'Nearest true value')
title('trimmed binary image')
axis tight
axis equal
lg = legend();
lg.Layout.Tile = 'south';

several-in-a-logical-array

 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.