Effacer les filtres
Effacer les filtres

how to crop image automatically to 512x512px?

16 vues (au cours des 30 derniers jours)
Dayangku Nur Faizah Pengiran Mohamad
Hello everyone. I want to crop with the size of 512x512px. But matlab only save the image as 10x9 uint8. and also, I want it to crop at the center of the lesion without any specific area. can the matlab do such that task? here's my code given below and result of the image that not at the center of the lesion.
close all;
I=imread('1_245.jpg');
figure, imshow(I);
I=rgb2gray(I);
BW=I>100;
figure,imshow(BW);
labeledImage = bwlabel(BW);
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
I2=imcrop(I,[thisBB(1),thisBB(2),thisBB(3),thisBB(4)]);
[rows, cols, depth]=size(I2);
if rows*cols>100
I2=imresize(I2,[512 512]);
figure,imshow(I2);
end
end
  1 commentaire
DGM
DGM le 9 Mai 2024
I don't really know what you want, since this code seems to be at odds with what's being described. The given code simplifies to this:
I = imread('1_245.jpg');
I = im2gray(I);
BW = I > 100;
imshow(BW);
measurements = regionprops(BW,'BoundingBox');
for k = 1 : numel(measurements)
thisBB = measurements(k).BoundingBox;
thisIm = imcrop(I,thisBB); % the cropped image
% if the box area is above a certain size
if prod(thisBB(3:4)+1) > 100
% stretch the image, misrepresenting its actual shape
thisIm = imresize(thisIm,[512 512]);
% display the region??
imshow(thisIm);
drawnow
pause(0.1)
end
end
If you're after the green annotation, it doesn't make sense to me that you're looking for all bright gray regions. If you're wanting to crop a specific area for illustration or analysis, it doesn't make sense to me that you'd stretch it out and throw away all the information about its original shape.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Mai 2024
Modifié(e) : Walter Roberson le 10 Mai 2024
RegionSize = 512;
I=imread('1_245.jpg');
figure, imshow(I);
I=rgb2gray(I);
BW=I>100;
figure,imshow(BW);
labeledImage = bwlabel(BW);
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
thismid = thisBB(1:2) + thisBB(3:4)/2;
startpos = floor(max(1, thismid - RegionSize/2));
I2 = imcrop(I,[startpos, RegionSize, RegionSize]);
filename = "l_245_crop_" + k + ".jpg";
imwrite(I2, filename);
end

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by