Use mask to crop circle images from a picture with Matlab

3 views (last 30 days)
The code I use here is perfect for cropping circle images from my '294.jpg' image
The cropping order is from top left to bottom left then go to second column from top to bottom and then to the third column so on and so forth..
How to change the cropping order from top left to top right and the go the second "row" left to right and then to the third "row" left to right?
clear all, close all, clc;
Img_filename=ls('C:\Users\User\Desktop\294.jpg');
I = imread('294.jpg');
imageSize = size(I);
%initial values: c_row=400, col_min=1, col_max=800, row_max=800, row_min= 1, radius = 400, c_col= 400
c_row= 40; %center of circle row value. DON'T CHANGE
radius= 40; %radius of circle
StepSizeRow= 10;
StepSizeCol= 50;
%%
c_col=50; %center of circle column value. CHANGE BY 120
col_min= c_col-radius+1; %column val left side of bounding box. CHANGE BY 120
col_max= c_col+radius; %column val right side of bounding box. CHANGE BY 120
[rows, cols, rgb]= size(I);
count = 0;
for j = c_col:StepSizeCol: cols-radius
row_min=c_row-radius+1; %row val top of bounding box. DON'T CHANGE
row_max=c_row+radius; %row val bottom of bounding box. DON'T CHANGE
for i= c_row:StepSizeRow:rows-radius
ci = [i, j, radius]; % center and radius of circle ([c_row, c_col, r]) og size (1920, 2560)
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = uint8((xx.^2 + yy.^2)<ci(3)^2);
croppedImage = uint8(zeros(size(I)));
croppedImage(:,:,1) = I(:,:,1).*uint8(mask);
croppedImage(:,:,2) = I(:,:,2).*uint8(mask);
croppedImage(:,:,3) = I(:,:,3).*uint8(mask);
sp(1) = col_min; %min(floor(p(1)), floor(p(2))); %xmin
sp(2) = row_min; %min(floor(p(3)), floor(p(4))); %ymin
sp(3) = col_max; %max(ceil(p(1)), ceil(p(2))); %xmax
sp(4) = row_max; %max(ceil(p(3)), ceil(p(4))); %ymax
row_min=row_min+StepSizeRow;
row_max=row_max+StepSizeRow;
% Index into the original image to create the new image
MM = croppedImage(sp(2):sp(4), sp(1): sp(3),:);
%Display the subsetted image with appropriate axis ratio
count = count+1;
% figure;
% imshow(croppedImage)
% figure; image(MM); axis image;
FileName = fullfile('C:\Users\User\Desktop\Tensorflow\1cm 45deg circle cropped',sprintf('Cropped_Parallel_5x_20_400Radius_%d.jpg',count));
imwrite(MM, FileName)
end
col_min=col_min+StepSizeCol;
col_max= col_max+StepSizeCol;
end

Answers (1)

Shadaab Siddiqie
Shadaab Siddiqie on 8 Oct 2020
From my understanding you want to change the order of cropping the image from column wise to row wise. You can try interchanging the for loops and few other variables .Here is a code that might help (make sure to change the file name).
clear all, close all, clc;
Img_filename=ls('happy_dog.jpg');
I = imread('happy_dog.jpg');
imageSize = size(I);
%initial values: c_row=400, col_min=1, col_max=800, row_max=800, row_min= 1, radius = 400, c_col= 400
c_row= 40; %center of circle row value. DON'T CHANGE
radius= 40; %radius of circle
StepSizeRow= 10;
StepSizeCol= 50;
%%
c_col=50; %center of circle column value. CHANGE BY 120
row_min=c_row-radius+1; %row val top of bounding box. DON'T CHANGE
row_max=c_row+radius; %row val bottom of bounding box. DON'T CHANGE
[rows, cols, rgb]= size(I);
count = 0;
for i= c_row:StepSizeRow:rows-radius
col_min= c_col-radius+1; %column val left side of bounding box. CHANGE BY 120
col_max= c_col+radius; %column val right side of bounding box. CHANGE BY 120
for j = c_col:StepSizeCol: cols-radius
ci = [i, j, radius]; % center and radius of circle ([c_row, c_col, r]) og size (1920, 2560)
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = uint8((xx.^2 + yy.^2)<ci(3)^2);
croppedImage = uint8(zeros(size(I)));
croppedImage(:,:,1) = I(:,:,1).*uint8(mask);
croppedImage(:,:,2) = I(:,:,2).*uint8(mask);
croppedImage(:,:,3) = I(:,:,3).*uint8(mask);
sp(1) = col_min; %min(floor(p(1)), floor(p(2))); %xmin
sp(2) = row_min; %min(floor(p(3)), floor(p(4))); %ymin
sp(3) = col_max; %max(ceil(p(1)), ceil(p(2))); %xmax
sp(4) = row_max; %max(ceil(p(3)), ceil(p(4))); %ymax
col_min=col_min+StepSizeCol;
col_max=col_max+StepSizeCol;
% Index into the original image to create the new image
MM = croppedImage(sp(2):sp(4), sp(1): sp(3),:);
%Display the subsetted image with appropriate axis ratio
count = count+1;
% figure;
imshow(croppedImage)
% figure; image(MM); axis image;
%FileName = fullfile('C:\Users\User\Desktop\Tensorflow\1cm 45deg circle cropped',sprintf('Cropped_Parallel_5x_20_400Radius_%d.jpg',count));
%imwrite(MM, FileName)
end
col_min=col_min+StepSizeCol;
col_max= col_max+StepSizeCol;
end

Community Treasure Hunt

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

Start Hunting!