hi i used the following code to crop an image and i got the answer, but i want to apply this to a large dataset of masks and save the results to another folder. How do i do this?

1 vue (au cours des 30 derniers jours)
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 25
grayImage = imread('C:\Users\MAHESH\Desktop\prjct\mask\mask\aeroplane\1_2008_000077.jpg');
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original image.
subplot(1, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Color Image, %s', grayImage);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
mask = grayImage > 7;
% Take the largest blob only.
mask = bwareafilt(mask, 1);
% Find the bounding box
props = regionprops(mask, 'BoundingBox');
% Crop the image to the bounding box.
croppedImage = imcrop(grayImage, props.BoundingBox);
% Display the image.
subplot(1, 2, 2);
imshow(croppedImage);
axis on;
caption = sprintf('Cropped Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;

Réponses (1)

KALYAN ACHARJYA
KALYAN ACHARJYA le 18 Mai 2019
Modifié(e) : KALYAN ACHARJYA le 18 Mai 2019
clc;
clear all;
close all;
format short g;
format compact;
fontSize = 25
% Make sure that all images are in same working directory (current directory)
list=dir('*.jpg');
for j=1:length(list)
grayImage=imread(list(j).name);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels]=size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
mask = grayImage > 7;
% Take the largest blob only.
mask = bwareafilt(mask, 1);
% Find the bounding box
props = regionprops(mask, 'BoundingBox');
% Crop the image to the bounding box.
croppedImage = imcrop(grayImage, props.BoundingBox);
% Next line:Comlplete path like C:\kalyan\segmentation_result\im, save file
% name as im1 im2 etc
%........segmentation_result target folder name
destination='complete path\im';
imwrite(croppedImage,[destination,num2str(j),'.jpg']);
end
Some minor changes may be required, just undestand how call images and same the result cropped images in different folder.
  2 commentaires
MANISHA P
MANISHA P le 20 Mai 2019
I get an error llike this while running the programerror.PNG
KALYAN ACHARJYA
KALYAN ACHARJYA le 20 Mai 2019
Modifié(e) : KALYAN ACHARJYA le 20 Mai 2019
It does works, I have answered the same in multiple questions
% Make sure that all images are in same working directory (current directory)
list=dir('*.jpg');
for j=1:length(list)
grayImage=imread(list(j).name);
% do your work
% Your main code after image read
% if gray conversion requited add one line rgb2gray....
% Lets say result is the croped images
result=... % which is croped image
% Next line:Comlplete path like C:\kalyan\segmentation_result\im, save file
% name as im1 im2 etc
%........crop_result target folder name
destination='C:\kalyan\crop_result\im';
imwrite(result,[destination,num2str(j),'.jpg']);
end
I hope you are not dealing with figure winndow. Just save the result images

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by