cropping satellite image (RGB image)

hy everyone,,, i want to ask a question,, I have some satellite imagery that I downloaded from USGS. I want to doing cropping to the part of that image that unnecessary and take just the areas that I need. I want to ask how to cut the unnecessary part of that image and leaving only the parts that I need? and how to keep cropping I did, have same result for every image? because I need the same cropping area for each image.
can anyone help me? need reply soon.
best regards _trimurti_ God bless you.

Answers (2)

On one of the images, use
[croppedIMG, rect] = imcrop(IMG);
The value stored in to rect will then be the cropping rectangle. You can apply that to another image as an extra argument to imcrop:
croppedIMG2 = imcrop(IMG2, rect);

5 Comments

Can you show how to perform image cropping directly on a folder of image? because I will do the cropping on many images(about 100 images)and save it as a database. thanks
You cannot perform image cropping directly on a folder of images. You have to do the cropping on each image by reading it in and cropping it in memory.
Reading the FAQ should help you: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
i got it sir. And how to save the cropped image into the.jpg format, without reducing the image resolution. I've tried using imshow function to display the cropped image, after that I use the save as to save the image into the .jpg format, but the image resolution is reduced (my image size 17 MB now only 38 KB).
can you help me?
have code like below, (you can try to run it).
%Change the current folder to the folder of this m-file
%(The line of code below is from Brett Shoelson of The Mathworks.)
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=20;
%Read in standad MATLAB color demo images.
imagesFolder='';
if~exist(imagesFolder,'dir')
message=sprintf('Please browse to your image folder');
button=questdlg(message,'Specify Folder','OK','Cancel','OK');
drawnow;%Refresh screen to get rid of dialog box remnants.
if strcmpi(button,'Cancel')
return;
else
imagesFolder=uigetdir();
if imagesFolder==0
return;
end
end
end
%Read the directory to get a list of images.
filePattern=[imagesFolder,'\*.jpg'];
jpegFiles=dir(filePattern);
filePattern = [imagesFolder, '\*.tif'];
tifFiles = dir(filePattern);
filePattern = [imagesFolder, '\*.png'];
pngFiles = dir(filePattern);
filePattern = [imagesFolder, '\*.bmp'];
bmpFiles = dir(filePattern);
imageFiles = [jpegFiles; tifFiles; pngFiles; bmpFiles];
%Bail out if there aren't any images in that folder.
numberOfImagesProcessed = 0;
numberOfImagesToProcess = length(imageFiles);
if numberOfImagesToProcess <= 0
message = sprintf('I did not find any JPG, TIF, PNG, or BMP images
in the folder\n%s\nClick OK to Exit.', imagesFolder);
uiwait(msgbox(message));
return;
end
for k = 1 : numberOfImagesToProcess
%Read this one file.
baseFileName = imageFiles(k).name;
fullFileName = fullfile(imagesFolder, baseFileName);
OriginalImage = imread(fullFileName);
%Cropping Images
[croppedIMG1, rect] = imcrop(OriginalImage);
figure
imshow(croppedIMG1),title('Cropped Image1')
%Prompt user to continue.
promptMessage = sprintf('Currently displaying image #%d of %d:\n%s\n\nDo you want to\nContinue processing, or\nCancel processing?',numberOfImagesProcessed, numberOfImagesToProcess, baseFileName);
button=questdlg(promptMessage, 'Continue?', 'Continue', 'Cancel','Continue');
if strcmp(button, 'Cancel')
break;
end
%Prompt user to continue.
promptMessage = sprintf('Currently displaying image #%d of %d:\n%s\n\nDo you want to\nContinue processing, or\nCancel processing?',numberOfImagesProcessed, numberOfImagesToProcess, baseFileName);
button=questdlg(promptMessage, 'Continue?', 'Continue', 'Cancel','Continue');
if strcmp(button, 'Cancel')
break;
end
end
if numberOfImagesProcessed == 1
caption = sprintf('Done with demo!\n\nProcessed 1 image.\nCheck out the command window for the results');
else
caption = sprintf('Done with demo!\n\nProcessed %d images.\nCheck out the command window for the results', numberOfImagesProcessed);
end
Can you tell me how, when I press the continue button on that program then the next picture in the folder will follow the size of the cropping I did before, (like the function that previously you've given me:
[croppedIMG, rect] = imcrop (IMG);
croppedIMG2 = imcrop (img2, rect);
those functions works on the code below:
img1 = imread ('');
[croppedIMG1, rect] = imcrop (img1);
img2 = imread ('');
croppedIMG2 = imcrop (img2, rect);
% imshow
figure
imshow (croppedIMG1), title ('Cropped image1')
figure
imshow (croppedIMG2), title ('Cropped image2')
my problem now is how to insert the "croppedIMG2 = imcrop (img2, rect); into the first code above, to make subsequent cropping for all images in that folder. Because if i press continue on the program then I have to do the cropping again on the next picture so that the cropping results will be different for each image. It also displayed only cropped image for the first image.
Can you help me?
imwrite() the cropped images rather than using save() of the displayed version of the cropped image.

Sign in to comment.

Unhas,
To save the image in high quality, look for the print function
doc print
You can even set PaperPositionMode. I usually specify a landscape A4, then the resolution.
My sample:-
set(figurehnd,'PaperUnits','centimeters','PaperType','A4','PaperPosition',[0 0 29.7 21.0]);
print(figurehnd,'-djpeg','-r400',g);
figurehnd is the Figure handle variable. -r400 means resolution at 400 dpi.

1 Comment

See also this FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_save_my_figure.2C_axes.2C_or_image.3F_I.27m_having_trouble_with_the_built_in_MATLAB_functions.

Sign in to comment.

Categories

Asked:

on 6 Sep 2011

Community Treasure Hunt

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

Start Hunting!