matlab原始图片Iin分割为row*col份,打乱顺序重新组成一张新的图片Iout

%原始图片Iin分割为row*col份,打乱顺序重新组成一张新的图片Iout
Iin = imread('H:\Data_WangYu\tooopen_sy_139205349641.jpg');
Iout=Iin;
segment_length = 256;
[a,b,c] = size(Iin);
ROWS = floor(a/segment_length); %行数,纵向
COLS = floor(b/segment_length); %列数,横向

managed = 0;
index_index = 1:ROWS*COLS;
num = ROWS*COLS;

for i=1:ROWS
    for j=1:COLS
        myrand = floor( rand()*(num - managed) )+1;%index的index
        if(myrand==2)
            myrand=1;
        end
        index = index_index(myrand) %选择的
        index_index(myrand)=[];
        
        col1 = floor((index-0.1)/ROWS)+1;
        row1 = index - (col1-1)*ROWS ;
        

        tmp = Iin( (i-1)*segment_length+1 : i*segment_length, (j-1)*segment_length+1 : j*segment_length, : );
        Iout( (row1-1)*segment_length+1 : row1*segment_length, (col1-1)*segment_length+1 : col1*segment_length, : ) = tmp;
        managed = managed + 1;
    end
end

figure(1)
imagesc(Iin);
figure(2)
imagesc(Iout);



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为AI语言模型,我无法直接访问您的电脑上的文件夹。但是,我可以提供一些基于transformer的图像分类的MATLAB代码示例,您可以根据自己的需要进行修改和调整。 首先,您需要准备好数据集和标签。假设您的数据集存储在D:\path文件夹中,并且有两个类别:cat和dog。那么,您需要在代码中指定这些信息: ```matlab % 定义数据集路径和类别标签 dataFolder = "D:\path"; classNames = ["cat","dog"]; ``` 接下来,您需要使用ImageDatastore函数读取数据集,并进行图像增强处理: ```matlab % 设置图像增强处理 imageSize = [224 224]; augmenter = imageDataAugmenter('RandXReflection',true,'FillValues',0,'Resize',imageSize); % 读取数据集 data = imageDatastore(dataFolder,'IncludeSubfolders',true,'LabelSource','foldernames','ReadFcn',@(x)preprocessImage(x,imageSize),'Augmenter',augmenter); ``` 在上面的代码中,我们使用了preprocessImage函数对图像进行了预处理,这个函数的代码如下: ```matlab function Iout = preprocessImage(Iin,imageSize) % 等比例缩放并补零 Iout = imresize(Iin,[NaN imageSize(2)],'bilinear'); if size(Iout,1) < imageSize(1) Iout = padarray(Iout,[imageSize(1)-size(Iout,1) 0],'post','symmetric'); end % 转换为单精度浮点数 Iout = im2single(Iout); end ``` 接下来,我们需要构建一个Transformer模型。在MATLAB中,您可以使用transformerLayer函数创建Transformer层,然后使用layerGraph函数将其组合成一个完整的神经网络模型。 ```matlab % 构建Transformer模型 numHeads = 8; dModel = 512; dff = 2048; numLayers = 6; dropoutRate = 0.1; inputSize = [imageSize 3]; outputSize = numel(classNames); % 创建Transformer层 selfAttentionLayer = transformerLayer(dModel,numHeads,'SelfAttentionDropout',dropoutRate,'Normalization','none','Name','self-attention'); feedForwardLayer = [ fullyConnectedLayer(dff,'Name','fc1') reluLayer('Name','relu') dropoutLayer(dropoutRate,'Name','dropout') fullyConnectedLayer(dModel,'Name','fc2') dropoutLayer(dropoutRate,'Name','dropout') ]; encoderLayer = layerGroup([selfAttentionLayer feedForwardLayer],'Name','encoder'); % 组成完整的神经网络模型 inputLayer = imageInputLayer(inputSize,'Name','input'); outputLayer = [ layerNormalizationLayer('Name','norm') fullyConnectedLayer(outputSize,'Name','fc') softmaxLayer('Name','softmax') classificationLayer('Name','output') ]; lgraph = layerGraph(); lgraph = addLayers(lgraph,inputLayer); lgraph = addLayers(lgraph,encoderLayer); lgraph = addLayers(lgraph,outputLayer); lgraph = connectLayers(lgraph,'input','encoder/in'); lgraph = connectLayers(lgraph,'encoder/out','norm/in'); lgraph = connectLayers(lgraph,'norm','fc'); lgraph = connectLayers(lgraph,'fc','softmax'); lgraph = connectLayers(lgraph,'softmax','output'); ``` 最后,您需要使用trainNetwork函数对模型进行训练,并使用classify函数对的图像进行分类。 ```matlab % 训练神经网络模型 options = trainingOptions('adam',... 'InitialLearnRate',1e-4,... 'MiniBatchSize',16,... 'MaxEpochs',10,... 'Shuffle','every-epoch',... 'Verbose',true,... 'Plots','training-progress'); net = trainNetwork(data,lgraph,options); % 使用神经网络模型进行分类 newImage = imread("new_image.jpg"); newImage = preprocessImage(newImage,imageSize); label = classify(net,newImage); disp("Predicted label: "+label); ``` 以上是一个基于transformer的图像分类MATLAB代码示例。您可以根据自己的需要进行修改和调整。注意,在使用该代码时,请确保您已经安装了Deep Learning Toolbox和Computer Vision Toolbox。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值