Why images dimensions goes up when cropping it

I enclose two images. Both are the same image.
The original ('A1b.jpg') has the beggining point of the imaging window at [136,120] and end point at [548,120].
The ('A1b(al).jpg') is the same image cropped but the imaging window length is outrageous enlarged with starting point at [180,96] and end point at [1237,96].
Why this difference? I only cropped the image under certain dimensions. Why it took that scaling and stretching?

 Risposta accettata

Image Analyst
Image Analyst il 21 Gen 2019
Looks like you're saving the figure window itself instead of the image in the axes. When you save the figure window, you get all that white padding, and the image is screen pixels, which may not be the same as the underlying image pixels because the image may have been zoomed or subsampled to fit your figure window.

15 Commenti

How do I make it have the same resolution but be cropped to the dimensions I want to
Just crop the image by indexing and save it with imwrite
croppedImage = rgbImage(row1:row2, col1:col2, :);
imwrite(croppedImage, filename);
Error using imwrite (line 420)
Expected DATA to be nonempty.
Why this error?
Either your image or filename is empty. Use normal debugging methods to figure out which one, and why.
Image Analyst.
I am not doing anything more than just cropping the image to the size of the imaging window that we care because of the valuable information. I also make it a bit smaller so the automated algorithm segmentation will be faster.
I use this code
for iter = 1:NumIter
phi =evolution_LGD(Img,phi,epsilon,Ksigma,KONE,KI,KI2,mu,nu,lambda1,lambda2,timestep,alf);
if(mod(iter,50) == 0)
figure(2),
imagesc(uint8(Img),[0 255]),colormap(gray),axis off;axis equal,title(num2str(iter))
hold on,[c,h] = contour(phi,[0 0],'r','linewidth',1); hold off
pause(0.02);
end
end
The problem is that I cannot use imwrite since if I use imwrite(Img,[]) nothing happens and even if something eventually happens, the outcome image will not be what I want. Since, the active contour I am using for segmentation is not part of the image, how else can I save the image, if not the usual "Save As ...".
I need to give a specific value for imwrite to work. This value needs to be the image AND the contour overlaid. Otherwise, I get error.
When I save it e.g. as jpg (I know I'll try another format, but at this time with jpg format I am most familiar with), the rows and columns are messed.
Original image start from 136 to 548 Columns and after cropping I start from 73 to 508. Why this loss is happening? What is the reason causing this loss? The width of imaging window is the same cropped. Only the height is changing
As you should know by now, attach all files and code necessary for me to replicate your problem. And please have your code have just one line of code per line, not 5 or 6.
Image Analyst, I use this code for automated segmentation
And the images I attach
clc;clear all;close all;
Img=imread('A6.jpg');
Img = imcrop(Img,[254.25 161.75 445 154.5])
Img = double(Img(:,:,1));
imadjust(Img);
imsharpen(Img);
histeq(Img);
adapthisteq(Img);
NumIter = 1400; %iterations
timestep=0.1; %time step
mu=0.1/timestep;% level set regularization term, please refer to "Chunming Li and et al. Level Set Evolution Without Re-initialization: A New Variational Formulation, CVPR 2005"
sigma = 3;%size of kernel
epsilon = 1;
c0 = 2; % the constant value
lambda1=1.03;%outer weight, please refer to "Chunming Li and et al, Minimization of Region-Scalable Fitting Energy for Image Segmentation, IEEE Trans. Image Processing, vol. 17 (10), pp. 1940-1949, 2008"
lambda2=1.0;%inner weight
%if lambda1>lambda2; tend to inflate
%if lambda1<lambda2; tend to deflate
nu = 0.0005*255*255;%length term
alf = 20;%data term weight
figure,imagesc(uint8(Img),[0 255]),colormap(gray),axis off;axis equal
[Height Wide] = size(Img);
[xx yy] = meshgrid(1:Wide,1:Height);
phi = (sqrt(((xx - 64).^2 + (yy - 76).^2 )) - 15);
phi = sign(phi).*c0;
Ksigma=fspecial('gaussian',round(2*sigma)*2 + 1,sigma); % kernel
ONE=ones(size(Img));
KONE = imfilter(ONE,Ksigma,'replicate');
KI = imfilter(Img,Ksigma,'replicate');
KI2 = imfilter(Img.^2,Ksigma,'replicate');
figure,imagesc(uint8(Img),[0 255]),colormap(gray),axis off;axis equal,
hold on,[c,h] = contour(phi,[0 0],'r','linewidth',1); hold off
pause(0.5)
tic
for iter = 1:NumIter
phi =evolution_LGD(Img,phi,epsilon,Ksigma,KONE,KI,KI2,mu,nu,lambda1,lambda2,timestep,alf);
if(mod(iter,50) == 0)
figure(2),
imagesc(uint8(Img),[0 255]),colormap(gray),axis off;axis equal,title(num2str(iter))
hold on,[c,h] = contour(phi,[0 0],'r','linewidth',1); hold off
pause(0.02);
end
end
toc
Check the first row and column of the imaging window at the original image and the same at the cropped one. They are different. Also the length of the image (number of columns) are different among the two images. Under this condition, the total values of pixels in every column will be also different as saved in the xls file. I don't want this. I wish I could keep the cropped image so the active contour should run faster and accurate (if you apply the whole-original image for automated segmentation the contour just get messed with all this information. It doesn't segment right). But having exactly the same columns as the original image where the manual segmentation took place.
Although, I will probably use an interpolation method to match the differences between the two images in units of length. I would pleased to know whether there are alternative ways.
You are so correct Image Analyst. I just realized that if I save the cropped image with the figure window maximized the row, columns goes insanely up (175-1237). But when I save the cropped image with normal figure window size it remains more or less the same as the original image (74-508)
Still there is a small difference with the other images (135-548).
Do you know where this difference is caused by?
I will use interpolation to fix the gaps but I'd like to know the root of this trouble
Saving the cropped image does not depend on what the size of the figure is that holds the axes the image is contained in. The size of the saved image will only change with the figure size if you save the figure as an image with saveas() (usually a bad idea) rather than saving the image variable itself with imwrite().
But I use that script
for iter = 1:NumIter
phi =evolution_LGD(Img,phi,epsilon,Ksigma,KONE,KI,KI2,mu,nu,lambda1,lambda2,timestep,alf);
if(mod(iter,50) == 0)
figure(2),
imagesc(uint8(Img),[0 255]),colormap(gray),axis off;axis equal,title(num2str(iter))
hold on,[c,h] = contour(phi,[0 0],'r','linewidth',1); hold off
pause(0.02);
end
end
Which means that using the command
imwrite(Img,'ImageName.jpg')
Won't work. SInce, the outcome image is a combination of image + active contour. When I use the command of imwrite as above I get a white rectangular shape. How do I create the final outcome image combined with the active contour as one variable.
One variable (e.g. Img) needs to enclose the overall image and contour.
@Stelios: I do not know, what the variable Img is. You convert it by uint8, so maybe it is a double matrix of integer values from 0 to 255? What exactly is "a combination of image + active contour"?
It means that the active contour is being applied on top of the image and it is not part of it. It means, that when I try to imwrite() using the image variable, IT WON'T include the segmentation contour as well since they are different things.
See, my code above, evolution_LGD( automated segmentation method) is applied on the image but cannot be exported as jpg or any other format with the image as well.
May you know a way to help me please?
If you did something to the image, like drew things into the overlay above it, then you'll need to use getframe() to get the image "as you see it".
What a pity that evolution_LGD does not contain a useful help text, which explains, what the output is. Using getframe has the drawback, that you work with screen resolution. If the processed image is displayed with a scaling, the data are resampled. Therefore it might be more useful to obtain the output of the contour as a matrix with the original dimensions as the image and use it as overlay to join it with the image.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Images in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by