Returning cropped image on to original image after doing some analysis on that cropped portion
2 views (last 30 days)
Show older comments
Returning cropped image on to original image after doing some analysis on that cropped portion. Basically that cropped portion became contour now after doing some analysis, i just wanted to put it back to its original place. I cropped using imcrop, i have cropped image coordinates [Xmin Ymin Width Height]
Can anyone help me out to do this.
Thanks in advance
0 Comments
Accepted Answer
More Answers (1)
DGM
on 17 Jan 2023
Moved: Matt J
on 17 Jan 2023
It seems like this is something that would be easier done in-figure, rather than trying to cut up screenshots.
% ROI extents as subscript vectors
roiy = 30:150;
roix = 50:150;
% get image and sample region
inpict = imread('cameraman.tif'); % 256x256x1
% get the sample region and filter it or something
roisample = inpict(roiy,roix);
roisample = imgaussfilt(roisample,5);
% draw image (must be RGB!)
if size(inpict,3) == 1
safepict = repmat(inpict,[1 1 3]);
end
imshow(safepict); hold on
% overlay the contourf plot
[~,hc] = contourf(roix,roiy,roisample,10);
hc.LineStyle = 'none';
% if you want the contour plot to appear transparent
% overlay another copy of the image with alpha
%hi = imshow(safepict);
%hi.AlphaData = 0.1;
colormap(parula)
colorbar
7 Comments
DGM
on 30 Jan 2023
Edited: DGM
on 30 Jan 2023
contour() and contourf() both accept X and Y data as either vectors or as a meshgrid (matrix) format.
x = 1:10; % a 1x10 vector
y = 1:20; % a 1x20 vector
[X Y] = meshgrid(x,y); % two 10x20 matrices
Z = x.*y.'; % a 10x20 matrix
subplot(1,2,1)
contourf(x,y,Z) % using vector XData,YData
subplot(1,2,2)
contourf(X,Y,Z) % using matrix XData,YData
What matters is that the image object and the contour object have their XData and YData in comparable units. In order for that to work, either the contourf() object needs to have X and Y in pixels, or the call to imshow() needs to specify XData and YData in whatever coordinates the contourf() plot is using.
In the prior examples, I did everything in image coordinates. Plotting can still be done in image coordinates, even if the Z data isn't calculated in image coordinates. So long as the X and Y vectors/matrices you're feeding to contourf() are compatible in size with the Z data you give to contourf(), the values therein can be in any units.
figure
% ROI extents as subscript vectors (in image coordinates)
roiy = 30:150;
roix = 50:150;
% get image and sample region
inpict = imread('cameraman.tif'); % 256x256x1
% get the sample region
roisample = inpict(roiy,roix);
% say you're using some arbitrary X,Y to generate the Z data ...
x = linspace(-1.5,1.5,numel(roix));
y = linspace(-0.5,0.5,numel(roiy));
[X Y] = meshgrid(x,y);
roisample = double(roisample).*(X+Y); % some hypothetical operation (X and Y are not in px)
% draw image (must be RGB!)
if size(inpict,3) == 1
safepict = repmat(inpict,[1 1 3]);
end
imshow(safepict); hold on
% ...the X,Y data used for plotting can still be in px
[~,hc] = contourf(roix,roiy,roisample,10);
hc.LineStyle = 'none';
colormap(parula)
colorbar
axis on % just to show the coordinate system
If instead, you want to plot the image in the same coordinate space that was used to generate the Z data, then that's doable, but I think it's a lot more cumbersome. Note the range of the xticks and yticks.
figure
% ROI extents as subscript vectors (in image coordinates)
roiy = 30:150;
roix = 50:150;
% get image and sample region
inpict = imread('cameraman.tif'); % 256x256x1
% get the sample region and filter it or something
roisample = inpict(roiy,roix);
% say you're using some arbitrary X,Y to generate the Z data ...
x = linspace(-1.5,1.5,numel(roix));
y = linspace(-0.5,0.5,numel(roiy));
[X Y] = meshgrid(x,y);
roisample = double(roisample).*(X+Y); % some hypothetical operation (X and Y are not in px)
% rescale X,Y extents of image (in px) to the coordinate space used to generate Z
imgx = interp1(imrange(roix),imrange(x),[1 size(inpict,2)],'linear','extrap');
imgy = interp1(imrange(roiy),imrange(y),[1 size(inpict,1)],'linear','extrap');
% draw image (must be RGB!)
if size(inpict,3) == 1
safepict = repmat(inpict,[1 1 3]);
end
imshow(safepict,'xdata',imgx,'ydata',imgy); hold on % specify the non-px X,Y extents
% ... the X,Y data used for plotting are the same used to generate Z
[~,hc] = contourf(X,Y,roisample,10);
hc.LineStyle = 'none';
% adjust things so that dataAR allows image AR to be preserved
roiar = range(roix)/range(roiy);
contar = range(x)/range(y);
set(gca,'dataaspectratio',[contar/roiar 1 1])
colormap(parula)
colorbar
axis on % just to show the coordinate system
See Also
Categories
Find more on Red in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!