MATLAB: How to get filename of input image in “Image Batch Processor” App

i/oimage processingImage Processing ToolboxMATLABtoolbox apps

Hello. I'm trying to use the Image Batch Processor App to analyze a folder of images. My goal is to count the number of cells bodies, color the cell bodies green, and overlay these green dots onto the original image. I want the overlay figure to have the same file name and file format as the variable im (the current image file being read by the app), and saved in a different output folder.
My code is below. So far, I can get the function in the app to count the cells and generate the overlay image that I want. (Which is attached for reference. Note that the bottom of the image will be cropped out before processing). I can also assign a hardcoded name to the figure and save it to my output folder. However, I can't figure out how to get the name of the input image to assign to the figure window. Any help would be appreciated!
function results = myimfcn(im)
%segmentImage Segment image using auto-generated code from imageSegmenter App
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the imageSegmenter App. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.
% Auto-generated by imageSegmenter app on 16-May-2017
%----------------------------------------------------
% IM - Input image.
% RESULTS - A scalar structure with the processing results.
close all;
% Threshold image - manual threshold
img=rgb2gray(im);
BW = img > 100;
% Close mask with disk
radius = 6;
decomposition = 0;
se = strel('disk', radius, decomposition);
BW = imclose(BW, se);
% Invert mask
BW = imcomplement(BW);
%Image Region Analyzer app filter code here
BW_filt = bwpropfilt(BW, 'Area', [150, 1000]);
%count cells
[~,n]=bwlabel(BW_filt);
%Change white in filtered image to green
blackImage = zeros(size(BW_filt));
rgbImage = cat(3, blackImage , BW_filt, blackImage);
%Overlay green mask onto original GRAYSCALE image
%[~,name,~] = fileparts(im); %get the name of the image file...HOW?
imshow(img);
hold on
green = cat(3, zeros(size(img)),ones(size(img)),zeros(size(img)));
h = imshow(green);
hold off;
% Use our filtered cells as the AlphaData for the solid green image.
set(h, 'AlphaData', BW_filt); %Not sure how to save this image in a batch
set(gcf, 'Name', 'Test1', 'NumberTitle','off'); %How to set file name of im variable, in place of 'Test1'?
figure=gcf;
title=figure.Name;
figuredir='C:\Users\bratka\Desktop\Output\';
saveas(gcf,strcat(figuredir,title),'png');
results.CellCount=n;

Best Answer

Hi James,
As of now, you can't get the name of the image directly from the App, to name the figure window. That said, the same can be done, once you generate the MATLAB code for the batch processing you have done.
The generated codes contain all the routines required to read the images and do the processing and hence the name of the image can be easily obtained. To generate the code: click Export >> Generate Function
To add, Apps are intended to be quick prototyping tools and finer details are expected to be implemented in the generated code. hence the limitation.
Thanks, Abel