You need to make sure that the image is saved in the "png" format. Then you can use the 'Alpha' parameter of the png file, which is a matrix that determines the transparency of each pixel separately. This is essentially a Boolean matrix, which is 1 if the pixel is transparent and 0 is not. This can be done easily with a for loop, as long as the color you want to be transparent always has the same value (i.e. 255 for uint8). If this is not always the same value, you can define a threshold or range of values ββwhere this pixel will be transparent.
Update:
First create an alpha matrix by repeating the image and (assuming that the white color becomes transparent) whenever the pixel is white, set the alpha matrix in this pixel to 1.
Then use this newly created alpha matrix (A) to save the image as ".png"
imwrite(X,'your_image.png','Alpha',A);
source share