Rearrange matrix terms is a challenge

조회 수: 1 (최근 30일)
Roger Breton
Roger Breton 2022년 5월 3일
답변: Binaya 2023년 10월 11일
I am not finding exactly the right parameters to rearrange the terms of a matrix? It starts off as an RGB image I later convert to Lab, and want to store the Lab values to a text file in a particular rows x columns order, such that I end up with 49 rows by 33 columns. This is the code I have so far :
img = imread('Image 8-bit aRGBD65.tif');
imshow(img);
img_double = im2double(img);
img2Lab = rgb2lab(img_double, 'ColorSpace','adobe-rgb-1998', 'WhitePoint','d50');
LabIMG = reshape(permute(img2Lab,[3 1 2]),3,[]); % 3 x 1617
The LabIMG give mes a 3 rows x 1617 columns like this :
What I would like to have is for the data to be rearranged this way :
A1 = 42.29 66.60 0.69
B1 = some data (not shown)
C1 = some other data (not shown either)
... up to column 49 (AW1)
Then, the second row would follow with :
A2 = 47.57 58.15 1.54
B2 = ...
B3 = ...
.... Up to BW1
Third row :
C1 = 53.84 46.90 -0.31
C2 = ...
C3 = ...
... Up to CW1
Until I have 33 rows by 49 data points. I tried Transpose to no avail :
Transposed_CIELab = transpose(LabIMG);
I enclosed the resultant text file.... in case
I'm going in circles... Any help is appreciated.
  댓글 수: 5
Stephen23
Stephen23 2022년 5월 3일
imfinfo('Imge file.png')
ans = struct with fields:
Filename: '/users/mss.system.Rr7KOA/Imge file.png' FileModDate: '03-May-2022 20:20:05' FileSize: 3546 Format: 'png' FormatVersion: [] Width: 49 Height: 33 BitDepth: 24 ColorType: 'truecolor' FormatSignature: [137 80 78 71 13 10 26 10] Colormap: [] Histogram: [] InterlaceType: 'none' Transparency: 'none' SimpleTransparencyData: [] BackgroundColor: [] RenderingIntent: [] Chromaticities: [] Gamma: [] XResolution: [] YResolution: [] ResolutionUnit: [] XOffset: [] YOffset: [] OffsetUnit: [] SignificantBits: [] ImageModTime: '3 May 2022 20:20:05 +0000' Title: [] Author: [] Description: [] Copyright: [] CreationTime: [] Software: [] Disclaimer: [] Warning: [] Source: [] Comment: [] OtherText: []
Your image has 33x49x3 = 4851 elements. How do you expect to reshape 4851 elements into 33x49 = 1617 elements?
Roger Breton
Roger Breton 2022년 5월 3일
편집: Roger Breton 2022년 5월 3일
I was coming to a solution on my own, using a loop. It's not elegant :
LabIMG = reshape(permute(img2Lab,[3 1 2]),3,[]); % 3 x 1617
% Note: Les données sont en ordre de Colonnes dans LabIMG
Position = 1;
RowStep = 1;
for i = 1:1617
Index(i) = Position;
Position = Position + 49;
reste = rem(i,33);
if (reste == 0)
RowStep = RowStep + 1;
Position = RowStep;
end
end
IndexColumn = transpose(Index);
CIE_L = transpose(LabIMG(1,:,:)); % --> k'th slice along x direction
CIE_a = transpose(LabIMG(2,:,:)); % --> k'th slice along y direction
CIE_b = transpose(LabIMG(3,:,:)); % --> k'th slice along z direction
Final = [IndexColumn, CIE_L, CIE_a, CIE_b];
Let me digest your comment...
At this stage, I'm going to read on Matlab "sorting". We'll see where it gets me but glad I thought about the remainder function, so that I can test the position in the row and determine whether I need to break it or not on the base of remainder. Sigh! ...

댓글을 달려면 로그인하십시오.

답변 (1개)

Binaya
Binaya 2023년 10월 11일
Hi Roger,
As per my understanding, you would like to merge the 3 channels of image in LAB format, given in text file into a single object to be put in an cell in the table.
Please find the below code for creating the table of size [33,49]:
load('Transposed CIE Lab.txt');
cellArray = cell(33, 49);
% Store each column in the cell array
for i = 1:33
for j = 1:49
cellArray{i,j} = Transposed_CIE_Lab((i-1)*10+j, :);
end
end
table = cell2table(cellArray)
Following this you can export the created table into a text file.
I hope this solves your query.
Regards
Binaya

카테고리

Help CenterFile Exchange에서 Import, Export, and Conversion에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by