RGB images are 3-D(i.e 3 , 2-D array for R,G,B ), can we extract these array seperatly?

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

You just select each "page" of the matrix.
im = imread('ngc6543a.jpg');
Rim = im(:,:,1);
Gim = im(:,:,2);
Bim = im(:,:,3);

4 Comments

When i am using imtool() function and checking the pixel information,pixel value is not matched with the pixel value which is obtained by redChannel = rgbImage(:, :, 1); greenChannel = rgbImage(:, :, 2); blueChannel = rgbImage(:, :, 3);
To solve that we will need you to attach a screenshot of imtool with your cursor showing one RGB and x,y set of values, and your original image and code
Something like
rgbImage = imread('peppers.png');
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
imtool(rgbImage);
x = 10;
y = 300;
hold on;
plot(x, y, 'r+', 'MarkerSize', 30);
fprintf('RGB(%d, %d) = (%d, %d, %d)\n',...
redChannel(y, x), greenChannel(y, x), blueChannel(y, x));
Are you sure you didn't confuse (x,y) with row,column)? Very very common mistake. Remember (x,y) is (column, row), NOT (row, column) like you need to program into your code.

Sign in to comment.

More Answers (2)

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

is this working for tif Image with a sensor alignment 'grbg' ?
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.
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
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?
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
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.
Thank you for the code. Very informative and usefl for me.

Sign in to comment.

When i am using imtool() function and checking the pixel information,pixel value is not matched with the pixel value which is obtained by redChannel = rgbImage(:, :, 1); greenChannel = rgbImage(:, :, 2); blueChannel = rgbImage(:, :, 3);

1 Comment

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);

Sign in to comment.

Categories

Asked:

on 29 Mar 2014

Commented:

on 16 Oct 2022

Community Treasure Hunt

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

Start Hunting!