Scan image and crop it to certain size
Show older comments
Hi,
I have two different size of images in one folder
What I am trying to do with these two different size of image is:
- Trying to scan an image row by row to see the area or section with highest intensity of white pixel.
- With that data I am trying to crop it to 320x320 image and save it to different folder
Below is my code that I constructed and working with.
I am very new to Matlab and coding it for image processing so hope you understand the mess.
data = '2018.png';
baseFileName = imread(data);
[rows, columns]=size(baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
originalImage = imread(fullFileName);
binaryImage = originalImage >= 1;
binaryImage = bwareafilt(binaryImage, [1, inf]); % Extract only the largest.
Pixels = sum(binaryImage(:)); % Check total pixel
% Find the bounding box.
[blobRows, blobColumns] = find(binaryImage);
row1 = min(blobRows);
row2 = max(blobRows);
col1 = min(blobColumns);
col2 = max(blobColumns);
if row2 <= 322
for x = 1:160
for y = 1:2
im_buff = binaryImage(y:(y+320-1), x:(x+320-1));
x = x + 1;
% Crop it.
crop = imcrop(im_buff, [row1 col1 320 320]);
img1g_NB = length(crop(crop~=0));
% Display the cropped image.
imshow(crop);
end
end
else
for x = 1:2
for y = 1:160
im_buff = binaryImage(y:(y+320-1), x:(x+320-1));
y = y + 1;
% Crop it.
crop = imcrop(im_buff, [row1 col1 320 320]);
img1g_NB = length(crop(crop~=0));
% Display the cropped image.
imshow(crop);
end
end
end
Accepted Answer
More Answers (1)
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1243147/image.png') ;
[y,x] = find(I) ;
% Get bounding box
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
I1 = I(y0:y1,x0:x1) ;
subplot(211)
imshow(I) ;
subplot(212)
imshow(I1)
3 Comments
dominic
on 26 Dec 2022
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

