Hello,
I would like to overlay a contourf plot onto a jpeg image to visually show the strain field (y-y) with respect to the body in question. I am currently saving the contourf plot as a jpeg, putting both jpegs on the same figure, and varying the alpha level of the contour. This presents two issues: 1) The contour figure needs to be slighly moved over the right to line up correctly with the base image and 2) It doesn't look very appealing.
My questions are:
1) Is there any alternate way for completing this visual representation (rather than overlaying two jpegs)? Maybe somehow using the raw data for the contourf plot?
2) How can I move over the contour jpeg relative to the sample jpeg?
Thank you.
imageOverlay('image_0001.jpg','YYstrainContour.jpg')
function imageOverlay(image1,image2)
%image1 is underformed sample, image2 is contourf
%must be jpegs or pngs. Meta file does not appear to work.
a = imread(image1); %read in undeformed sample image
C = imread(image2); %reading in contourf plot
C = imresize(C, [1024, 1280]); %resize contourf plot to same size as undeformed sample image
h = imshow(a) %display undeformed sample
hold on
h1 = imshow(C) %display contourf
h1.AlphaData = 0.3 %change transparency of contourf
hold off

 Respuesta aceptada

DGM
DGM el 2 de Abr. de 2022
Editada: DGM el 2 de Abr. de 2022
This is one example. You'll have to adapt it to the code you already have. I'm assuming it's preferred to do this with plotting tools.
% this is stuff i just picked out by using tooltips
% you already know the plot box size, but i don't
% so i need to find it to create the demo
% box coordinates (px)
boxpx = [504 409 1275 534];
% tick spacing (px)
tickw = [154 123];
% plot box size in data coordinates
axlimits = boxpx(3:4)./tickw*5;
% you already have code to plot a contourf
% so just use that
x = linspace(0,axlimits(1),5);
y = linspace(0,axlimits(2),5);
z = rand(5,5);
contourf(x,y,z);
colorbar
axis equal
set(gca,'ydir','reverse')
% crop the relevant part out of the image
samppict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/949734/undeformed%20sample.png');
samppict = rgb2gray(samppict); % don't really want color
samppict = imcrop(samppict,boxpx); % crop out the relevant part
% optionally, you can filter/adjust the image
% you could try a median filter to reduce interference
% caused by the weave pattern
samppict = medfilt2(samppict,[11 11]);
% even though we discarded the color, the image still needs
% to be RGB for image() to not colormap it -- which would
% ruin the colormapping on the contour plot
samppict = repmat(samppict,[1 1 3]);
hold on;
h1 = image(xlim,ylim,samppict);
h1.AlphaData = 0.3;
Instead of using a uniform alpha, you could use the image to create an alpha map, that way only certain parts are overlaid. I assume that it would be preferred to not have the dark areas muting the colors of the contour map.
clf
% you already have code to plot a contourf
% so just use that
x = linspace(0,axlimits(1),5);
y = linspace(0,axlimits(2),5);
z = rand(5,5);
contourf(x,y,z);
colorbar
axis equal
set(gca,'ydir','reverse')
% crop the relevant part out of the image
samppict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/949734/undeformed%20sample.png');
samppict = rgb2gray(samppict); % don't really want color
samppict = imcrop(samppict,boxpx); % crop out the relevant part
% optionally, you can filter/adjust the image
% you could try a median filter to reduce interference
% caused by the weave pattern
samppict = medfilt2(samppict,[11 11]);
% take a copy and adjust the input limits to cut off the dark areas
% the alpha of the bright spots is 70%
alph = imadjust(samppict,[0.78 1],[0 0.7]);
% even though we discarded the color, the image still needs
% to be RGB for image() to not colormap it -- which would
% ruin the colormapping on the contour plot
samppict = repmat(samppict,[1 1 3]);
hold on;
h1 = image(xlim,ylim,samppict);
h1.AlphaData = alph; % use the alpha map

Más respuestas (1)

KSSV
KSSV el 31 de Mzo. de 2022

0 votos

It is better to plot the contourf plot on the image.
  1. First read the image using imread.
  2. Plot the image with your desired tagging/ coordinates using image.
  3. Set the y-axis direction. (the direction might be reversed)
  4. On the image, use hold on
  5. Now plot the data you have using contourf.

7 comentarios

Steven Bellefontaine
Steven Bellefontaine el 31 de Mzo. de 2022
Thank you. I tried your suggestion but the contourf plot now blocks out the jpeg and it appears I cannot control the transparency of the contourf plot. I tried doing the opposite where I plot the contorf first and then overlay the image on the figure, but the quality makes the image unrecognizable. Is there anything else I can try?
KSSV
KSSV el 31 de Mzo. de 2022
Do you have aby expected output? Like what are your expectations..attach your data too.
Steven Bellefontaine
Steven Bellefontaine el 31 de Mzo. de 2022
Ideally, I would like it to be along the lines of the attached "OverlayAttempt" jpg but matching the original image of the sample such that the overlayed contorf plot is dimensionally accurate. The concentration should begin at the left edge of the first set of dots (The same image but shifted to the right). I would also like to make it as high quality/presentation worthy as possible.
Is there a way I can copy the data (the three contorf arguments) I am using from the workspace? There's a lot of pre-processing code and for this purpose, just the three arguments I send to the contourf plot are relevent. Thank you.
DGM
DGM el 31 de Mzo. de 2022
Editada: DGM el 31 de Mzo. de 2022
If I recall correctly, trying to make a transparent contourf plot is not so simple.
You'd have an easier time plotting the image on top of the contour and making it transparent. How exactly the photo needs to be registered with respect to the contour plot extents is something I cannot know.
Steven Bellefontaine
Steven Bellefontaine el 1 de Abr. de 2022
Hi DGM, the attached photo is what I was going for. For this, I converted the contourf plot to a jpeg, and overlayed it on top of the undeformed sample. I then moved the contourf plot to the correct spot on the undeformed sample with image(X,Y,C) where X and Y are the arrays spanning the range of which the contorf image needs to be on the undeformed sample. Is there is any way to make this more professional looking? This should satisfy my needs, but it would be nice to know of any better method in the future.
DGM
DGM el 1 de Abr. de 2022
What are the image coordinates that correspond to the boundaries of the plot box?
Steven Bellefontaine
Steven Bellefontaine el 1 de Abr. de 2022
I used a differently sized underformed sample image, which is 2128x1242. So using the following values lined it up.
h1 = image([290,2128],[150,1242],C)

Iniciar sesión para comentar.

Categorías

Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 31 de Mzo. de 2022

Editada:

DGM
el 2 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by