|
| 1 | +classdef AverageValueByRegion < imagem.actions.CurrentImageAction |
| 2 | +% One-line description here, please. |
| 3 | +% |
| 4 | +% Class AverageValueByRegion |
| 5 | +% |
| 6 | +% Example |
| 7 | +% AverageValueByRegion |
| 8 | +% |
| 9 | +% See also |
| 10 | +% |
| 11 | + |
| 12 | +% ------ |
| 13 | +% Author: David Legland |
| 14 | + |
| 15 | +% Created: 2020-12-23, using Matlab 9.8.0.1323502 (R2020a) |
| 16 | +% Copyright 2020 INRAE - BIA-BIBS. |
| 17 | + |
| 18 | + |
| 19 | +%% Properties |
| 20 | +properties |
| 21 | +end % end properties |
| 22 | + |
| 23 | + |
| 24 | +%% Constructor |
| 25 | +methods |
| 26 | + function obj = AverageValueByRegion(varargin) |
| 27 | + % Constructor for AverageValueByRegion class. |
| 28 | + end |
| 29 | + |
| 30 | +end % end constructors |
| 31 | + |
| 32 | + |
| 33 | +%% Methods |
| 34 | +methods |
| 35 | + function run(obj, frame) %#ok<INUSL> |
| 36 | + |
| 37 | + app = frame.Gui.App; |
| 38 | + imageNames = getImageNames(app); |
| 39 | + |
| 40 | + % creates a new dialog, and populates it with some fields |
| 41 | + gd = imagem.gui.GenericDialog('Average Value by Region'); |
| 42 | + addChoice(gd, 'Label image of regions:', imageNames, imageNames{1}); |
| 43 | + addChoice(gd, 'Image of intensities:', imageNames, imageNames{1}); |
| 44 | + |
| 45 | + % displays the dialog, and waits for user |
| 46 | + showDialog(gd); |
| 47 | + % check if ok or cancel was clicked |
| 48 | + if wasCanceled(gd) |
| 49 | + return; |
| 50 | + end |
| 51 | + |
| 52 | + % parse the user inputs |
| 53 | + labelImageName = getNextString(gd); |
| 54 | + intensityImageName = getNextString(gd); |
| 55 | + |
| 56 | + % check label image |
| 57 | + labelImageDoc = getImageDocument(app, labelImageName); |
| 58 | + labelImage = labelImageDoc.Image; |
| 59 | + if ~isLabelImage(labelImage) |
| 60 | + warning('Requires region image to be a label image'); |
| 61 | + return; |
| 62 | + end |
| 63 | + |
| 64 | + % retrieve intensity image |
| 65 | + intensityImageDoc = getImageDocument(app, intensityImageName); |
| 66 | + intensityImage = intensityImageDoc.Image; |
| 67 | + |
| 68 | + |
| 69 | + % get labels |
| 70 | + labels = unique(labelImage.Data(:)); |
| 71 | + labels(labels == 0) = []; |
| 72 | + nLabels = length(labels); |
| 73 | + |
| 74 | + if isScalarImage(intensityImage) |
| 75 | + resData = zeros(nLabels, 1); |
| 76 | + for i = 1:nLabels |
| 77 | + resData(i) = mean(intensityImage.Data(labelImage.Data == labels(i))); |
| 78 | + end |
| 79 | + res = Table(resData, {'Mean'}); |
| 80 | + |
| 81 | + elseif isVectorImage(intensityImage) |
| 82 | + nc = channelCount(intensityImage); |
| 83 | + resData = zeros(nLabels, nc); |
| 84 | + |
| 85 | + for ic = 1:nc |
| 86 | + channelImage = channel(intensityImage, ic); |
| 87 | + for i = 1:nLabels |
| 88 | + resData(i, ic) = mean(channelImage.Data(labelImage.Data == labels(i))); |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + channelNames = intensityImage.ChannelNames; |
| 93 | + if isempty(channelNames) || length(channelNames) ~= nc |
| 94 | + channelNames = cellstr(num2str((1:nc)', 'Ch%02d'))'; |
| 95 | + end |
| 96 | + res = Table(resData, channelNames); |
| 97 | + |
| 98 | + else |
| 99 | + error(['Can not manage type of image: ' intensityImage.Name]); |
| 100 | + end |
| 101 | + |
| 102 | + % create first axis |
| 103 | + res.Axes{1} = table.axis.NumericalAxis('Label', labels(:)'); |
| 104 | + res.Name = [intensityImage.Name '-AverageValues']; |
| 105 | + |
| 106 | + % display in a new table |
| 107 | + createTableFrame(frame.Gui, res); |
| 108 | + end |
| 109 | +end % end methods |
| 110 | + |
| 111 | +end % end classdef |
| 112 | + |
0 commit comments