% 导入9张待复原的图片 img1 = imread('C:\Users\zhong\Desktop\题1\IMG_1192.JPEG'); img2 = imread('C:\Users\zhong\Desktop\题1\IMG_1193.JPEG'); img3 = imread('C:\Users\zhong\Desktop\题1\IMG_1194.JPEG'); img4 = imread('C:\Users\zhong\Desktop\题1\IMG_1195.JPEG'); img5 = imread('C:\Users\zhong\Desktop\题1\IMG_1196.JPEG'); img6 = imread('C:\Users\zhong\Desktop\题1\IMG_1197.JPEG'); img7 = imread('C:\Users\zhong\Desktop\题1\IMG_1198.JPEG'); img8 = imread('C:\Users\zhong\Desktop\题1\IMG_1199.JPEG'); img9 = imread('C:\Users\zhong\Desktop\题1\IMG_1200.JPEG'); % 将图片存储在一个单元数组中 smallImages = {img1, img2, img3, img4, img5, img6, img7, img8, img9}; % 初始化结果大图 resultSize = size(img1) * 3; % 假设结果大图为3x3的网格 resultImage = uint8(zeros(resultSize)); % 对每张图片进行边缘检测 edgeImages = cell(1, 9); for i = 1:9 grayImage = rgb2gray(smallImages{i}); edgeImage = edge(grayImage, 'Canny'); % 使用Canny算子进行边缘检测 edgeImages{i} = edgeImage; end % 计算边缘相似度矩阵 similarityMatrix = zeros(9, 9); for i = 1:9 for j = 1:9 similarityMatrix(i, j) = calculateSimilarity(edgeImages{i}, edgeImages{j}); end end % 构建最小生成树 G = graph(similarityMatrix); mst = minspantree(G); %创建一个大小为300x300的大图像 resultSize = [300 300]; resultImage = zeros(resultSize(1), resultSize(2), 3); % 每行显示3张小图像 for row = 1:3 for col = 1:3 % 计算小图像在大图像中的位置 startIndex = (col-1) * resultSize(2)/3 + 1; endIndex = col * resultSize(2)/3; % 将 smallImages{(row-1)*3+col} 图像复制到对应位置 resultImage((row-1)resultSize(1)/3+1:rowresultSize(1)/3, startIndex:endIndex, :) = smallImages{(row-1)*3+col}; end end % 显示结果图像 imshow(resultImage); % 计算边缘相似度的函数(这里仅示意,实际可根据需要进行修改) function similarity = calculateSimilarity(edgeImage1, edgeImage2) similarity = sum(edgeImage1(:) == edgeImage2(:)) / numel(edgeImage1); end为什么生成图片空白,如何解决
时间: 2024-04-22 15:26:56 浏览: 145
生成的图片空白是由于在复制小图像到大图像的过程中,存在一个索引计算错误的问题。在下面的代码中,你需要将 `resultImage((row-1)resultSize(1)/3+1:rowresultSize(1)/3, startIndex:endIndex, :)` 改为 `resultImage((row-1)*resultSize(1)/3+1:row*resultSize(1)/3, startIndex:endIndex, :)`。
修改后的代码如下所示:
```matlab
% 导入9张待复原的图片
img1 = imread('C:\Users\zhong\Desktop\题1\IMG_1192.JPEG');
img2 = imread('C:\Users\zhong\Desktop\题1\IMG_1193.JPEG');
img3 = imread('C:\Users\zhong\Desktop\题1\IMG_1194.JPEG');
img4 = imread('C:\Users\zhong\Desktop\题1\IMG_1195.JPEG');
img5 = imread('C:\Users\zhong\Desktop\题1\IMG_1196.JPEG');
img6 = imread('C:\Users\zhong\Desktop\题1\IMG_1197.JPEG');
img7 = imread('C:\Users\zhong\Desktop\题1\IMG_1198.JPEG');
img8 = imread('C:\Users\zhong\Desktop\题1\IMG_1199.JPEG');
img9 = imread('C:\Users\zhong\Desktop\题1\IMG_1200.JPEG');
% 将图片存储在一个单元数组中
smallImages = {img1, img2, img3, img4, img5, img6, img7, img8, img9};
% 初始化结果大图
resultSize = size(img1) * 3; % 假设结果大图为3x3的网格
resultImage = uint8(zeros(resultSize));
% 对每张图片进行边缘检测
edgeImages = cell(1, 9);
for i = 1:9
grayImage = rgb2gray(smallImages{i});
edgeImage = edge(grayImage, 'Canny'); % 使用Canny算子进行边缘检测
edgeImages{i} = edgeImage;
end
% 计算边缘相似度矩阵
similarityMatrix = zeros(9, 9);
for i = 1:9
for j = 1:9
similarityMatrix(i, j) = calculateSimilarity(edgeImages{i}, edgeImages{j});
end
end
% 构建最小生成树
G = graph(similarityMatrix);
mst = minspantree(G);
% 创建一个大小为300x300的大图像
resultSize = [300 300];
resultImage = zeros(resultSize(1), resultSize(2), 3);
% 每行显示3张小图像
for row = 1:3
for col = 1:3
% 计算小图像在大图像中的位置
startIndex = (col-1) * resultSize(2)/3 + 1;
endIndex = col * resultSize(2)/3;
% 将 smallImages{(row-1)*3+col} 图像复制到对应位置
resultImage((row-1)*resultSize(1)/3+1:row*resultSize(1)/3, startIndex:endIndex, :) = smallImages{(row-1)*3+col};
end
end
% 显示结果图像
imshow(resultImage);
% 计算边缘相似度的函数(这里仅示意,实际可根据需要进行修改)
function similarity = calculateSimilarity(edgeImage1, edgeImage2)
similarity = sum(edgeImage1(:) == edgeImage2(:)) / numel(edgeImage1);
end
```
通过修改代码中的索引计算错误,应该能够解决生成图片空白的问题。如果问题仍然存在,请提供更多信息,以便更好地帮助你解决问题。
阅读全文