# How to read a subplot as an image and find correlation for two images?

2 views (last 30 days)
Tawsif Mostafiz on 28 Jun 2021
Commented: Tawsif Mostafiz on 30 Jun 2021
Hi, I am writing a code for image segmentation where it takes multiple images as input and the output is as follow for each images.
The relevent code is:
figure(k);
subplot(1,2,1);
imshow(imageArray,[]);
title('Image');
subplot(1,2,2);
imshow(imageArray,[]);
hold on
for i=1:length(B)
plot(B{i}(:,2),B{i}(:,1), 'y' ,'linewidth',1.45);
end
title('Segmented Tumor');
hold off;
Where B is defined in the code. My goal is to find the correlation of two images (Chosen by the user), and correlation value is shown for the second subplot of the selected images, that is "Segmented Tumor" . How can I do it?
Tawsif Mostafiz on 28 Jun 2021
No actually. The image that I have uploaded here shows only one output. The code I am writing can take multiple images as input and shows output as this image shows. Which means if I take three input images, there will be three output figures similar to the image shown above. Now, my goal is for example: I take two images as input. Then as the output comes for both, I want to take the second subplot for both images, that is the segmented tumor. And then find the correlation between them. Not an image with and without the plot, but between two different image with plots drawn on. Thanks again for your interest.

DGM on 28 Jun 2021
Edited: DGM on 28 Jun 2021
This is one way:
% example images and points
% points to plot [x y], one point per row
inpict2 = fliplr(inpict1);
pointlist1 = [80 80; 70 140; 100 190; 200 150; 160 70; 130 80];
pointlist2 = [80 40; 40 160; 130 150; 160 210; 250 160; 100 50];
hl = images.roi.Polyline;
hl.Position = pointlist1;
markedpict1 = inpict1;
markedpict1(plm) = 255;
hl.Position = pointlist2;
markedpict2 = inpict2;
markedpict2(plm) = 255;
% find corr
corr2(markedpict1,markedpict2)
This does not use the images as altered by imshow. The lines are drawn directly on a copy of the original image. Of course, there are considerations.
The lines that are drawn are approximately 1px wide with no antialiasing. As far as I know, there are no practical tools in base or IP toolboxes for drawing antialiased lines or lines of variable width. There may be some on the FEX, but I'd have to further question the utility. As the correlation coefficient becomes a function of line properties, what exactly does it mean conceptually?
Is it possible to extract the image directly anyway? Sure. Use getframe(), but the images will be whatever size they are rendered on the screen. They'll also likely be RGB, which corr2() can't handle, so you'll have to convert them to grayscale.
% do it the lossy way
inpict = imread('cameraman.tif'); % same image, same points
imshow(inpict)
S = getframe;
f1 = rgb2gray(S.cdata); % captured plot
Again, why should the correlation coefficient be a function of the line color? Since the plot geometry depends on the figure geometry, the correlation coefficient is also dependent on how big the figure is. What technical meaning does that have?
Is it possible to force them to be rendered exactly one pixel per pixel? Yes, but it's not as trivial as the imshow docs make it sound. Of course, none of this will work unless the images can actually fit within the figure at 1:1 resolution.
% demonstrate forced 1:1 image display using imshow
% create perfect 1px fine checkerboard
x = mod(1:250,2);
y = mod(1:210,2);
inpict = xor(x,y');
s = size(inpict);
figure
set(gcf,'position',[0 0 640 480])
% may need to use tight borders so there's enough room for 1:1 display
options = {'border','tight'};
h1 = subplot(1,2,1,'units','pixels');
h1.Position(1:2) = h1.Position(1:2) + h1.Position(3:4)/2 - s([2 1])/2;
h1.Position(3:4) = s([2 1]);
imshow(inpict,options{:});
h2 = subplot(1,2,2,'units','pixels');
h2.Position(1:2) = h2.Position(1:2) + h2.Position(3:4)/2 - s([2 1])/2;
h2.Position(3:4) = s([2 1]);
imshow(inpict,options{:});
Again, I'd try to avoid ever using captured plots for any analytical purposes.
Tawsif Mostafiz on 30 Jun 2021
Thanks @DGM!

R2021a

### Community Treasure Hunt

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

Start Hunting!