Resize the object in binary image
2 views (last 30 days)
Show older comments
prashant singh
on 10 Jul 2017
Commented: Image Analyst
on 11 Jul 2017
I have a binary image of size 1000*1002. I have some multiple objects inside the image. I want to resize the objects and put it back to the original image size 1000*1002. I have use regionprops to extract my mask from the image. there are two issues: 1) regionprops just give me biggest mask? 2) once i resize my object, how can i put it back to my original image replacing that object with the updated object size?
2 Comments
Gopichandh Danala
on 11 Jul 2017
attach the binary image, and what is the criteria you want to use for resizing few objects in it? Like top 2, 3 blobs?
Accepted Answer
Image Analyst
on 11 Jul 2017
prashant, I'm making it super easy for you. Start with this code, then insert code from my attached copy and paste demo at the indicated place in the loop. And of course, change it to use your image instead of the demo image.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
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 = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'eight.tif';
% Get the full filename, with path prepended.
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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
hold on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
drawnow;
% Threshold the image
binaryImage = grayImage < 210;
% Extract 4 largest objects.
binaryImage = bwareafilt(binaryImage, 4);
% Fill holes.
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 3);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize);
drawnow;
% Label image
[labeledImage, numRegions] = bwlabel(binaryImage);
% Get subimages and centroids.
props = regionprops(labeledImage, 'Image', 'Centroid')
% Define an enlargement factor.
enlargementFactor = 1.5; % Whatever you want.
for k = 1 : numRegions
% Plot centroid on original image
subplot(2, 2, 1);
plot(props(k).Centroid(1), props(k).Centroid(2), 'r+', 'LineWidth', 3, 'MarkerSize', 50);
% Get this region.
thisImage = props(k).Image;
% Enlarge it
thisImage = imresize(thisImage, enlargementFactor);
subplot(2, 2, 4);
imshow(thisImage);
axis on;
caption = sprintf('This is region #%d, enlarged by a factor of %.2f', k);
title(caption, 'FontSize', fontSize);
% Now, part left for prashant, paste each image onto the original image.
% Use Image Analyst's copy and paste code here.
% Prompt user to continue.
promptMessage = sprintf('%s\nDo you want to Continue processing,\nor Quit processing?', caption);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
return;
end
end
2 Comments
Image Analyst
on 11 Jul 2017
Yes, there is. I don't know what you mean by fuse though. Writing white in the same locations is one thing, enlarging and pasting is another, putting it into an overlay is another, masking is another. Depends on what you want to achieve.
More Answers (0)
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!