Relabel ROIs in a circular pattern
Mostrar comentarios más antiguos
I have a set of blobs in a circular pattern, that is, they all are lined up in a circular pattern and I need to label them according the circle starting from the bottom of the circle. The way bwlabel works makes it very difficult to see an easy way to do this, the far left blob on the circle is one and the blob on the far right of the circle is the last label 8. Anyone have any ideas or have done this before? I have an idea to find the center of the circle and use angles to determine each blobs relative position, but then I'm pretty lost. Thank you!!
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 28 de Jun. de 2013
0 votos
Where did you post your image? I imagine you'd have to segment your blobs and get their centroids. Then renumber your blobs according to your own custom algorithm, perhaps based on the angle from the center of the image or something. Then you can do a relabeling really fast by using intlut and your original labeled image along with your renumbered list. Then, you can either sort your measurements from regionprops the same way, or simply start all over again by calling regionprops with your newly labeled image.
2 comentarios
Jeremy
el 28 de Jun. de 2013
Editada: Image Analyst
el 28 de Jun. de 2013
Image Analyst
el 28 de Jun. de 2013
The algorithm I gave should work. I'd just mask out the center of the image because you don't want those. Can you do it? Here's some code to get you started:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = 'D:\Temporary stuff';
baseFileName = '8SzRpvi.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Binarize the image
binaryImage = grayImage > 128;
% Get rid of small particles.
binaryImage = bwareaopen(binaryImage, 400);
% Mask out central area.
binaryImage(130:380, 130:380) = false;
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Masked Binary Image', 'FontSize', fontSize);
% Do connected components labeling.
cc = bwconncomp(binaryImage);
% Measure the centroid locations.
measurements = regionprops(cc, 'Centroid');
allCentroids = [measurements.Centroid]
% Plot centroids over the image.
centroidX = allCentroids(1:2:end);
centroidY = allCentroids(2:2:end);
hold on;
plot(centroidX, centroidY, 'b*');
% Specify the center of the image.
imageCenterX = columns / 2;
imageCenterY = rows / 2;
% Calculate the angles from the center to the discs.
angles = atand((centroidY - imageCenterY) ./ (centroidX - imageCenterX))
% Find the blob closest to the bottom with angle -pi/2
[~, minIndex] = min(angles - (-pi/2))
% Plot a red square over that one
plot(centroidX(minIndex), centroidY(minIndex), ...
'rs', 'MarkerSize', 20, 'LineWidth', 2);
% Plot the initial numbers
for blob = 1 : cc.NumObjects
x = centroidX(blob)
y = centroidY(blob) - 5
text(x, y, num2str(blob), 'Color', [1, 0, 0], 'FontWeight', 'Bold');
end
[sortedAngles, sortIndexes] = sort(angles, 'ascend')
% Get a labeled matrix. Bottom blob has angle +pi/2 since y increases as it goes downward.
labeledImage = labelmatrix(cc);
See if you can finish it.
Categorías
Más información sobre Region and Image Properties en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
