How to control the order of detected objects by region in MATLAB?

I am working on the MATLAB project, where the user will upload a scanned image of ellipse-like objects, and the program should calculate the measurements (height, width, area, etc.) of each object in the image.

I started with a threshold value that creates a binary image (black background and white independent objects). This is the BW image after the threshold:

BW image

After that, I used regionprops , since it returns most of the measurements I need, and it worked perfectly.

The problem is that the order in which the function "recognizes / detects" objects is incompatible. I added code to show the amount of each object, so that I can know which object has regionprops , which is considered as the first, and the second is the second .cc.

Code:

 % read image rgb=imread('bw'); s = regionprops(bw,'Area', 'BoundingBox', 'Eccentricity', 'MajorAxisLength', 'MinorAxisLength', 'Orientation', 'Perimeter','Centroid'); % show the number of each object imshow(bw) hold on; for k = 1:numel(s) c = s(k).Centroid; text(c(1), c(2), sprintf('%d', k), ... 'HorizontalAlignment', 'center', ... 'VerticalAlignment', 'middle', 'color', 'r'); end hold off; 

This image after showing the order of objects:

BW image with numbered objects

I need the order from top to bottom to bottom right. (the first row of objects is numbered from 1 to 6, the second row from 7 to 12..nets). Is it possible?

Thank you very much.

+5
source share
1 answer

Disabling the Suever Offer to use Centroid data and provided that s contains only 18 areas of interest in the examples, here is one way to sort s from left to right, top to bottom, using histcounts and sortrows :

 coords = vertcat(s.Centroid); % 2-by-18 matrix of centroid data [~, ~, coords(:, 2)] = histcounts(coords(:, 2), 3); % Bin the "y" data [~, sortIndex] = sortrows(coords, [2 1]); % Sort by "y" ascending, then "x" ascending s = s(sortIndex); % Apply sort index to s 

And here is the image denoting the marking of each area (as in the code example):

enter image description here

First, data bination โ€œyโ€ allows you to group objects into 3 lines of 6. The sortrows function sortrows after sorting by this bin value, can then perform a sub-series of all โ€œxโ€ values โ€‹โ€‹for each unique binn group.

+3
source

Source: https://habr.com/ru/post/1266031/


All Articles