RGB images are 3-D(i.e 3 , 2-D array for R,G,B ), can we extract these array seperatly?
Show older comments
I want to convert the 3-D image array into 1-D array, so if i got R,G & B array seperatly then these can be converted into 2-D array easily.. so how to get these 3 (R,G,B) array??
Accepted Answer
More Answers (2)
Image Analyst
on 29 Mar 2014
Extract each color channel
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
as shown in my attached demo that takes RGB histograms of every image in your folder.
Screenshot:

7 Comments
Yassine Zaafouri
on 12 Oct 2016
is this working for tif Image with a sensor alignment 'grbg' ?
Image Analyst
on 12 Oct 2016
Yes, if you're looking at the demosaiced (normal) image. If you're looking at the "raw" (non-demosaiced) image, then you'll have 4 color channels and you can just take the histogram of 4 instead of 3 color channels. I'm not sure where the demosaicing takes place in your system - while it's saving out to a tiff, or if it's expecting you to do that yourself later. If it demosaiced it before saving, like most cameras, then when imread reads it back in, it should be a 3 color channel RGB image, not a 4 channel image.
Yassine Zaafouri
on 12 Oct 2016
Edited: Yassine Zaafouri
on 12 Oct 2016
So i have a raw Image from a camera (1280*980 unit16 grbg) , then i extracted from it just a smaller Image ( 644*640 ) because i do not need the rest . Then i Worte with this new Matrix a new image and i saved it with the format tif because i think it is easier to work with when you compare it to .raw . Then when i call your function with this image it doesn't work that's why i called the function demosaic and then wrote a new image . After that i called your function with this image but the results are strange . There is my code and i hope you understand the Problem .
if true
new = im_raw(y1:y4, x1:x4 );
imwrite(uint16(new),'xyz.tif','compression','none')
test=double(imread('.\xyz.tif'))
figure; imshow(test,[160 4000])
title ('tif image ')
test1=imread('.\xyz.tif')
J = demosaic(test1,'grbg');
imwrite(uint16(J),'abc.tif')
end
Image Analyst
on 12 Oct 2016
Why is im_raw not 3D? It should be 1280 rows by 980 columns by 4 color channels, shouldn't it? How did you get im_raw read in, and what is its layout? Why is it only 2D?
Yassine Zaafouri
on 13 Oct 2016
it is an image sensor for automotive aand i took a monochrome image . But i think when i do that, the rgb informations are stored with the raw image, shouldn't it? so when i want to extract the rgb layers it should work.
if true
Sensor info:
// Width = 1280
// Height = 980
// Image Format = Bayer 12
// Subformat = GRBG
// Sensor output = 12 bits per pixel
//
// .RAW file info:
// stored as = 16 bits unsigned short
end
and that's how i read the image
if true
fid1 = fopen('capture0003.raw');
helpinterx = fread(fid1,'uint16');
helpinter=reshape(helpinterx,1280,964);
helpinter = helpinter';
im_raw=helpinter;
end
Image Analyst
on 13 Oct 2016
That doesn't look right. If the image is 980 by 1280 by 4 color channels, and you've read in all the data, then your reshape would not work since your new desired size does not contain all the pixels. I suggest you start your own discussion thread (rather than keep bugging priyanka), and read this and attach your actual image.
Jeykishan
on 16 Oct 2022
Thank you for the code. Very informative and usefl for me.
priyanka
on 29 Mar 2014
0 votes
1 Comment
Image Analyst
on 27 Mar 2021
Attach your image. There's a good possibility you're cheking the value using the x,y from the cursor in imtool as redChannel(x,y), which would be incorrect. Remember y is row, not x, so it needs to be redChannel(y, x), NOT redChannel(x, y);
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!