gpt4 book ai didi

java - 在 Java 中实现 MATLAB 代码 - 光照不变式

转载 作者:行者123 更新时间:2023-11-30 05:37:09 25 4
gpt4 key购买 nike

我用 MATLAB 编写了以下代码,取自 this paper on page 3:

function [ ii_image ] = RGB2IlluminationInvariant( image, alpha )
ii_image = 0.5 + log(image(:,:,2)) - alpha*log(image(:,:,3)) - (1-alpha)*log(image(:,:,1));

此代码应将 3 channel RGB 图像转换为其照明不变量。我想知道代码在做什么,以便我可以用 Java 实现它。

据我所知,它正在计算每个红/绿/蓝像素的对数,并相互减去这些值,但结果不是整数,所以我无法将其应用于Java的BufferedImage更改 RGB 值时的类。如何在 Java 中模拟此功能?

最佳答案

将函数转换为JAVA并没有那么困难。
以下代码示例是 MATLAB 实现,可以直接转换为 JAVA。

您应该注意输入和输出元素的范围和类型以及内存顺序。

阅读以下代码中的注释:

%function [ ii_image ] = RGB2IlluminationInvariant( image, alpha )

%Initialize input (for executing the example):
image = imread('peppers.png');
image = max(image, 1); %Replace zero values with 1, because log(0) is -Inf
image = double(image)/255; %Convert image to double in range [1/255, 1]. In JAVA, you should use double(pix)/255 if pix is a byte in range [0, 255].
alpha = 0.9;

ii_image = 0.5 + log(image(:,:,2)) - alpha*log(image(:,:,3)) - (1-alpha)*log(image(:,:,1));

%Assume elements in JAVA are stored in three 2D arrays (3 planes): R, G, B
%For example: double[][] R = new double[384][512];
%In Matlab 3 planes are:
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);
%II_image = 0.5 + log(G) - alpha*log(B) - (1-alpha)*log(R);
II_image = zeros(size(R));

%Equivalent for loop (simple to implement in JAVA):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
image_width = size(image, 2);
image_height = size(image, 1);
for y = 1:image_height %Iterate rows, In JAVA: for(y = 0; y < image_height; y++)
for x = 1:image_width %Iterate columns, In JAVA: for(x = 0; x < image_width; x++)
r = R(y, x); %In JAVA: double r = R[y][x];
g = G(y, x);
b = B(y, x);

p = 0.5 + log(g) - alpha*log(b) - (1.0-alpha)*log(r);

II_image(y, x) = p;
end
end

%Display ii_image
figure;imshow(ii_image, []);impixelinfo;title('ii\_image');

%Show difference (for debugging):
%figure;imshow(ii_image - II_image, []);impixelinfo

%Display minimum and maximum - values are not valid as pixel values in JAVA.
disp(['min(ii_image) = ', num2str(min(ii_image(:)))]);
disp(['max(ii_image) = ', num2str(max(ii_image(:)))]);


%Convert II_image range to [0, 1]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Find minimum and maximum (in JAVA you can do it inside the loop).
lo = min(II_image(:));
hi = max(II_image(:));

%Apply linear transformation:
II_image = (II_image - lo) / (hi - lo);

figure;imshow(II_image, []);impixelinfo;title('II\_image');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

希望对您的JAVA实现有所帮助。

关于java - 在 Java 中实现 MATLAB 代码 - 光照不变式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56363623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com