【问题标题】:From image to vector and vice versa, rotated image从图像到矢量,反之亦然,旋转图像
【发布时间】:2017-02-28 14:06:31
【问题描述】:

我读取了一个 DICOM 图像,出于各种原因,我不得不将图像矩阵转换为行向量。 在对向量的位进行各种操作后,我需要在与原始大小相同的DICOM图像中重新处理向量。

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

这是代码的一部分:

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 显示的图像类似于矩阵,第一个坐标(行)从上到下,第二个(列)从左到右。

我无法解决,有人可以帮助我吗? 谢谢!

【问题讨论】:

  • 把代码改成mat = reshape(vecComp, [width, height]).';
  • @AnderBiguri 谢谢,完美运行。 .' 是什么意思?

标签: matlab image-processing dicom image-rotation


【解决方案1】:

在展开、处理和滚动数据的过程中,它似乎被转置了。您可以尝试找出发生这种情况的原因,但如果您不在乎,您始终可以将结果最后转置为

mat = reshape(vecComp, [width, height]).';

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    相关资源
    最近更新 更多