Image splitting and saving the images separately.

21 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 31 Dec 2019
Edited: Image Analyst on 31 Dec 2019
Next, when you look at what you're writing, it's this:
image16bit = uint16(24/256);
imwrite(image16bit, fullfile(Folder,baseFileName));
so you can see that you're defining image16bit to be a single value, so no wonder it saves as a single pixel. Why did you do that?
If you need anymore help, attach at least one of the "temp%05i.jpg" images.
  3 Comments
Image Analyst
Image Analyst on 1 Jan 2020
You need to split the image into two images and save each one (I guess that's what you want to do):
[rows, columns, numberOfColorChannels] = size(rgbImage); % rgbImage is your original image.
% Extract the left image.
leftImage = rgbImage(:, 1:columns/2, :);
% Save the left image.
baseFileName = sprintf("split_image_test_left%d.png", a);
fullFileName = fullfile(Folder, baseFileName)
imwrite(leftImage, fullFileName);
% Extract the right image.
rightImage = rgbImage(:, columns/2+1:end, :);
% Save the right image.
baseFileName = sprintf("split_image_test_right%d.png", a);
fullFileName = fullfile(Folder, baseFileName)
imwrite(rightImage, fullFileName);
Austin Goodall
Austin Goodall on 1 Jan 2020
Thank you, that worked perfectly. I've been stuck on that section of the project for a while now, very much appreciated.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!