Here is some code that might do the job. I am assuming that your image is truecolor, so the data have dimensionsM x N x 3 for someM andN, and the colors are 24-bit, so the data type inR isuint8. If your images are different, you'll have to adjust the code:
Best Answer