MATLAB Answers

I am struggling to get this code to work.

2 views (last 30 days)
Martin Olowe
Martin Olowe on 4 Aug 2021
Commented: Image Analyst on 5 Aug 2021
Im unsure of which parameters to change to stitch the image to get the code to work. I have attached the 2 codes in the question as well as a sample set of images

Answers (2)

Image Analyst
Image Analyst on 4 Aug 2021
Try this:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
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 long g;
format compact;
%Division Option
DO=8;
%Defect Color Threshold
colorThreshold=60;
% Specify folder.
folder = fullfile(pwd, 'Image sample');
% Assume all files in folder are to be stitched together.
defaultValues = {'8', '1'};
[pics] = inputdlg({'How many pictures were taken in x direction?','How many pictures were taken in y direction?'},...
'Size of concacenated image', [1 51; 1 51], defaultValues);
x=double(string(pics{1,1}));
y=double(string(pics{2,1}));
% Get all filenames:
filePattern = fullfile(folder, '*.tif');
fileList = dir(filePattern);
fileNames = {fileList.name}
loopCounter = 1;
fullFileName = fullfile(folder, fileNames{loopCounter});
thisImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(thisImage);
% Preallocate output image.
stitchedImage = zeros(y * rows, x * columns);
% Do the stitching.
for row = 1 : y
thisRow = (row - 1) * rows + 1;
for col = 1 : x
thisColumn = (col - 1) * columns + 1;
% Read in this image.
fullFileName = fullfile(folder, fileNames{loopCounter});
thisImage = imread(fullFileName);
thisImage=double(thisImage);
% Paste onto canvass.
stitchedImage(thisRow : (thisRow + rows - 1), thisColumn : (thisColumn + columns -1), :) = thisImage;
% Increment file counter.
loopCounter = loopCounter + 1;
end
end
subplot(2, 1, 1);
imshow(stitchedImage, [])
impixelinfo
title('Original Stitched Image', 'FontSize', 17);
axis('image', 'on');
binaryImage = stitchedImage < colorThreshold;
subplot(2, 1, 2);
imshow(binaryImage)
axis('image', 'on');
g = gcf;
g.WindowState = 'maximized';
% Compute area fraction.
areaFraction = nnz(binaryImage) / numel(binaryImage)
caption = sprintf('Binary Image. The Area fraction for a threshold of %.1f is %.4f.', colorThreshold, areaFraction);
title(caption, 'FontSize', 17);
message = sprintf('The Area fraction for a threshold of %.1f is %.4f.', colorThreshold, areaFraction);
fprintf('%s\n', message);
fprintf('Done running %s.m.\n', mfilename);
uiwait(helpdlg(message));
  2 Comments
Image Analyst
Image Analyst on 5 Aug 2021
You'll need to change the number of rows and columns of course. And I haven't tested it for the case where you have multiple rows and you only have enough images to fill up a partial row. So that may need to be checked and made more robust. Just give it a try and adapt as needed.

Sign in to comment.


Walter Roberson
Walter Roberson on 5 Aug 2021
You might be interested in using montage(). The output of montage() is an image() object that you can get the CData for, and the CData will be the composite image.

Community Treasure Hunt

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

Start Hunting!