Effacer les filtres
Effacer les filtres

crop a circle in images and have "Integers can only be combined with integers of the same class, or scalar doubles." problem I can't solve...

2 vues (au cours des 30 derniers jours)
I want to crop circles in a image. With the code below, I encounter an Error : "Integers can only be combined with integers of the same class, or scalar doubles." How to address this problem. Thank you!
Img_filename=ls('C:\Users\User\Desktop\data science\0_deg\*.tif');
I = imread('1.tif');
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= 400; %center of circle row value. DON'T CHANGE
radius= 400; %radius of circle
StepSizeRow= 80;
StepSizeCol= 120;
c_col=400; %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\data science\0_deg\Cropped',sprintf('Cropped_Parallel_WA_A1_5x_20_400Radius_%d.jpg',count));
imwrite(MM, FileName)
end
col_min=col_min+StepSizeCol;
col_max= col_max+StepSizeCol;
end
% figure (1);
% imshow(croppedImage)
  2 commentaires
Sai Sri Pathuri
Sai Sri Pathuri le 8 Juil 2020
I was unable to reproduce the error at my end. At which line did you encounter the error? Can you also attach the tif file you are using?
Guan-Lin Chen
Guan-Lin Chen le 8 Juil 2020
Thank you for trying to solve my problem.
I found that the error was because of cropping area. I shrink down the cropping area and the code works.
Thank you!

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by