I retrieved all non-white pixel from an image :
[ii, jj] = find(BlackOnWhite < 255)
Then I tried to index those pixel coordinates to a matrix :
image(ii, jj) = 0
But zeros do not appear in the expected places. How can I put zeros only in the places specified by pairs from ii
and jj
(i.e. [ii(1), jj(1)], [ii(2), jj(2)]
etc.)?
A simple way to do that is to use linear indexing. This means using a single index that traverses all entries in the matrix (down, then across). In your case:
find
with one ouput. This gives the linear indices of the desired pixels.So:
ind = find(BlackOnWhite < 255);
image(ind) = 0;
You can even remove find
and use logical indexing. This means that the result of the logical comparison is directly used as an index:
ind = BlackOnWhite < 255;
image(ind) = 0;
The problem with the code shown in your question is that ii
and jj
are being used as "subscript indices". This selects all pairs formed by any value from ii
and any value from jj
, which is not what you want.
If you have subscripts ii
and jj
like in your question and you need to only select corresponding values from each subscript (instead of all pairs), you can use sub2ind
to convert to a linear index:
[ii, jj] = find(BlackOnWhite < 255);
image(sub2ind(size(image), ii, jj)) = 0;