我有以下用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通道RGB图像转换为其照度不变。我想知道代码在做什么,以便可以在Java中实现它。
根据我的收集,它正在计算每个红色/绿色/蓝色像素的对数,并将它们彼此相减,但结果不是整数,因此我无法将其应用于Java的{更改RGB值时为{1}}类。如何在Java中模拟此功能?
答案 0 :(得分:1)
将函数转换为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。
答案 1 :(得分:0)