3D array of an image to 2D array of the same image

23 views (last 30 days)
Hi,
How can I transform a 3D array (of an image) to 1 2D array of the same image without loosing the image, of course, but resulting grayscale image is acceptable. For example,
jpegimage = imread('image001','jpeg');
>> size(jpegimage)
ans =
288 384 3
'jpegimage' is a 3D array. I want this array to convert into a 2D array. I did some experiments with MS paint and saw that converting a JPEG image to a GIF does exactly the same.i.e.:
>> imread('image','gif');
>> size(ans)
ans =
288 384
>>
And the resulting image looks exactly similar (grayscale due to imshow function). But I cannot do this with MS paint because I have huge collection of images. Therefore I try following:
>> imwrite(jpegimage,'Image002','gif');
Warning: Image data contains values that are out of range. Out of range values will be given the nearest valid value.
> In imagesci\private\writegif at 339 In imwrite at 406
But what i get as a result is a 3*1 GIF image.
Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 20 Dec 2011
img2d = rgb2gray(jpegimage);
  2 Comments
Ashish Bhatt
Ashish Bhatt on 20 Dec 2011
Thanks Walter! Is there a way to retain the color? just curious.
Walter Roberson
Walter Roberson on 20 Dec 2011
rgb2ind() can retain the color while producing a 2D array, by using an auxiliary array (a "color map") to store some color information. However, the numbers (indices) for any one pixel of the 2D array will not necessarily be correlated: color index 6 might be black, color index 7 might be red, color index 8 might be green, color index 9 might be hot pink, color index 10 might be a slightly different shade of green... just no inherent ordering to or meaning for the values. As such, there are not many meaningful ways to process the 2D color index array, not even for something as conceptually simple as calculating the mean of the image.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!