I am struggling to get this code to work.

1 次查看(过去 30 天)
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

回答(2 个)

Image Analyst
Image Analyst 2021-8-4
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 个评论
Martin Olowe
Martin Olowe 2021-8-4
willi need to change any variables? I have images consisting of 153 tiles to be stitched
Image Analyst
Image Analyst 2021-8-5
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2021-8-5
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.

类别

Help CenterFile Exchange 中查找有关 Agriculture 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by