How to read a subplot as an image and find correlation for two images?
2 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/668033/image.png)
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?
4 个评论
DGM
2021-6-28
But just to be clear, you're trying to find the correlation coeff between an image and the image with the plot line drawn on it, right?
采纳的回答
DGM
2021-6-28
编辑:DGM
2021-6-28
This is one way:
% example images and points
% points to plot [x y], one point per row
inpict1 = imread('cameraman.tif');
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];
% create line mask
hl = images.roi.Polyline;
hl.Position = pointlist1;
plm = createMask(hl,inpict1);
% apply mask
markedpict1 = inpict1;
markedpict1(plm) = 255;
% create/apply new line mask
hl.Position = pointlist2;
plm = createMask(hl,inpict2);
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!