Wednesday, February 28, 2024

Matlab: Convert RGB to HSV/XYZ/LAB/Ycbcr/NTSC

File. 2nd.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
% Created by LI Xu
% Version 1.0
% February 22, 2024


% If you have any question about this code,
% please do not hesitate to contact me via E-mail: 
% jeremy456@163.com

% Blog:
% http://blog.sciencenet.cn/u/lixujeremy

clear;
clc;

timebegin=tic;
cur_data=date;
cur_time=fix(clock);
str1=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');

filename='wheat';

% Source Directory
SouDir='input';
% Destination Directory
DesDir='output';

files=dir(SouDir);
files=files(3:end);


for ii=1:numel(files)
    % construct the input file path
    filepath=fullfile(SouDir, files(ii).name);

    [image, geo]=readgeoraster(filepath);
    image=double(image);

    try
        info=geotiffinfo(filepath);
    catch
        info=georasterinfo(filepath);
    end

    Num_bands=size(image, 3);

    if Num_bands==3
        RGB=image;
    else
        RGB=zeros(size(image, 1), size(image, 2), 3);
        RGB(:, :, 1)=image(:, :, 4);
        RGB(:, :, 2)=image(:, :, 2);
        RGB(:, :, 3)=image(:, :, 1);
    end

    % Normalise the RGB bands [0, 255]
    RGB=GenNormalise(RGB);


    % https://www.mathworks.com/help/images/understanding-color-spaces-and-color-space-conversion.html
    xyz=rgb2xyz(RGB);
    lab=rgb2lab(RGB);
    ycbcr=rgb2ycbcr(RGB);
    ntsc=rgb2ntsc(RGB);



    HSV=rgb2hsv(RGB);
    % ExG = 2 * G - R - B
    ExG=2*RGB(:, :, 2)-RGB(:, :, 1)-RGB(:, :, 3);



    otpath=strsplit(files(ii).name, '_');
    otpath=otpath(2:end);
    otpath=strjoin(otpath, '_');
    otpath_rgb=[filename, '_rgb_', otpath];
    otpath_hsv=[filename, '_hsv_', otpath];

    otpath_xyz=[filename, '_xyz_', otpath];
    otpath_lab=[filename, '_lab_', otpath];
    otpath_ycbcr=[filename, '_ycbcr_', otpath];
    otpath_ntsc=[filename, '_ntsc_', otpath];

    otpath_exg=[filename, '_exG_', otpath];


    otpath_rgb=fullfile(DesDir, otpath_rgb);
    otpath_hsv=fullfile(DesDir, otpath_hsv);
    otpath_xyz=fullfile(DesDir, otpath_xyz);
    otpath_lab=fullfile(DesDir, otpath_lab);
    otpath_ycbcr=fullfile(DesDir, otpath_ycbcr);
    otpath_ntsc=fullfile(DesDir, otpath_ntsc);
    otpath_exg=fullfile(DesDir, otpath_exg);


    try
        geotiffwrite(otpath_rgb, RGB, geo);
        geotiffwrite(otpath_hsv, HSV, geo);
        geotiffwrite(otpath_xyz, xyz, geo);
        geotiffwrite(otpath_lab, lab, geo);
        geotiffwrite(otpath_ycbcr, ycbcr, geo);
        geotiffwrite(otpath_ntsc, ntsc, geo);
        geotiffwrite(otpath_exg, ExG, geo);
    catch
        strcmd=['gdalinfo ', filepath];
        [~, cmdout]=system(strcmd);
        epsg=extractBetween(cmdout, "EPSG:"," got from GeoTIFF keys");
        epsg=epsg{1};
        % geotiffwrite(otpath, uint8(class_imag), geo, 'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag);
        geotiffwrite(otpath_rgb, RGB, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_hsv, HSV, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_xyz, xyz, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_lab, lab, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_ycbcr, ycbcr, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_ntsc, ntsc, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_exg, ExG, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
    end

    disp(['[', num2str(ii), '/', num2str(numel(files)), ']~', files(ii).name]);


end




fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');

cur_data=date;
cur_time=fix(clock);
str2=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time End: ');
disp(str2);
timespan=toc(timebegin);
fprintf('Time Span: %.4f s\n', timespan);


disp('***********************************************');

function output=GenNormalise(input)

    output=input*0;

    for ii=1:size(input, 3)
        image=input(:, :, ii);
        % red_normalized = (red - red.min()) / (red.max() - red.min()) * 255
        aa=image(:);
        image_normalized=(image-min(aa))./(max(aa)-min(aa))*255.0;
        output(:, :, ii)=image_normalized;



    end


end

No comments:

Post a Comment