Image splitting and saving the images separately.
Afficher commentaires plus anciens
Hi all. Can anyone help me with some tweaking in my code for image splitting and saving the images seperately please? I have the code splitting the image into 2 images but it's displaying 4 images. 2 of them are the images i need and the other 2 are blank. I want to stop the 2 blank images from showing and when saving the images they seem to saving as extremely small images or as just a single pixel.
The code :
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
for i = [1:10]
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\Examples\R2019b\vision\MeasuringPlanarObjectsExample\stereo_calibration_test');
baseFileName = sprintf("temp%05i.jpg", i);%change this to the name of the photo you want to split.
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Read the image from disk.
rgbImage = imread(fullFileName);
% Test code if you want to try it with a gray scale image.
% Uncomment line below if you want to see how it works with a gray scale image.
%rgbImage = rgb2gray(rgbImage);
% Display image full screen.
imshow(rgbImage);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
%==========================================================================
% The first way to divide an image up into blocks is by using mat2cell().
blockSizeR = 1080; % Rows in block.
blockSizeC = 1920; % Columns in block.
% Figure out the size of each block in rows.
% Most will be blockSizeR but there may be a remainder amount of less than that.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
% Create the cell array, ca.
% Each cell (except for the remainder cells at the end of the image)
% in the array contains a blockSizeR by blockSizeC by 3 color array.
% This line is where the image is actually divided up into blocks.
if numberOfColorBands > 1
% It's a color image.
ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
else
ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 2: numPlotsC
% fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
%subplot(numPlotsR, numPlotsC, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
%imsave(rbgBlock)
% Make the caption the block number.
%caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
% plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
% title(caption);
%drawnow;
% Increment the subplot to the next location.
plotIndex = plotIndex + i;
for a = [1:20]
Folder = 'C:\test';
baseFileName = sprintf("split_image_test%d.png",a);
image16bit = uint16(24/256);
imwrite(image16bit, fullfile(Folder,baseFileName));
pause(2);
end
end
end
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
