How to imread() an RGB image to a [3 rows by n columns] double form?
14 次查看(过去 30 天)
显示 更早的评论
I confess I'm struggling with array manipulations -- call me a newbie in Matlab.
Here's the gist of my project, nothing fancy, to read an RGB image as data points to plot in 3D using scatter3.
I created a simple 4 x 3 pixel RGB image in Photoshop, just to experiment.
(I had to resize the original image for Matlab Community)
So, I start with :
img=imread('4x3_RGB.tif');
Then I rescaled the 0 to 255 RGB values from 0 to 1.0 with :
img_double=double(img)./255;
Then I convert RGB to Lab using with :
Lab=rgb2lab(img_double);
At this point, I get this 4 x 3 x 3 double array :
Lab(:,:,1) =
53.5850 53.2408 97.1393
87.7347 91.1132 77.9855
96.5375 60.3242 32.2970
89.2650 68.0698 14.7631
Lab(:,:,2) =
0 80.0925 -21.5537
-86.1827 -48.0875 16.7037
0 98.2343 79.1875
-12.5051 -66.1018 48.1573
Lab(:,:,3) =
0 67.2032 94.4780
83.1793 -14.1312 16.5673
0 -60.8249 -107.8602
43.9621 51.3645 -61.6661
Trouble is, this "format" will not work with scatter3. I need to massage the data to get it in this form :
I tried Reshape, Permute and a few others to no avail. I just can't conceptualize this transform. I was about to try my hand at some kind of loop, to "convert" the data into a new [4 rows by 12 columns] 'matrix' but, before I embarked in this adventure, I decided to use Excel to enter the data in an empty sheet, then copy and paste into a new Matlab variable. I know, shame on me... But at least, I was able to get a "test of concept".
I guess I could continue doing this (tedious) approach but I'm curious what am I missing? How can I massage my Lab data into a series of column vectors to feed into scatter3?
0 个评论
采纳的回答
Voss
2021-12-16
You mentioned you made attempts to use reshape and permute, which is the way I would do it. Does this:
Lab_reshape = reshape(permute(Lab,[3 1 2]),3,[]);
seem to produce the expected result?
3 个评论
Voss
2021-12-16
编辑:Voss
2021-12-16
No problem!
reshape will go along the first dimension first (i.e., down the first column, then down the second column, etc.), so you've got to permute first to get the first dimension to be the RGB dimension, which is how you want the result to be. Then reshape to "collapse" the other two dimensions into one. It's worth pointing out that the following command:
Lab_reshape = reshape(permute(Lab,[3 2 1]),3,[]);
(note: [3 2 1] vs [3 1 2]) will also give a matrix of the right size with the RGB values in rows, as intended, but the pixels will be in a different order (corresponding to going across the first row of the image first, then 2nd row, etc., rather than down the 1st column, 2nd column, etc. as the first command does). It won't matter for scatter3, but for other purposes it might, so I thought I should point it out so you can make sure you're using the right command for the purpose you need.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!