MATLAB Answers

how to know the location pixel

1 view (last 30 days)
Dear all,
I done convert gray image to binary image. picture as attached.
my image name is spect128x128
I15 = dicomread('spect128x128', 'frame', 15);
T15 = graythresh(level);
BW15 = imbinarize(I15,T15);
figure
imshowpair(I15, BW15, 'montage')
CC = bwconncomp(BW16)
CC =
struct with fields:
Connectivity: 8
ImageSize: [130 130]
NumObjects: 4
PixelIdxList: {[6×1 double] [22×1 double] [35×1 double] [59×1 double]}
>> numPixels = cellfun(@numel,CC.PixelIdxList)
numPixels =
6 22 35 59
how to know the location for 16, 22, 35 56 it self?

Accepted Answer

Image Analyst
Image Analyst on 12 Nov 2020
Try this well commented demo. It finds all the blobs, then gets their area, centroid, and list of all the pixels in each blob. It also colorizes each discrete blob so you know what blob is distinct.
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 = 16;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in gray scale image.
folder = pwd;
baseFileName = 'image.jpeg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
% If it's RGB instead of grayscale, convert it to gray scale.
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage);
axis on;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Gray Scale Image', 'FontSize', fontSize);
% Get the binaryImage
mask = imbinarize(grayImage);
% Get rid of huge white frame
mask = imclearborder(mask); % Eliminate blobs touching the border.
% mask = grayImage > 44; % or whatever value works.
% Display the image.
subplot(2, 2, 3);
imshow(mask);
axis on;
title('Mask Image', 'FontSize', fontSize);
% Get areas, centroids, and pixel coordinates.
[labeledImage, numberOfBlobs] = bwlabel(mask);
props = regionprops(mask, 'Area', 'Centroid', 'PixelList');
% Colorize each blob.
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(2, 2, 4);
imshow(coloredLabels);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
caption = sprintf('Pseudo colored labels, from label2rgb().\nBlobs are numbered from top to bottom, then from left to right.');
title(caption, 'FontSize', fontSize);
% Print out the results.
for k = 1 : numberOfBlobs
xy = props(k).PixelList; % Get an N by 2 array of (x,y) coordinates for this blob.
fprintf('Blob #%d: Area = %d. Centroid (x, y) = (%.4f, %.4f)\nPixels in blob #%d:\n ', ...
k, props(k).Area, props(k).Centroid(1), props(k).Centroid(2), k);
for k2 = 1 : size(xy, 1) % For every pixel in this blob, print it's coordinates
fprintf('(%d, %d), ', xy(k2, 1), xy(k2, 2));
end
fprintf('\n'); % Go to next line in command window.
end
In the command window you will see a list of all the measurements:
Blob #1: Area = 6. Centroid (x, y) = (151.1667, 83.1667)
Pixels in blob #1:
(150, 83), (151, 82), (151, 83), (151, 84), (152, 83), (152, 84),
Blob #2: Area = 14. Centroid (x, y) = (163.7143, 83.5000)
Pixels in blob #2:
(162, 83), (162, 84), (163, 82), (163, 83), (163, 84), (163, 85), (164, 82), (164, 83), (164, 84), (164, 85), (165, 82), (165, 83), (165, 84), (165, 85),
Blob #3: Area = 28. Centroid (x, y) = (169.7857, 94.5000)
Pixels in blob #3:
(167, 94), (167, 95), (168, 93), (168, 94), (168, 95), (168, 96), (169, 92), (169, 93), (169, 94), (169, 95), (169, 96), (169, 97), (170, 92), (170, 93), (170, 94), (170, 95), (170, 96), (170, 97), (171, 92), (171, 93), (171, 94), (171, 95), (171, 96), (171, 97), (172, 93), (172, 94), (172, 95), (172, 96),
Blob #4: Area = 6. Centroid (x, y) = (274.8333, 93.8333)
Pixels in blob #4:
(274, 93), (274, 94), (275, 93), (275, 94), (275, 95), (276, 94),
Blob #5: Area = 22. Centroid (x, y) = (281.0909, 83.0909)
Pixels in blob #5:
(279, 82), (279, 83), (279, 84), (280, 81), (280, 82), (280, 83), (280, 84), (280, 85), (281, 81), (281, 82), (281, 83), (281, 84), (281, 85), (282, 81), (282, 82), (282, 83), (282, 84), (282, 85), (283, 82), (283, 83), (283, 84), (283, 85),
Blob #6: Area = 35. Centroid (x, y) = (293.5714, 83.6286)
Pixels in blob #6:
(291, 82), (291, 83), (291, 84), (291, 85), (292, 81), (292, 82), (292, 83), (292, 84), (292, 85), (292, 86), (293, 81), (293, 82), (293, 83), (293, 84), (293, 85), (293, 86), (293, 87), (294, 81), (294, 82), (294, 83), (294, 84), (294, 85), (294, 86), (294, 87), (295, 81), (295, 82), (295, 83), (295, 84), (295, 85), (295, 86), (296, 81), (296, 82), (296, 83), (296, 84), (296, 85),
Blob #7: Area = 59. Centroid (x, y) = (299.6780, 94.6102)
Pixels in blob #7:
(296, 93), (296, 94), (296, 95), (296, 96), (296, 97), (297, 92), (297, 93), (297, 94), (297, 95), (297, 96), (297, 97), (297, 98), (298, 91), (298, 92), (298, 93), (298, 94), (298, 95), (298, 96), (298, 97), (298, 98), (299, 91), (299, 92), (299, 93), (299, 94), (299, 95), (299, 96), (299, 97), (299, 98), (300, 91), (300, 92), (300, 93), (300, 94), (300, 95), (300, 96), (300, 97), (300, 98), (301, 91), (301, 92), (301, 93), (301, 94), (301, 95), (301, 96), (301, 97), (301, 98), (302, 91), (302, 92), (302, 93), (302, 94), (302, 95), (302, 96), (302, 97), (302, 98), (303, 92), (303, 93), (303, 94), (303, 95), (303, 96), (303, 97), (304, 95),
  6 Comments

Sign in to comment.

More Answers (2)

Matt J
Matt J on 12 Nov 2020
Edited: Matt J on 12 Nov 2020
If you mean you want the centroids of the different regions, it would be easier to use regionprops
stats=regionprops(BW16,'Centroid','Area')
  5 Comments
mohd akmal masud
mohd akmal masud on 17 Nov 2020
yah, i got it. tq so much

Sign in to comment.


Ameer Hamza
Ameer Hamza on 12 Nov 2020
PixelIdxList property already contain the linear indexes of the pixels. If you want row and column number then you can try this
I15 = dicomread('spect128x128', 'frame', 15);
T15 = graythresh(level);
BW15 = imbinarize(I15,T15);
figure
imshowpair(I15, BW15, 'montage')
CC = bwconncomp(BW16)
[r, c] = cellfun(@(x) ind2sub(size(BW15), x), CC.PixelIdxList, 'UniformOutput', 0);
  1 Comment
mohd akmal masud
mohd akmal masud on 12 Nov 2020
ok, thank you so much sir.
then how to to fill the color in every object. lets say i want fill blue color in object no.4?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!