Cropping an image with coordinate values

26 views (last 30 days)
Sterne_17 on 2 Jan 2023
Commented: Image Analyst on 4 Jan 2023
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?

Image Analyst on 2 Jan 2023
Edited: Image Analyst on 2 Jan 2023
Try this:
[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
% Get rid of things touching the border.
% Label the image
% Eliminate the outermost blob, which will have a label of 1. Just keep blobs labeled 2 and up.
% Find the corners of the remaining blobs -- the bounding box.
row1 = min(r)
row1 = 424
row2 = max(r)
row2 = 790
col1 = min(c)
col1 = 437
col2 = max(c)
col2 = 542
% Do the cropping
subplot(2, 1, 2);
axis('on', 'image');
Sterne_17 on 4 Jan 2023
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.
Image Analyst on 4 Jan 2023
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.
That way nothing on the border or any of those spurious reflections will appear after masking.

KALYAN ACHARJYA on 2 Jan 2023
[rows,colm]=find(image==1);
Get the indices as rows & colm
3 CommentsShow 1 older commentHide 1 older comment
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!
Sterne_17 on 2 Jan 2023
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!

Categories

Find more on Image Arithmetic 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!