本文介绍了平均matlab中循环中矩阵的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用我认为是矩阵的图像。

I work with an image that I consider as a matrix.

我想将800 x 800矩阵(A)转换为400 x 400矩阵(B )其中A矩阵的4个单元格的平均值= B矩阵的1个单元格(我知道这不是正确的代码行):

I want to turn a 800 x 800 matrix (A) into a 400 x 400 matrix (B) where the mean of 4 cells of the A matrix = 1 cell of the B matrix (I know this not a right code line) :

B[1,1] =mean2(A[1,1 + 1,2 + 2,1 + 2,2])

依此类推整个矩阵......

and so on for the whole matrix ...

B [1,2]=mean2(A[1,3 + 1,4 + 2,3 + 2,4 ])

我想:

1)将A矩阵重塑为2 x 320 000矩阵,这样我得到的四个单元格需要彼此相邻,它是之后更容易处理行号。

1) Reshape the A matrix into a 2 x 320 000 matrix so I get the four cells I need to average next to each other and it is easier to deal with the row number afterwards.

Im4bis=reshape(permute(reshape(Im4,size(Im4,2),2,[]),[2,3,1]),2,[]);

2)使用我需要平均(子集化)的4个单元格创建一个单元格数组并计算它的意思。这就是它不起作用的地方

2) Create a cell-array with the 4 cells I need to average (subsetted) and calculate the mean of it. That's where it doesn't work

I{1,160000}=ones,
for k=drange(1:2:319999)
    for n=1:160000
        I{n}=mean2(Im4bis(1:2,k:k+1));
    end
end

我创建了一个400 x 400单元格的空矩阵(实际上是一个1 x 160000的矢量,我想用平均值填充它,但我得到一个1 x 319 999个单元格的矩阵,其中一个单元格为2个空格。

I created an empty matrix of 400 x 400 cells (actually a vector of 1 x 160000) and I wanted to fill it with the mean but I get a matrix of 1 x 319 999 cells with one cell out of 2 empty.

寻找光明

获取循环

out(size(A,1)/2,size(A,2)/2) = 0;
k = 1;
for i = 1:2:size(A,1)
    l = 1;
    for j = 1:2:size(A,2)
        out(k,l) = mean(mean(A(i:i+1,j:j+1)));
        l = l+1;
    end
    k = k+1;
end

测试输入图像:

A = rgb2gray(imread('inputImage.png'));

%// Here, You could use any of the method from any answers
%// or you could use the best method from the bench-marking tests done by Divakar
out = reshape(mean(im2col(A,[2 2],'distinct')),size(A)./2);

imshow(uint8(out));

imwrite(uint8(out),'outputImage.bmp');

输出图片:

最终通过阅读已写入的图像进行检查

Final check by reading the already written image

B = imread('outputImage.bmp');

>> whos B

Name        Size              Bytes  Class    Attributes

B         400x400            160000  uint8

这篇关于平均matlab中循环中矩阵的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:13