Sir . I have successfully cropped the upper region (in 5 coin) but couldn't crop lower region from the same image as I need to crop the image so to get center part only as high.

1 view (last 30 days)

Accepted Answer

Image Analyst
Image Analyst on 18 Feb 2018
Try this. Adapt as needed, like the percentage of the middle that you want.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'c3.jpg';
folder = pwd
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
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(3, 3, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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')
drawnow;
% Find the max in all color channels.
mask = max(rgbImage, [], 3);
% Take the largest blob only, to get rid of noise.
% mask = bwareafilt(mask, 1);
% Fill any holes in it.
% mask = imfill(mask, 'holes');
% Let's assume it's supposed to be convex. Make sure.
% mask = bwconvhull(mask);
% Display the image.
subplot(3, 3, 2);
histogram(mask);
% Binarize the image
mask = mask > 225;
% Keep only largest region then invert it to get the coin masks.
mask = ~bwareafilt(mask, 1);
% Fill holes.
mask = imfill(mask, 'holes');
% Get rid of blobs less than 1000 pixels. There seems to be a few.
mask = bwareafilt(mask, [1000, inf]);
[labeledImage, numBlobs] = bwlabel(mask);
imshow(mask, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the image with the new, cleaned mask.
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the image.
subplot(3, 3, 3);
imshow(maskedRgbImage, []);
axis on;
title('Masked RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get the equivalent bounding box.
props = regionprops(mask, 'BoundingBox', 'area');
allAreas = [props.Area]
plotNumber = 7;
percentage = 0.15; % Percentage of coin the middle part should be.
for k = 1 : length(props)
% Crop the image
croppedImage = imcrop(rgbImage, props(k).BoundingBox);
% Display the images.
subplot(3, 6, plotNumber);
imshow(croppedImage, []);
axis on;
title('Cropped RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Now crop out the middle 50%
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(croppedImage);
row1 = round(rows * (0.5 - percentage));
row2 = round(rows * (0.5 + percentage));
col1 = round(rows * (0.5 - percentage));
col2 = round(rows * (0.5 + percentage));
middlePart = croppedImage(row1:row2, col1:col2, :);
% Display the images.
subplot(3, 6, plotNumber + 1);
imshow(middlePart, []);
axis on;
title('Cropped RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
plotNumber = plotNumber + 2;
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!