Image Region filter how to only get mask for one area?

3 views (last 30 days)
Yu Jin
Yu Jin on 21 Jan 2022
Edited: Kevin Holly on 21 Jan 2022
Very new to image processing, bascially I want to obtain each cell of a table image.
How can I only get the mask of one area in the image region analyzer? I want to use that to crop out one piece of the table image and run ocr on that, is there a better way to do this?

Answers (1)

Kevin Holly
Kevin Holly on 21 Jan 2022
Edited: Kevin Holly on 21 Jan 2022
I would check Exclude Border
If you click the dropdown box under Export>Export Function
function [BW_out,properties] = filterRegions(BW_in)
%filterRegions Filter BW image using auto-generated code from imageRegionAnalyzer app.
% [BW_OUT,PROPERTIES] = filterRegions(BW_IN) filters binary image BW_IN
% using auto-generated code from the imageRegionAnalyzer app. BW_OUT has
% had all of the options and filtering selections that were specified in
% imageRegionAnalyzer applied to it. The PROPERTIES structure contains the
% attributes of BW_out that were visible in the app.
% Auto-generated by imageRegionAnalyzer app on 20-Jan-2022
BW_out = BW_in;
% Remove portions of the image that touch an outside edge.
BW_out = imclearborder(BW_out);
% Get properties.
properties = regionprops(BW_out, {'Area', 'Eccentricity', 'EquivDiameter', 'EulerNumber', 'MajorAxisLength', 'MinorAxisLength', 'Orientation', 'Perimeter'});
% Uncomment the following line to return the properties in a table.
% properties = struct2table(properties);
Now under the properities menu, I did not see an option to extract the region's Bounding Box nor Image when using regionprops. So, I am going to do it programmatically below.
I = imread('Capture.png');
grayscale_I = rgb2gray(I);
bw_I = imbinarize(grayscale_I);
% Remove portions of the image that touch an outside edge.
BW_out = imclearborder(bw_I);
% Get properties.
rp = regionprops(BW_out, {'Area', 'Centroid','BoundingBox','Image'})
rp = 75×1 struct array with fields:
Area Centroid BoundingBox Image
Cellnumber = 1;
The image above is completely white as this is what the binary image looks like.
Below is an extra step to verfiy the region of interest was select correctly.
% Check to verify region location.
roi_image = zeros(size(BW_out));
% Display region of interest within scale of original image.
BoundingBox_Rows = round(rp(Cellnumber).BoundingBox(2)):round((rp(Cellnumber).BoundingBox(2)+rp(Cellnumber).BoundingBox(4))-1);
BoundingBox_Columns = round(rp(Cellnumber).BoundingBox(1)):round((rp(Cellnumber).BoundingBox(1)+rp(Cellnumber).BoundingBox(3)))-1;
roi_image(BoundingBox_Rows,BoundingBox_Columns) = rp(Cellnumber).Image;

Community Treasure Hunt

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

Start Hunting!