how to cropt the image and save cropped part
Mostra commenti meno recenti
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();
Risposte (2)
KSSV
il 5 Dic 2016
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 Commenti
andhavarapu lokesh
il 5 Dic 2016
KSSV
il 5 Dic 2016
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
il 5 Feb 2018
Modificato: Image Analyst
il 5 Feb 2018
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
Image Analyst
il 5 Feb 2018
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
Shakir Ishaq
il 6 Feb 2018
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);
Shakir Ishaq
il 7 Feb 2018
Thanks for helping. I got it.
Ade Aulya
il 18 Ago 2018
how could u solve it, sir ? if u don't mind for sharing it
Image Analyst
il 18 Ago 2018
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
il 7 Mar 2021
0 voti
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 Commenti
Walter Roberson
il 7 Mar 2021
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.
Aytaç Tok
il 8 Mar 2021
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);
Walter Roberson
il 8 Mar 2021
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
il 10 Mar 2021
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
il 10 Mar 2021
The background is a problem because I will take only the banana object in this picture and operate on it.
Walter Roberson
il 12 Mar 2021
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.
Categorie
Scopri di più su Convert Image Type in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!