简体   繁体   English

Matlab函数中使用的数据类型

[英]Data types use in Matlab functions

I am new to Matlab. 我是Matlab的新手。 I have this function which I want to calculate the Euclidean distance between two pixels(RGB). 我有这个函数,我想计算两个像素(RGB)之间的欧几里得距离。

function[distance]=calc_dist(R1, R2, G1, G2, B1, B2)

    if (R1>R2)
        dR=R1-R2;
    else
        dR=R2-R1;
    end

    if (G1>G2)
        dG=G1-G2;
    else
        dG=G2-G1;
    end

    if (B1>B2)
        dB=B1-B2;
    else
        dB=B2-B1;
    end

  sum=uint64(3*dR*dR+4*dG*dG+2*dB*dB);
  disp(sum);
  distance=(sqrt(double(3*dR*dR+4*dG*dG+2*dB*dB));
end

The problem is the displayed value for sum is 255 each time. 问题是每次显示的总和值为255。 This must be happening because the variables are of type uint8. 因为变量的类型为uint8,所以必须这样做。 How do I change them? 如何更改它们? I tried to do some sort of casting 我试图做一些铸造

sum=uint64(3*dR*dR+4*dG*dG+2*dB*dB); 

but I get this error: 'Undefined function 'uit64' for input arguments of type 'uint8'. 但我收到此错误:类型为'uint8'的输入参数为'Undefined function'uit64'。 How should I display the right value for the sum? 如何显示正确的总和值? Thanks. 谢谢。 '

Consider converting your input of 6 variables to one 2x3 matrix, where the first row is the RGB colours from one pixel, and the second row is the RGB colours from the second pixel: 考虑将6个变量的输入转换为一个2x3矩阵,其中第一行是一个像素的RGB颜色,第二行是第二个像素的RGB颜色:

function[distance]=calc_dist(R1, R2, G1, G2, B1, B2)

rgbPixels = [R1 G1 B1; R2 G2 B2];

% cast as double
rgbPixels = double(rgbPixels);

% compute the difference between the rows
rgbDiffs = diff(rgbPixels);

% compute the Euclidean distance
distance = sqrt(sum(rgbDiffs.^2));

This way, you don't have to change your signature and all casting can be done in one line. 这样,您不必更改签名,并且所有转换都可以在一行中完成。 Try the above and see what happens! 尝试以上操作,看看会发生什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM