1

검은 색/흰색 이미지 I0 (512 x 512)이 있는데이 이미지에서 첫 번째 k 픽셀을 제거하고 결과 이미지의 히스토그램을 계산해야합니다.히스토그램이 너무 달라야합니다.

내가 설명하자면, 픽셀을 고려하지 않고 I0 이미지의 막대 그래프를 만들어야합니다.

k = 8; 

% compute the histogram of the entire image I0 
[vecComp, histComp] = histKtoEnd(I0, 0, k); 

% compute the histogram of the image I0 without the first k pixel  
[vecWithoutKPixel, histWithoutKPixel] = histKtoEnd(I0, k, k); 

:

내 코드입니다

subplot(1, 2, 1); 
bar(0:2^k-1, histComp, 'r'); 
title('Histogram of the entire image'); 
axis([minColor, maxColor, 0, numberOfPixels]); 
subplot(1, 2, 2); 
bar(0:2^k-1, histWithoutKPixel, 'r'); 
title('Histogram of the image without the first k pixels'); 
axis([minColor, maxColor, 0, numberOfPixels]); 

내가 얻을 :

function [vecWithoutKPixel, hist] = histKtoEnd(image, k, colorDepth) 
    % image to row vector 
    imageVec = reshape(image.', [], 1); 
    l = length(imageVec); 

    % I "delete" the first k pixel 
    vecWithoutKPixel = imageVec((k+1) : l-1); 
    vecWithoutKPixel(end+1) = imageVec(l); 

    % inizialization 
    hist = zeros(1, 2^colorDepth); 

    % create the vector of occurrences 
    for i = 0 : (2^colorDepth - 1) 
     grayI = (vecWithoutKPixel == i); 
     hist(1, i+1) = sum(grayI(:)); 
    end 
end 

표시하려면 두 히스토그램 할 enter image description here

알 수 있듯이 히스토그램은 매우 다르지만 그 차이는 단지 8 픽셀이기 때문에 거의 차이가 없어야합니다.

어디서 잘못 되었나요?

또한 warkspace에서 새로 만든 변수는 이러한 치수가 :

vecComp -> 262144 x 1 uint8 
histComp -> 1 x 256 double 
vecWithoutKPixel -> 65528 x 1 uint8 
histWithoutKPixel -> 1 x 256 double 

이 매우 이상하다. 나는 가지고 있어야한다 :

vecComp -> 262144 x 1 
vecWithoutKPixel -> 262136 x 1 

누군가 나를 도울 수 있었느냐? 내가 DICOM 이미지와 함께 일하고 있어요 및 명령 info = dicominfo(filename)

size = info.FileSize; % 262582; 
colorType = info.ColorType; % grayscale 

가 그래서 문제가 이미지라고 생각하지 않습니다 얻을 당신에게


감사드립니다. I 라인 vecWithoutKPixel(end+1) = imageVec(l);에 brakpoint을 넣으면

나는 imageVec가 262144 X 1 UINT8 것을 얻고, :

function [vecWithoutKPixel, hist] = histKtoEnd(image, k, colorDepth) 
    % image to row vector 
    imageVec = reshape(image.', [], 1); 
    l = length(imageVec); 

    size(imageVec) % 262144 x 1 

    % I "delete" the first k pixel 
    vecWithoutKPixel = imageVec((k+1) : l-1); 
    vecWithoutKPixel(end+1) = imageVec(l); 

    % inizialization 
    hist = zeros(1, 2^colorDepth); 

    % create the vector of occurrences 
    for i = 0 : (2^colorDepth - 1) 
     grayI = (vecWithoutKPixel == i); 
     hist(1, i+1) = sum(grayI(:)); 
    end 
end 

것은 내가 명령 vecWithoutKPixel = imageVec((k+1) : l);vecWithoutKPixel = imageVec((k+1) : l-1); vecWithoutKPixel(end+1) = imageVec(l); 내가 vecWithoutKPixel = [] 것을 얻을 변경하는 경우.

+0

'vecWithoutKPixel = imageVec ((k + 1) : l-1);'줄에 중단 점을 넣고'size (imageVec)'를 사용하여'imageVec'의 크기를 검사 할 수 있습니까? 또한 1)'(k + 1) : (l-1)'이 다를 수도 있고 2) 왜 마지막 요소를 "지우고"다시 넣었습니까? – Yvon

+0

그리고'imageVec = image (:);'시도하십시오. – Yvon

+0

글쎄 진짜 문제는 이미지 데이터에 있습니다. 나는 B/W 그림을 다루고 있다고 믿지만 Matlab에서는 RGBA 이미지로 저장됩니다. 이것은 원본 이미지의 크기로 볼 수 있습니다. 먼저 B/W로 변환해야합니다. – Yvon

답변

1

이 같은 음모를 생성합니다 코드를 실행 한 후 I0=randi(255,100,100)를 사용하여 '임의의 이미지를'만들기 및 : Histogram of randomly created image 이 플롯은 정확해야합니다. 변수의 크기도 정확합니다.

vecComp 10000x1 double 
vecWithoutKPixel 9992x1 double 

따라서 우리의 문제는 귀하의 코드가 아니라 귀하의 이미지입니다. 당신의 이미지는 이전에 다른 사람들이 언급 한 회색 레벨 값이 아닙니다. 그러나 @Richard와 같은 일부 변수를 uint8으로 캐스팅하는 대신 이미지를 두 번 캐스팅해야합니다. 이미지의 rgb 값이 I0=double(rgb2gray(I0));이거나 다른 이유로 인해 uint8 인 경우 I0=double(I0)을 사용하고 함수에 전달합니다. 희망이 도움이됩니다.

0

이미지 데이터가 원래 uint8 형식이며 어느 시점에서 double으로 변환되는 것이 문제라고 생각합니다. (당신이 정수로 데이터를 비교하기 때문에) 따라서, histKtoEnd에서

grayI = (vecWithoutKPixel == i); 

라인은 아마도에만 uint8 데이터와 함께 작동합니다.

시도 histKtoEnd 함수에 라인

vecWithoutKPixel = uint8(vecWithoutKPixel); 

를 추가 :

function [vecWithoutKPixel, hist] = histKtoEnd(image, k, colorDepth) 
    % image to row vector 
    imageVec = reshape(image.', [], 1); 
    l = length(imageVec); 

    % I "delete" the first k pixel 
    vecWithoutKPixel = imageVec((k+1) : l-1); 
    vecWithoutKPixel(end+1) = imageVec(l); 

    % inizialization 
    hist = zeros(1, 2^colorDepth); 

    % create the vector of occurrences 
    for i = 0 : (2^colorDepth - 1) 
     vecWithoutKPixel = uint8(vecWithoutKPixel); % Cast to integer 
     grayI = (vecWithoutKPixel == i); 
     hist(1, i+1) = sum(grayI(:)); 
    end 
end 
+0

답장을 보내 주셔서 감사합니다. 내가 제안한 변경 사항을 적용하려고했지만 항상 같은 문제가 있습니다 ... – beth

관련 문제