subplot with multiple images - Pulling images from API into one figure

3 Ansichten (letzte 30 Tage)
Hello, I have some working code where I search for chemical structures (compounds) via the PubChem API, and then loop through the retrieved PubChem Compound identifiers and display the PNG images of them. What I would like to do is not create a separate figure for each image, but rather use something like subplot to create a grid (3 columns wide). This way, all of the compound images are in one figure. I am not having much luck. Your help would be much appreciated. Here is the code I wrote below:
% Search for chemical structures by Identity (same connectivity, SC) as Ingenol
api = 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/';
SC_url = [api 'fastidentity/cid/442042/cids/JSON?identity_type=same_connectivity'];
SC = webread(SC_url);
SC = num2cell(SC.IdentifierList.CID)
% Retrieve PNG images of same connectivity CIDs in SC dataset.
% loop through CIDs and define web api url
for r = 1:length(SC)
SC_CID = SC{r};
SC_CID_url = [api 'cid/' num2str(SC_CID) '/PNG'];
try
% retrieve CID PNG image and display
[SC_CID_img,map] = imread(SC_CID_url);
figure;
imshow(SC_CID_img,map)
drawnow;
title(num2str(SC_CID));
% be polite to PubChem server
n = 1;
pause(n);
catch
disp('CID image not found')
disp('Execution will continue')
end
end

Akzeptierte Antwort

Anton Semechko
Anton Semechko am 5 Jul. 2018
Hey, Vincent. Here is an example:
[IM,IM_all]=PubChem_compound_image_stacking_demo;
------------------------------------------------------------------------------------------------------------------------------------------
function [IM,IM_all]=PubChem_compound_image_stacking_demo
% Search for chemical structures by Identity (same connectivity, SC) as Ingenol
api = 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/';
SC_url = [api 'fastidentity/cid/442042/cids/JSON?identity_type=same_connectivity'];
SC = webread(SC_url);
SC = num2cell(SC.IdentifierList.CID);
% Retrieve PNG images of same connectivity CIDs in SC dataset.
% loop through CIDs and define web api url
N=numel(SC);
[IM,CID,flag]=deal(cell(N,1),zeros(N,1),true(N,1));
for i = 1:N
SC_CID = SC{i};
SC_CID_url = [api 'cid/' num2str(SC_CID) '/PNG'];
try
% retrieve CID PNG image and display
[IM{i},map] = imread(SC_CID_url);
CID(i)=SC_CID;
IM{i}=uint8(255*ind2rgb(IM{i},map));
flag(i)=false;
% be polite to PubChem server
pause(1)
fprintf('image %2u\\%2u dowloaded\n',i,N)
catch
fprintf(2,'CID image # %u not found\n',SC_CID);
end
end
% All available images
IM(flag)=[];
CID(flag)=[];
% Sort images according to CID; all images are assumed to have equal dimensions
N=numel(IM);
[CID,id_srt]=sort(CID);
IM=IM(id_srt);
siz=size(IM{1});
% Arrange images into a rectangular array, so that images are ordered
% according to CID from top to bottom, left to right
m=floor(sqrt(N)); % number of colums
n=ceil(N/m); % number of rows
cnt=0;
IM_all=cell(n,m);
for i=1:n % row
for j=1:m % column
% Get the image
cnt=cnt+1;
if cnt<=N
im=IM{cnt};
IM{cnt}=[];
if cnt==1
bkg_col=im(1,1,:);
im_bkg=repmat(bkg_col,[siz(1:2) 1]);
bkg_col=bkg_col(:)';
end
% annotate
im=insertText(im,[siz(2)/2 round(0.92*siz(1))],num2str(CID(cnt)),'AnchorPoint','center','BoxColor',bkg_col,'FontSize',25);
else
im=im_bkg;
end
% Make border
im(1:2,:,:)=0;
im(:,1:2,:)=0;
if j==m, im(:,(siz(2)-1):siz(2),:)=0; end
if i==n, im((siz(1)-1):siz(1),:,:)=0; end
IM_all{i,j}=im;
end
end
IM=cell2mat(IM_all);
% Visualize
figure('color','w')
imshow(IM)
if nargout<1, clear IM IM_all; end
  4 Kommentare
Anton Semechko
Anton Semechko am 5 Jul. 2018
Glad to help, Vincent. You can tune the number of columns by manually setting the variable 'm' to any desired value; the number of rows (specified by 'n') will be adjusted automatically. Example of inserting text into images when Image Processing Toolbox is unavailable can be found here.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Read, Write, and Modify Image finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by