Search code examples
matlabmatriximage-thresholding

How to set pixel values of a matrix at specific indexes to pixel values of a different matrix with the same indexes?


What I'm trying to do is image thresholding with matrix operations, but rather than setting the threshold result equal to a fixed value, like 256 or something, I'm trying to set the result equal to the calculation of pixel values from two other images of the same size. So, for example:

firstImage = img1;
secondImage = img2;
thirdImage = img3;
secondImage(firstImage < 100) = thirdImage(at the same indexes as where the thresholding condition holds true) .* 10;

MATLAB normally attempts to multiply the entire thirdImage .* 10 and save that, but what I want is only those specific pixels that match to do the operation and overwrite the respective values in the secondImage.

How to do this?


Solution

  • You have kinda figured it out yourself in the question:

    secondImage(firstImage < 100) = thirdImage(firstImage < 100) * 10;
    

    i.e. just like you're indexing secondImage, index thirdimage in the same way.