How i crop the image using image processing?

1 次查看(过去 30 天)
hi
i need a help , how i can crop the image automaticlly to get the selected image as the attached image.
  1 个评论
Walter Roberson
Walter Roberson 2025-7-3
I am unclear as to whether the line is something that you added to indicate the bounds that you want and is not present in the image, or if the line is present in the image ?

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2025-7-2
Your data are not easy to work with, and the scribe line yu added does not help!
I needed to get into other plot types and do some simple corrections to get this to work. I doubt if any simple morphological operations will work, so something similar to what i did is likely necessary.
Try this --
I1 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1836517/image.png');
[Imin,Imax] = bounds(I1(:))
Imin = uint8 0
Imax = uint8 255
I1g = im2gray(I1);
I1gd = double(I1g);
figure
imshow(I1g)
figure
image(I1g)
figure
surf(I1gd, EdgeColor='none')
colormap(turbo)
colorbar
axis([400 420 400 420 zlim])
Ax = gca;
Ax.YDir = 'reverse';
view(-25, 45)
axis('equal')
I1gd(I1gd == max(I1gd(:))) = 0;
[r,c] = find(I1gd >= 100);
[rmin,rmax] = bounds(r)
rmin = 337
rmax = 367
[cmin,cmax] = bounds(c)
cmin = 394
cmax = 424
figure
surf(I1gd(rmin:rmax, cmin:cmax), EdgeColor='none')
colormap(turbo)
colorbar
axis([400 420 400 420 zlim])
Ax = gca;
Ax.YDir = 'reverse';
axis('equal')
crop_pad = 10;
I1gds = I1gd(rmin-crop_pad:rmax+crop_pad, cmin-crop_pad:cmax+crop_pad);
figure
surf(I1gds, EdgeColor='none')
colormap(turbo)
colorbar
Ax = gca;
Ax.YDir = 'reverse';
% view(0,90)
view(-25, 45)
axis('equal')
xlabel('X')
ylabel('Y')
figure
imshow(I1(rmin-crop_pad:rmax+crop_pad, cmin-crop_pad:cmax+crop_pad, :))
Set the padding limits (crop_pad) to be whatever you want.
This is essentially 'automatic', however it rwaauires some intermediate steps. Without the scribe line (that I cannot figure out how to remove), the thresholding operation would be:
[r,c] = find(I1gd > 10);
or something similar. You may have to tweak the threshold (10 here) to return the result you want.
The essence of the code would then be simply:
I1 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1836517/image.png');
I1g = im2gray(I1);
I1gd = double(I1g);
I1gd(I1gd == max(I1gd(:))) = 0;
[r,c] = find(I1gd >= 100);
[rmin,rmax] = bounds(r);
[cmin,cmax] = bounds(c);
crop_pad = 10;
figure
imshow(I1(rmin-crop_pad:rmax+crop_pad, cmin-crop_pad:cmax+crop_pad, :))
This assumes that any other data you have are similar to the provided image.
Make appropriate changes to get the result you want.
.
  3 个评论
Star Strider
Star Strider 2025-7-3
My pleasure!
Those values should not equal zero for the image you posted, and my code should work. I need whatever image you are actually using in order to determine what the problem is. (I assume that they are all similar to the posted image.)
Otherwise, experiment with the threshold ('test_value' here) to detect the central region:
test_value = 10;
[r,c] = find(I1gd > test_value);
I cannot solve problems with data or images I do not have and are not available for me to work with.
If you post the image that is causing problems, I will see if it is possible to get my code to work with it.
Image Analyst
Image Analyst 2025-7-3
And be sure to post the original grayscale image, not the one you color mapped into an RGB image.
If the image is floating point instead of integer (Uint8) then save it in a .mat file.
save('answers.mat', 'YourFloatingPointImageNameHere');
and attach the answers.mat file.

请先登录,再进行评论。


Image Analyst
Image Analyst 2025-7-3
I made an attempt to recover your original image using rgb2ind. Try this. Adjust threshold if you want. Raise it if you want to extract only the brighter pixels, which will be a closer/smaller crop.
originalImage = imread('yasmeen.png');
subplot(3, 1, 1);
imshow(originalImage, []);
axis('on', 'image')
title('Original Image')
% Find limits of image greater than 9 gray levels.
mask = originalImage >= 9;
subplot(3, 1, 2);
imshow(mask, []);
axis('on', 'image')
title('Mask Image')
[r, c] = find(mask);
[topRow, bottomRow] = bounds(r)
topRow = 284
bottomRow = 356
[leftCol, rightCol] = bounds(c)
leftCol = 291
rightCol = 350
% Do the cropping
grayImage = originalImage(topRow:bottomRow, leftCol:rightCol);
subplot(3, 1, 3);
imshow(grayImage, []);
axis('on', 'image')
impixelinfo
title('Cropped Image')

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by