필터 지우기
필터 지우기

How can i crop multiple images

조회 수: 1 (최근 30일)
shru s
shru s 2017년 5월 28일
댓글: Image Analyst 2017년 5월 28일
Hello, I have, with the help of regionprops, drawn a bounding box around the parts I would like to crop. Could anyone tell me how i can crop the images and store it. Thank you. Edit: I am trying to crop handwritten characters.

채택된 답변

MathReallyWorks
MathReallyWorks 2017년 5월 28일
Hello shru,
Use this code for properly cropping the regions and saving them in a folder:
grayImage = imread('shapes.png');
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = grayImage(:, :, 2);
end
binaryImage = grayImage > 128;
binaryImage = imclearborder(binaryImage);
binaryImage = bwareaopen(binaryImage, 1000);
labeledImage = bwlabel(binaryImage, 8);
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
blobMeasurements = regionprops(labeledImage, 'BoundingBox');
for m=1:numberOfBlobs
BB(m,:) = blobMeasurements(m).BoundingBox;
end
txt=['A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K'];
% txt is Random matrix of character for naming the files for saving them
for i=1:numberOfBlobs
imagen = imcrop(grayImage, [BB(i,1)-5 BB(i,2)-5 BB(i,3)+10 BB(i,4)+10]);
figure,
imshow(imagen);
saveas(gcf,txt(i),'jpg'); %All cropped images are stored with the names A,B,C,D etc.
end
original image:
Result:
  댓글 수: 2
shru s
shru s 2017년 5월 28일
Beautiful piece of code. Thank you. But the thing is, I am trying to crop out handwritten characters. So this method is not helping me do that.
Image Analyst
Image Analyst 2017년 5월 28일
Printed or script? You forgot to attach your image.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by