2014-10-18 4 views
0

나는 그레이 이미지에서 무작위로 픽셀 세트를 선택하고 다른 위치의 다른 픽셀에서 한 위치의 픽셀 강도를 빼서 각 픽셀의 강도를 비교하는 코드 작업을하고 있습니다. 코드에서 무작위로 선택을 수행하지만이 코드를 잘 모르며 픽셀 빼기를 수행하는 방법을 모릅니다. 미리 감사드립니다.픽셀 뺄셈

{ 
N = 100; % number of random pixels 
im = imread('image.bmp'); 
[nRow,nCol,c] = size(im); 
randRow = randi(nRow,[N,1]); 
randCol = randi(nCol,[N,1]); 

subplot(2,1,1) 
imagesc(im(randRow,randCol,:)) 
subplot(2,1,2) 
imagesc(im) 
} 
+1

조회 ', 나는이 코드와 그 작업을 시도했다,하지만 난 찾고 있어요 결과가 난 후 무작위로 균일하게 픽셀을 선택하는 작은 패치로 나누어 져있다 different.the 이미지 –

답변

0

Parag는 기본적으로 답을 주셨습니다. 이를 벡터화하려면 sub2ind을 사용해야합니다. 그러나, 나는 행과 열의 두 세트를 생성합니다. 첫 번째 픽셀 세트에 대해 하나의 세트가 필요하고 두 세트의 강도를 뺄 수 있도록 다음 픽셀 세트에 다른 세트가 필요하기 때문입니다. 따라서, 같은 것을 할 :

N = 100; % number of random pixels 
im = imread('image.bmp'); 
[nRow,nCol,c] = size(im); 

%// Generate two sets of locations 
randRow1 = randi(nRow,[N,1]); 
randCol1 = randi(nCol,[N,1]); 
randRow2 = randi(nRow,[N,1]); 
randCol2 = randi(nCol,[N,1]); 

%// Convert each 2D location into a single linear index 
%// for vectorization, then subtract 
locs1 = sub2ind([nRow, nCol], randRow1, randCol1); 
locs2 = sub2ind([nRow, nCol], randRow2, randCol2); 

im_subtract = im(locs1) - im(locs2); 
subplot(2,1,1) 
imagesc(im_subtract); 
subplot(2,1,2) 
imagesc(im); 

그러나, 위의 코드는 이미지가 그레이 스케일 있다고 가정합니다. 색상을 지정하려면이 작업을 조금 더 수행해야합니다. 각 채널에 액세스하고 채널별로 빼야합니다. 위에 정의 된 선형 색인은 단일 채널에 대한 것입니다. 따라서 다음 채널에서 동일한 해당 위치에 액세스하려면 각 채널에 대해 nRow*nCol까지 오프셋해야합니다. 따라서, bsxfun과 함께 sub2ind을 사용하여 빼기를 벡터화하기위한 올바른 값을 올바르게 생성합니다. 위의 코드를 약간 수정하면됩니다. 따라서 :

N = 100; % number of random pixels 
im = imread('image.bmp'); 
[nRow,nCol,c] = size(im); 

%// Generate two sets of locations 
randRow1 = randi(nRow,[N,1]); 
randCol1 = randi(nCol,[N,1]); 
randRow2 = randi(nRow,[N,1]); 
randCol2 = randi(nCol,[N,1]); 

%// Convert each 2D location into a single linear index 
%// for vectorization 
locs1 = sub2ind([nRow, nCol], randRow1, randCol1); 
locs2 = sub2ind([nRow, nCol], randRow2, randCol2); 

%// Extend to as many channels as we have 
skip_ind = permute(0:nRow*nCol:(c-1)*nRow*nCol, [1 3 2]); 

%// Create 3D linear indices 
locs1 = bsxfun(@plus, locs1, skip_ind); 
locs2 = bsxfun(@plus, locs2, skip_ind); 

%// Now subtract the locations 
im_subtract = im(locs1) - im(locs2); 
subplot(2,1,1) 
imagesc(im_subtract); 
subplot(2,1,2) 
imagesc(im); 
+0

sub2ind' 즉, 선택 방법이 항상 동일해야합니다. 어떤 생각? – user3485941