how to cropt the image and save cropped part

i have an image i wanted to crop a part of the image and save the cropped part as image can any one help me for cropping and saving the image
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();

답변 (2개)

KSSV
KSSV 2016년 12월 5일

0 개 추천

I = imread('cameraman.tif');
[I2, rect] = imcrop(I);
imshow(I2) ;
imwrite(I2,'Example.jpg')
Extract the part you want, say it is I2. Using imwrite you can write the mage you want.

댓글 수: 8

sir i wanted to crop using free hand tool
hFH = imfreehand(gca);
pos = hFH.getPosition();
%%Do inteprolation
[nx,ny] = size(grayImage) ;
x = 1:nx ;
y = 1:ny ;
[X,Y] = meshgrid(x,y) ;
% make extracted grid
xi = min(pos(:,1)):max(pos(:,1)) ;
yi = min(pos(:,2)):max(pos(:,2)) ;
[Xi,Yi] = meshgrid(xi,yi) ;
Hi = interp2(X,Y,double(grayImage),Xi,Yi) ;
% Create a binary image ("mask") from the ROI object.
% binaryImage = hFH.createMask();
% keep only points which are inside the extracted region
idx = inpolygon(Xi(:),Yi(:),pos(:,1),pos(:,2)) ;
Hii = 255*ones(size(Hi)) ;
Hii(idx) = Hi(idx) ;
Hii = uint8(Hii) ;
figure
imshow(Hii) ;
Shakir Ishaq
Shakir Ishaq 2018년 2월 5일
편집: Image Analyst 2018년 2월 5일
I have coded for face detection and cropped the images. Now want to save the cropped images in a folder separately.
Here my code is:
img = imread('4.BMP');
% figure(1);
% imshow(img);
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold = 7 ;
BB = step(FaceDetect, img);
figure(2);
imshow(img);
for i = 1 : size(BB,1)
rectangle('Position', BB(i,:), 'LineWidth', 1, 'LineStyle', '-', 'EdgeColor', 'r');
end
for i = 1 : size(BB, 1)
face = imcrop(img, BB(i, :));
figure(3);
% subplot(6, 6, i);
imshow(face);
end
Please help me in further coding for saving the cropped images. I will wait for response.
Thanks
Try this:
for k = 1 : size(BB, 1)
face = imcrop(img, BB(k, :));
figure;
% subplot(6, 6, k);
imshow(face);
drawnow;
thisFileName = sprintf('Face #%d.png', k);
fullFileName = fullfile(pwd, thisFileName);
imwrite(face, fullFileName);
end
By trying the above code it give error:
Error using imwrite (line 454) Unable to open file "C:\Program Files\MATLAB\R2016a\bin\Face #1.png" for writing. You might not have write permission.
Error in facedetection1 (line 29) imwrite(face, fullFileName);
Thanks for helping. I got it.
Ade Aulya
Ade Aulya 2018년 8월 18일
how could u solve it, sir ? if u don't mind for sharing it
The code I gave was this.
for k = 1 : size(BB, 1)
face = imcrop(img, BB(k, :));
figure;
% subplot(6, 6, k);
imshow(face);
drawnow;
thisFileName = sprintf('Face #%d.png', k);
fullFileName = fullfile(pwd, thisFileName);
imwrite(face, fullFileName);
end
You'll see it if you click the "Show older comments" link above.

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

Aytaç Tok
Aytaç Tok 2021년 3월 7일

0 개 추천

Hello there. I want to get an object in the rgb picture, how can I do that. For example, there is a banana on the table and the background is black. How can I get only the banana picture from the picture. ı dont want to take by boundingbox. I must take with border coordinates because ı need just object.There should be no background in the picture I'm waiting for your help.pleaseeee

댓글 수: 6

How do you want to represent the irregular-shaped object? You would not be able to use a numeric array for it, as numeric arrays cannot have jagged edges as would be needed to not have any background around the curve.
Think of a photo of a banana on the table. I will crop the picture from here to get a new picture. In this new picture, all the pixels will belong to the banana picture. So the size of the original picture and the cropped picture will not be the same size.In the code below, I got the banana picture from the original picture, but the other pixels turned black. I don't want to get the black pixels.ı just want to rgb pixel of banana
e=imread('banana.jpeg');
l=imbinarize(rgb2gray(e));
[r c]=size(l);
bw=I;
bw=bwareaopen(sb,10);
se=strel('disk',5);
bw=imclose(bw,se);
bw=imfill(bw,'holes');
se2=strel('disk',20);
bw2=imerode(bw,se2);
x=e;
re=x(:,:,1);
ge=x(:,:,2);
bl=x(:,:,3);
tr=[];
temp=1;
gemp=1;
for i=1:r
for j=1:c
if (bw2(i,j)==1)
tr(temp,gemp)=re(i,j);
end
gemp=gemp+1;
end
temp=temp+1;
gemp=1;
end
tg=[];
temp=1;
gemp=1;
for i=1:r
for j=1:c
if (bw2(i,j)==1)
tg(temp,gemp)=ge(i,j);
end
gemp=gemp+1;
end
temp=temp+1;
gemp=1;
end
temp=1;
gemp=1;
tb=[];
for i=1:r
for j=1:c
if (bw2(i,j)==1)
tb(temp,gemp)=bl(i,j);
end
gemp=gemp+1;
end
temp=temp+1;
gemp=1;
end
rsm=cat(3,tr,tg,tb);
rsm1=uint8(rsm);
Are you asking to crop to the largest rectangle that includes only the banana and none of the background, even though that crops a bunch of the banana as well?
Aytaç Tok
Aytaç Tok 2021년 3월 10일
The picture shows the original picture and the picture with only the banana. I just want to crop the banana picture from the second picture so I don't want the black part in the second picture. how can I do that
Aytaç Tok
Aytaç Tok 2021년 3월 10일
The background is a problem because I will take only the banana object in this picture and operate on it.
You cannot do that. The closest you can get is
with all of the boundary taken out. But you cannot get rid of the black part to the lower left, for example, because doing so would involve creating a "ragged" array where those black pixels literally do not exist even though pixels above or to their right exist.

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

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

질문:

2016년 12월 5일

댓글:

2021년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by