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