从图像到矢量,反之亦然,旋转图像

时间:2017-02-28 14:06:31

标签: matlab image-processing dicom image-rotation

我读了DICOM图像,由于各种原因,我不得不将图像矩阵转换为行向量。 在对矢量位执行各种操作后,我需要在与原始尺寸相同的DICOM图像中重新处理矢量。

我已经完成了所有这些步骤但是当我去显示生成的图像时,它会被旋转。 这就是我得到的: enter image description here

这是代码的一部分:

function [I0, Iw] = watIns(filename, w)    
    I0 = dicomread(filename);
    figure(1);
    imshow(I0, []);
    title('(1) I0 originale');

    info = dicominfo(filename);
    width = info.Width;
    height = info.Height;
    size = info.FileSize;
    k = info.BitDepth;

    % I extract the first k pixels and memorize their LBS in a variable S.
    x = 1 : k;
    y = 1; 

    firstKPixel = I0(x, y);

    % convert in binary
    firstKPixel2 = 0*firstKPixel; 
    for i = 1 : length(firstKPixel)
        if firstKPixel(i) == 0
            firstKPixel2(i) = 0;
        else
            firstKPixel2(i) = dec2bin(firstKPixel(i), 'left-msb');
        end
    end


    % I take the LSB of each element in firstKPixel2 and concatenate in the string S
    S = '';
    for i = 1 : k
        c = firstKPixel2(i, :);
        s = num2str(c(end));
        S = strcat(S, s);
    end

    % I compute the vector corresponding to I0 but without the first 0 pixels and the corresponding histogram
    [vecComp, histComp] = histKtoEnd(I0, 0, k);

    % I compute the vector corresponding to I0 but without the first k pixels and the corresponding histogram
    [vecWithoutKPixel, histWithoutKPixel] = histKtoEnd(I0, k, k);  

    L = ...; % is a vector of values     

    % I save l_0 in the LSB of the first k pixels.

    % prendo l_0, ovvero il primo elemento di L
    l_0 = L(1);

    l_02 = fliplr(bitget(l_0, 1:k));

    % I take the LSB of each element in firstKPixel2 and I sret it to the i-th element of l0_2
    for i = 1 : k
        c = firstKPixel2(i, :);
        c(end) = l_02(i);
        firstKPixel2(i, :) = c;
    end

    % convert to decimal each element of firstKPixel2
    for i = 1 : length(firstKPixel2)
        str = int2str(firstKPixel2(i));
        firstKPixel2_lsb10(i) = bin2dec(str);
    end

    % I set first k pixels in the image to those just modified
    vecComp(1 : k) = firstKPixel2_lsb10(1 : k);

    % Transform the vector image
    mat = reshape(vecComp, [width, height]);
    dicomwrite(mat, './I1.dcm', 'CompressionMode', 'None');
    I1 = dicomread('./I1.dcm');
    figure(7);
    imshow(I1, []); % ROTATE IMAGE!!!

    % ...      
end

histKtoEnd功能是:

function [vecWithoutKPixel, hist] = histKtoEnd(image, k, colorDepth)

    imageVec = reshape(image.', [], 1);
    l = length(imageVec); 

    vecWithoutKPixel = imageVec((k+1) : l-1); 
    vecWithoutKPixel(end+1) = imageVec(l); 

    hist = zeros(1, 2^colorDepth); 

    for i = 0 : (2^colorDepth - 1) 
        grayI = (vecWithoutKPixel == i);
        hist(1, i+1) = sum(grayI(:));
    end

end

我读到here,Matlab显示的图像像一个矩阵,第一个坐标(行)从上到下,第二个(列)从左到右。

我无法解决,任何人都可以帮助我吗? 谢谢!

1 个答案:

答案 0 :(得分:4)

在某种程度上,在展开,处理和滚动数据时,它似乎会被转换。您可以尝试找出发生这种情况的原因,但如果您不在乎,您最终可以通过

转换结果。
mat = reshape(vecComp, [width, height]).';