how to crop image automatically to 512x512px?
Show older comments
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 Comment
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.
Accepted Answer
More Answers (0)
Categories
Find more on Color and Styling 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!
