How do I cut a specific area in the image

16 views (last 30 days)
I have some images as shown below
and I wnat to get the square area only, like the image below, how can I do?

Accepted Answer

Image Analyst
Image Analyst on 20 Jun 2021
You have to determine the starting and stopping row and column. Then you can extract it to a new image like this:
newImage = rgbImage(row1:row2, col1:col2, :);
Hopefully they're the same for all your images. If not, you'll have to find them, which is not too hard -
  1. just threshold for purple to get a mask. Use the Color Thresholder on the Apps tab of the tool ribbon.
  2. get rid of purple touching edge of image with mask = imclearborder(mask)
  3. call bwareafilt() to get the 16 largest blobs: mask = bwareafilt(mask, 16);
  4. call bwconvhull() to get a mask covering those 16 blob: mask = bwconvhull(mask)
  5. call props = regionprops(mask, 'BoundingBox');
  6. call newImage = imcrop(rgbImage, props.BoundingBox)
  4 Comments
Image Analyst
Image Analyst on 22 Jun 2021
On the Apps tab of the tool ribbon, look for Color Thresholder and start it. Click Load Image and specify your image. Then pick the HSV color space and adjust the sliders. Then click the Export function button to get the code. Call that code from your main program.
[mask, maskedRGBImage] = createMask(rgbImage);
For 2, I told you what to do:
mask = imclearborder(mask);
Here it is a little more filled out:
% Demo by Image Analyst, June, 2021.
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 = [];
baseFileName = 'image.jpeg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~isfile(fullFileName)
% 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
fullFileName = fullFileNameOnSearchPath;
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(2, 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';
%--------------------------------------------------------------------------------------------------------
% Mask the image
[mask, maskedRGBImage] = createMask(rgbImage);
% Get rid of purple touching edge of image with imclearborder():
mask = imclearborder(mask);
% Get rid of rectangles less than 15000 in area
mask = bwareaopen(mask, 15000);
mask = bwareafilt(mask, 16); % to get the 16 largest blobs
% Call bwconvhull() to get a mask covering those 16 blob:
mask = bwconvhull(mask);
% Display the image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Make measurements.
props = regionprops(mask, 'BoundingBox', 'Area');
% Crop out a new image from the old image.
newImage = imcrop(rgbImage, props.BoundingBox);
% Display the image.
subplot(2, 2, 3);
imshow(newImage, []);
axis('on', 'image');
caption = sprintf('New Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
fprintf('Done running %s.m\n', mfilename);
%==============================================================================================
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 21-Jun-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.642;
channel1Max = 0.735;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.436;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.697;
channel3Max = 0.790;
% 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
larry liu
larry liu on 22 Jun 2021
I am very grateful for your help and I will look into it again!!!!

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!