Printing row of maximum value of a column

7 次查看(过去 30 天)
Hi.
I am processing image data for an experiment I have where i try to track the centroid of laser beam on a detector.
I was able to extract the data and process them to find the areas of all shapes in the image and to find all their centroid. The centroid has two coordinates X and Y. I have created an array that has 3 columns, where the columns are area of each shape and the coordinate of its centeroidm as following: 'Area', 'X','Y'. I have used the following code:
directory = 'C:\Users\anask\Desktop\test\';
files = dir([directory '/*.jpg']);
[~,index] = sortrows({files.date}.'); files = files(index); clear index;
auxx = [];
auxy = [];
for x = 1: numel(files)
A= imread([directory '/' files(x).name]);
%Convert the image ino binary image
Ibw = im2bw(A);
%Fill image regions and holes
Ibw = imfill(Ibw,'holes');
Ilabel = bwlabel(Ibw);
%find the centroid
stat = regionprops(Ibw,'Area','Centroid'); %Note here stat is a struct array
end
Till here I have the stat array that has Area and Centroid coorinate but for all images and I want it to apply the following processing to every image
%Creating new array that have Area, X-coordinate, Y-coordinate
area = cat(1,stat.Area)
Coor = cat(1,stat.Centroid)
%Combaining the results in one Array
Array = cat(2,area,Coor)
%Adding a header to the array
header = {'Area','x','y'};
Array = [header; num2cell(Array)];
Then I want to append in a new matrix or Array that has the whole row of max 'Area' from every image.
Can someone help me please?

回答(1 个)

Shlok
Shlok 2024-8-30,5:38
Hi Ennes,
To extract the maximum area value from each image and store it in a new array, you should preprocess this data inside the for loop when accessing each image file. Within this loop, you'll extract and append the row corresponding to the maximum "Area" value, then store it in an array. Here's how you can achieve this:
directory = 'C:\Users\anask\Desktop\test\';
files = dir([directory '/*.jpg']);
[~, index] = sortrows({files.date}.');
files = files(index);
clear index;
% Initialize an empty array to store the rows with maximum area
maxAreaArray = [];
for x = 1:numel(files)
A = imread([directory '/' files(x).name]);
Ibw = im2bw(A);
Ibw = imfill(Ibw, 'holes');
Ilabel = bwlabel(Ibw);
stat = regionprops(Ibw, 'Area', 'Centroid');
% Extract the values from ‘stat’ object
area = cat(1, stat.Area);
Coor = cat(1, stat.Centroid);
Array = cat(2, area, Coor);
% Find the row with the maximum area
[~, maxIdx] = max(area);
maxRow = Array(maxIdx, :);
% Append the row with the maximum area to the maxAreaArray
maxAreaArray = [maxAreaArray; maxRow];
end
header = {'Area', 'x', 'y'};
maxAreaArray = [header; num2cell(maxAreaArray)];
This will provide you with an array where each row corresponds to the largest shape detected in each image, including its area and centroid coordinates.

Community Treasure Hunt

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

Start Hunting!

Translated by