Returning cropped image on to original image after doing some analysis on that cropped portion

11 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Matt J
Matt J il 14 Gen 2023
Modificato: Matt J il 14 Gen 2023
i0=floor(Ymin);
j0=floor(Xmin);
i1=i0+ceil(Height)-1;
j1=j0+ceil(Width)-1;
Image(i0:i1,j0:j1)=croppedImage %Put croppedImage back in original location
  26 Commenti

Accedi per commentare.

Più risposte (1)

DGM
DGM il 17 Gen 2023
Spostato: Matt J il 17 Gen 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 Commenti
HARISH KUMAR
HARISH KUMAR il 24 Gen 2023
Actually, i am modifing existing matlab code to get better output, even though i am new to matlab.
After looking at that code, as per my understanding, he created function, stated some inputs required for that function. In that function, he created mesgrid by using the size of that ROI and some stated inputs. Later some image correlation has happen, from this we got those three components which is used for generating contour.
I have realised that size of 1,2 and 3 components are same like same number of rows and columns.
Using these components i have generated contour ( when i try to overlay this contour on my original image, this contour is going and falling on top left corner of the image (irrespective of ROI))
Where as in your case, size of roix = 1*n, size of roiy = 1*m and size of roisample = n*m
DGM
DGM il 30 Gen 2023
Modificato: DGM il 30 Gen 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

Accedi per commentare.

Categorie

Scopri di più su Images in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by