Remove Rows/Columns from Binary Image Matrix

2 次查看(过去 30 天)
I know that when an image is converted to binary, it is turned into an array of 1s and 0s based on the threshold. I want to remove rows and columns based on the code. I would do a set to remove the columns (and rows) before and after the area I want to keep.
A(:, (columns I want to remove))=[].
I want to use this as a way to crop the binary image down. I am concerned in my project that if I crop the my image into partitions prior prior to being binarized, the results are inconsistent. ie, the total spots I may count in the full image is not equal to the sum of all the cropped images. Below is my code for the full project and an example file to work with.
classdef spot_to_heatmap1_exported < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Menu matlab.ui.container.Menu
OpenFileMenu matlab.ui.container.Menu
CloseProgramMenu matlab.ui.container.Menu
SpotDetectorLabel matlab.ui.control.Label
StartButton matlab.ui.control.Button
AnalysisParametersPanel matlab.ui.container.Panel
RowsSpinner matlab.ui.control.Spinner
RowsSpinnerLabel matlab.ui.control.Label
ColumnsSpinner matlab.ui.control.Spinner
ColumnsSpinnerLabel matlab.ui.control.Label
ColorSchemeforHeatMapButtonGroup matlab.ui.container.ButtonGroup
GrayButton matlab.ui.control.RadioButton
CoolButton matlab.ui.control.RadioButton
AutumnButton matlab.ui.control.RadioButton
ParulaButton matlab.ui.control.RadioButton
SpringButton matlab.ui.control.RadioButton
SummerButton matlab.ui.control.RadioButton
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
fullname %for calling file back later
end
% Callbacks that handle component events
methods (Access = private)
% Menu selected function: OpenFileMenu
function OpenFileMenuSelected(app, event)
[filename,pathname] = uigetfile('*.tif','Select the File to Open');
app.fullname = [pathname,filename];
movegui(app.UIFigure, 'center')
hold(app.UIAxes, 'on')
imshow(app.fullname, 'Parent', app.UIAxes)
end
% Menu selected function: CloseProgramMenu
function CloseProgramMenuSelected(app, event)
close all
app.delete
end
% Button pushed function: StartButton
function StartButtonPushed(app, event)
%Image detection------------------------------------------------
nRows=(app.RowsSpinner.Value);
nColumns=(app.ColumnsSpinner.Value);
image=imread(app.fullname);
[rows, columns, ~]=size(image);
nV=rows/nRows; %size of each vertical frame for partioning image %%%%%%i swithed, NH, nV if this start looking weird
nH=columns/nColumns; %size of each horizontal frame for partioning image
A = zeros(nRows, nColumns); %create empty array based on the user size input
for i=1:nRows %work through each row (top to bottom)
for k=1:nColumns %within each row, work through the various columns of the image
if k*nH==columns
continue
end
if k*nH ~= columns
I=imread(app.fullname);
%I=imcrop(I, [((k*nH)-nH) ((i*nV)-nV) nH nV]); %crop naming is [xmin ymin width height]
h=fspecial('gaussian',[3 3],1); %gaussian filter on raw image
I2=imfilter(I,h, 'replicate', 'conv'); bw=imfill(bw,'holes'); %fill potential holes in spots
bw=bwareafilt(bw,[20 500], 8); % [range of acceptable sizes], connectivity: adjust these bounds to adjust the size of the particles that are thresholded
stats=regionprops('table', bw, 'Circularity');
rowidx=(stats.Circularity < .7); %selects all points with circularity less than .6
keepSpots=stats(~rowidx, :); %removes all points previously selected by rowidx
totalSpots=height(keepSpots); %converts number found spots from table to single value
A(i,k)=totalSpots;
end
end
end
cdata=A; %convert matrix for use by heatmap code
%color selection and heatmapping-------------------------------
if app.AutumnButton.Value
color=autumn;
elseif app.ParulaButton.Value
color=parula;
elseif app.SpringButton.Value
color=spring;
elseif app.SummerButton.Value
color=summer;
elseif app.CoolButton.Value
color=cool;
elseif app.GrayButton.Value
color=gray;
end
h=heatmap(cdata, 'Colormap', color);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create Menu
app.Menu = uimenu(app.UIFigure);
app.Menu.Text = 'Menu';
% Create OpenFileMenu
app.OpenFileMenu = uimenu(app.Menu);
app.OpenFileMenu.MenuSelectedFcn = createCallbackFcn(app, @OpenFileMenuSelected, true);
app.OpenFileMenu.Text = 'Open File';
% Create CloseProgramMenu
app.CloseProgramMenu = uimenu(app.Menu);
app.CloseProgramMenu.MenuSelectedFcn = createCallbackFcn(app, @CloseProgramMenuSelected, true);
app.CloseProgramMenu.Text = 'Close Program';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Selected File')
zlabel(app.UIAxes, 'Z')
app.UIAxes.XTickLabel = '';
app.UIAxes.YTickLabel = '';
app.UIAxes.TitleHorizontalAlignment = 'left';
app.UIAxes.FontSize = 16;
app.UIAxes.Visible = 'off';
app.UIAxes.Position = [286 32 355 349];
% Create AnalysisParametersPanel
app.AnalysisParametersPanel = uipanel(app.UIFigure);
app.AnalysisParametersPanel.Title = 'Analysis Parameters';
app.AnalysisParametersPanel.FontWeight = 'bold';
app.AnalysisParametersPanel.FontSize = 16;
app.AnalysisParametersPanel.Position = [11 62 264 319];
% Create ColorSchemeforHeatMapButtonGroup
app.ColorSchemeforHeatMapButtonGroup = uibuttongroup(app.AnalysisParametersPanel);
app.ColorSchemeforHeatMapButtonGroup.Title = 'Color Scheme for Heat Map';
app.ColorSchemeforHeatMapButtonGroup.Position = [33 14 197 168];
% Create SummerButton
app.SummerButton = uiradiobutton(app.ColorSchemeforHeatMapButtonGroup);
app.SummerButton.Text = 'Summer';
app.SummerButton.Position = [14 102 67 17];
% Create SpringButton
app.SpringButton = uiradiobutton(app.ColorSchemeforHeatMapButtonGroup);
app.SpringButton.Text = 'Spring';
app.SpringButton.Position = [14 122 65 17];
app.SpringButton.Value = true;
% Create ParulaButton
app.ParulaButton = uiradiobutton(app.ColorSchemeforHeatMapButtonGroup);
app.ParulaButton.Text = 'Parula';
app.ParulaButton.Position = [14 58 65 17];
% Create AutumnButton
app.AutumnButton = uiradiobutton(app.ColorSchemeforHeatMapButtonGroup);
app.AutumnButton.Text = 'Autumn';
app.AutumnButton.Position = [14 80 63 17];
% Create CoolButton
app.CoolButton = uiradiobutton(app.ColorSchemeforHeatMapButtonGroup);
app.CoolButton.Text = 'Cool';
app.CoolButton.Position = [14 37 47 17];
% Create GrayButton
app.GrayButton = uiradiobutton(app.ColorSchemeforHeatMapButtonGroup);
app.GrayButton.Text = 'Gray';
app.GrayButton.Position = [14 15 48 22];
% Create ColumnsSpinnerLabel
app.ColumnsSpinnerLabel = uilabel(app.AnalysisParametersPanel);
app.ColumnsSpinnerLabel.HorizontalAlignment = 'right';
app.ColumnsSpinnerLabel.Position = [46 207 52 22];
app.ColumnsSpinnerLabel.Text = 'Columns';
% Create ColumnsSpinner
app.ColumnsSpinner = uispinner(app.AnalysisParametersPanel);
app.ColumnsSpinner.Limits = [1 15];
app.ColumnsSpinner.Position = [113 207 100 22];
app.ColumnsSpinner.Value = 3;
% Create RowsSpinnerLabel
app.RowsSpinnerLabel = uilabel(app.AnalysisParametersPanel);
app.RowsSpinnerLabel.HorizontalAlignment = 'right';
app.RowsSpinnerLabel.Position = [62 245 35 22];
app.RowsSpinnerLabel.Text = 'Rows';
% Create RowsSpinner
app.RowsSpinner = uispinner(app.AnalysisParametersPanel);
app.RowsSpinner.Limits = [1 15];
app.RowsSpinner.Position = [112 245 100 22];
app.RowsSpinner.Value = 3;
% Create StartButton
app.StartButton = uibutton(app.UIFigure, 'push');
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
app.StartButton.FontSize = 16;
app.StartButton.FontWeight = 'bold';
app.StartButton.Position = [92 15 100 28];
app.StartButton.Text = 'Start';
% Create SpotDetectorLabel
app.SpotDetectorLabel = uilabel(app.UIFigure);
app.SpotDetectorLabel.FontSize = 30;
app.SpotDetectorLabel.FontWeight = 'bold';
app.SpotDetectorLabel.FontAngle = 'italic';
app.SpotDetectorLabel.Position = [206 426 203 39];
app.SpotDetectorLabel.Text = 'Spot Detector';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = spot_to_heatmap1_exported
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

采纳的回答

dpb
dpb 2023-11-9
编辑:dpb 2023-11-9
A(:, (columns I want to remove))=[].
will remove everything from the top of the array to the bottom -- if there is a "spot" anywhere in those columns, you'll remove it, indeed, even though you may have only identified one specific location towards the top of the image; when you then do this, there won't be anything left to analyze when your loop over rows gets down to that next set of rows where the spot was.
You can't decimate the image until you've outlined all the areas of interest and, unless the ROIs don't overlap at all, you can't decimate the columns (or rows, either) that are within the overall bounds of all the ROIs.
IOW, you're trying to decimate too soon; you have to do all the ROI identification first and then save those pieces; you can't remove "holes" in the middle of an array (image).
To reiterate, you could only remove from the original image columns/rows that have no intersections with any ROIs across the whole image; how much or little that might be would all depend upon the number, size and relative placement of the ROIs.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by