Search code examples
matlabimage-processingdicomimage-rotation

From image to vector and vice versa, rotated image


I read a DICOM image and for a variety of reasons, I had to turn the image matrix in a row vector. After performing various operations on the bits of the vector, I need to reprocess the vector in a DICOM image of the same size as the original.

I've done all these steps but when I go to display the resulting image, this is rotated. This is what I get: enter image description here

That's part of the code:

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 is:

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

I read here that Matlab shows images like a matrix with the first coordinates (rows) going top-down and the second (columns) left-right.

I couldn't solve, can anyone help me? Thank you!


Solution

  • Somehow in the unrolling, processing and rolling of your data it seems to get transposed. You can try to find why that happens, but if you do not care, you can always just transpose the result in the end by

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