Cropping an image with coordinate values

I have an image with 848*1102 pixels after edge detection, and I want to crop out a part of the image accurately, so I need to use the coordinate values of the top left and bottom right points. How do I get the coordinates of these two points and crop the image?

 Accepted Answer

Try this:
grayImage = imread('001.jpg');
[rows, columns, numberOfColorChannels] = size(grayImage)
rows = 962
columns = 1286
numberOfColorChannels = 3
if numberOfColorChannels == 3
grayImage = grayImage(:, :, 2);
end
subplot(2, 1, 1);
imshow(grayImage);
axis('on', 'image')
% Binarize
mask = grayImage > 128;
% Get rid of things touching the border.
mask = imclearborder(mask);
% Label the image
[LabeledImage, numBlobs] = bwlabel(mask);
% Eliminate the outermost blob, which will have a label of 1. Just keep blobs labeled 2 and up.
mask = ismember(LabeledImage, 2:numBlobs);
% Find the corners of the remaining blobs -- the bounding box.
[r, c] = find(mask);
row1 = min(r)
row1 = 424
row2 = max(r)
row2 = 790
col1 = min(c)
col1 = 437
col2 = max(c)
col2 = 542
% Do the cropping
mask = mask(row1:row2, col1:col2, :);
subplot(2, 1, 2);
imshow(mask);
axis('on', 'image');

6 Comments

Thank you so much! It's very helpful to me! Since I have to process a series of images, I would like to ask you again if this processing (procedure) is applicable to other similar images. For example, as shown in the attachment. Because I want to process this series of images and then convert them into 3D images.
Dear Sir,
i want to remove the white borders from this set of images, not just crop out the image I want (this white outline at the bottom of the image), so I don't know if it will affect my subsequent conversion of 2D images to 3D images using coordinates, since the origin of the pixel coordinate system is in the center of the original image (unprocessed image). So I would like to ask you if the program needs to be modified to keep most of the black parts and to crop out the black borders.
Thank you very much.
These images all look a lot like they were saved from a figure. If that's the case, then that's why there's a bunch of white padding on the images. You'll probably find that the sizes don't match the original image either.
Don't save screenshots of images. Write clean, correctly-sized images with imwrite() instead of trying to fix screenshots.
% create a test image
inpict = imread('cameraman.tif');
edgemap = edge(inpict);
% display the image and save a screenshot
imshow(edgemap)
saveas(gcf,'savedfig.png')
% write the image directly
imwrite(edgemap,'edgemap.png')
% to test, read the images back again
badpict = imread('savedfig.png');
goodpict = imread('edgemap.png');
% original size
size(inpict)
ans = 1×2
256 256
% a screenshot is resized, padded, and has changed depth and class
size(badpict)
ans = 1×3
656 874 3
% the properly saved image is fine
size(goodpict)
ans = 1×2
256 256
Also, don't use JPG for any files that need to be processed further, especially not images with hard edges. PNG takes up less space (for this type of content) and doesn't damage the images. This is a section from one of your images (levels adjusted for emphasis).
Thanks @DGM for this information. @Changlong Li Make sure you show hidden comments and read @DGM's explanation.
@Changlong Li what part(s) of my final image did you not like? You say you want to get rid of the white border. I also cropped it so there is no black border -- on the first and last row and column you will find at least one white pixel so they're not completely black. I did exactly what you asked for so what's the problem? Please indicate the parts I'm including that you don't want. And this time do like @DGM says and don't save screenshots, and don't save anything in JPG format, which corrupts the image. Use imwrite to save your starting image in PNG format. And post another image where you have circles or arrows pointing to the places in the image that you don't want that still remain when using my solution.
Dear Sir,
The program you have written has helped me a lot and I thank you very much. Because I'm still a beginner, I don't have in-depth knowledge about it.
I would like to describe how my images were generated: a line laser was shone on a metal sphere, which was rotating, and I captured a series of images under that condition with an industrial camera. First I grayed out the images with MATLAB, then I used the canny algorithm for edge detection, after that the resulting images need to be processed with the program you wrote for them. I'm confused because the resolution and file size of the images have become smaller after processing with the program I wrote (grayscale and edge detection), because there is something wrong with the way I saved the images?
I have converted the initial image from jpg to png and then repeated the steps I described above: grayscale and edge detection. I have a question: as I tried to ask you before, I want to crop a series of images to generate a 3D image, and I don't know if the size of the cropped image will affect this step, this is what I want to ask you. If this will not affect my subsequent steps, then just removing the white border as the crop line will be fine.
Attached are a few samples of the image I converted and reprocessed from the initial image format and the image I marked with a color pen, first I want to remove the white border on the image, I draw the approximate location of the crop line with a red dotted line (just remove the white border), after cropping the image I don't need the axes and titles. After cropping the image, I have to remove some invalid white parts due to the reflection of the laser on the surface of the sphere, so that only a part of the surface of the sphere remains. I have circled in red the invalid white parts to be eliminated. The final part to be retained is circled in blue. I need to process a series of images, each with different details, so I need a generalized method.
Best wishes.
Since it's a sphere, and the curved part won't move very much from image to image, I suggestion you just make up a template mask that you can multiply by your image to erase everything outside it. Just take the attached mask and set your gray scale image, or segmented binary image, outside it to zero.
grayImage(~mask) = 0;
That way nothing on the border or any of those spurious reflections will appear after masking.

Sign in to comment.

More Answers (1)

[rows,colm]=find(image==1);
Get the indices as rows & colm

3 Comments

Then, to crop
row1 = min(rows)
row2 = max(rows)
col1 = min(colm)
col2 = max(colm)
yourImage = yourImage(row1:row2, col1:col2, :);
imshow(yourImage);
axis('on', 'image');
By the way, it may not be necessary for you to crop it unless you just want to see it bigger when you display it. Also you might want to call imclearborder to get rid of that thick white frame around it.
If you want to get rid of the thin white rectangular outline also, then let me know.
Sterne_17
Sterne_17 on 2 Jan 2023
Edited: Sterne_17 on 2 Jan 2023
Hello, my aim is to remove the white border and keep the rest of the pattern. Please help me. Thanks!
Hi, can you explain the code of the first step (Get the indices as rows & colm) in more detail? Because I can't get it to run. Thanks!

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!