I am struggling to get this code to work.

1 回表示 (過去 30 日間)
Martin Olowe
Martin Olowe 2021 年 8 月 4 日
コメント済み: Image Analyst 2021 年 8 月 5 日
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.
  1 件のコメント
Image Analyst
Image Analyst 2021 年 8 月 5 日
Yes, or imtile() introduced in r2018b.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAgriculture についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by