how to crop image automatically to 512x512px?

3 ビュー (過去 30 日間)
Dayangku Nur Faizah Pengiran Mohamad
編集済み: Walter Roberson 2024 年 5 月 10 日
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 件のコメント
DGM
DGM 2024 年 5 月 9 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2024 年 5 月 10 日
編集済み: Walter Roberson 2024 年 5 月 10 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by