Image splitting and saving the images separately.

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

 Akzeptierte Antwort

Image Analyst
Image Analyst am 31 Dez. 2019
Bearbeitet: Image Analyst am 31 Dez. 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 Kommentare

Austin Goodall
Austin Goodall am 1 Jan. 2020
Bearbeitet: Image Analyst am 1 Jan. 2020
Hi Image Analyst,
I'm a student that is currently on an internship with an engineering company. I've not used MATLAB much before -- just some small stuff in university. This is my first time getting to use MATLAB as a tool, so what I know is very basic. I have tried changing the following code to -
baseFileName = sprintf("split_image_test%d.png",a);
imwrite(uint16(24/256), fullfile(Folder,baseFileName));
and I'm getting the same results, i'm a bit lost on how to fix this code, it would be a great learning experience to get this working. also i have attached an "temp%05i.jpg" image below -
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);
Thank you, that worked perfectly. I've been stuck on that section of the project for a while now, very much appreciated.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by