How to find the area for each regions with different pixels intensity

3 views (last 30 days)
hi, there. i need your help. I ve this picture and i need to find the area for each of the regions with different pixels value. thank you in advance for those who willing to help me.

Answers (2)

darova
darova on 27 Jun 2020
See this trick
I0 = imread('image.png');
kk = unique(I0(:));
for i = 1:length(kk)
I1 = I0 == i;
imshowpair(I0,I1)
pause(0.5)
end
  1 Comment
Image Analyst
Image Analyst on 27 Jun 2020
Almost. To get the pixel counts:
I0 = imread('image.png');
kk = unique(I0(:));
for i = 1:length(kk)
thisGrayLevel = kk(i)
I1 = I0 == thisGrayLevel;
imshowpair(I0, I1)
pixelCount = nnz(I1);
caption = sprintf('For Gray Level %d, there are %d pixels.', thisGrayLevel, pixelCount)
title(caption, 'FontSize', 15);
pause(0.5)
end
But even that doesn't do it. Do you know why? What if two separate regions have the same gray level? This gives them sum of both of them, not each region's area by itself.

Sign in to comment.


Image Analyst
Image Analyst on 27 Jun 2020
Try this:
grayImage = imread('image.png');
uniqueGrayLevels = unique(grayImage(:))
for k = 1:length(uniqueGrayLevels)
thisGrayLevel = uniqueGrayLevels(k);
mask = grayImage == thisGrayLevel;
imshowpair(grayImage, mask)
impixelinfo;
pixelCount = nnz(mask);
% Identify the separate regions' areas.
props = regionprops(mask, 'Area');
allAreas = [props.Area];
caption = sprintf('\nFor Gray Level %d, there are a total of %d pixels in %d regions.', thisGrayLevel, pixelCount, length(allAreas));
title(caption, 'FontSize', 15);
for r = 1 : length(allAreas)
fprintf(' For region %d of graylevel %d, there are %d pixels.\n', r, thisGrayLevel, allAreas(r));
end
pause(0.25)
end
uniqueGrayLevels' % Show again in the command window.
  2 Comments
Nauratul Suqda
Nauratul Suqda on 27 Jun 2020
first of all . thank you for replying me. here, let me explain. i ve to find the area of pink colour region on the left colomn. i got these 5 pictures(right side) from matlab after implemented bresson model. here the problem is i dont know which one of those images should i used to get the area. would you please tell me which of the images can help me in determining the area of pink colour region?
Image Analyst
Image Analyst on 27 Jun 2020
I never heard of that model. What I would do is use the Color Thresholder on the Apps tab of the tool ribbon. You forgot to attach the original image so I had to get that extremely poor quality image from what you posted, so it looks awful, and not just because it looks like a severed finger from a horrific accident.
% Demo to find pink pixels.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = pwd;
baseFileName = 'pink.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, '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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the RGB image full size.
subplot(1, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Threshold the image to find the small dark black thing.
[mask, maskedRGBImage] = createMask(rgbImage);
% Fill the blobs.
mask = imfill(mask, 'holes');
% Take the largest blob only.
mask = bwareafilt(mask, 1);
% Take the blobs only if they're 400 pixels or larger.
mask = bwareaopen(mask, 400);
% Fill holes
mask = imfill(mask, 'holes');
% Display the binary image.
subplot(1, 2, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf(' Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the boundary
boundaries = bwboundaries(mask);
boundaries = boundaries{1}; % Extract from cell
x = boundaries(:, 2);
y = boundaries(:, 1);
hold on;
subplot(1, 2, 1);
hold on;
plot(x, y, 'r-', 'LineWidth', 3);
%================================================================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 27-Jun-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.963;
channel1Max = 0.139;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.441;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.356;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!