代码不适用于matlab中的图像处理 [英] Code not working for image processing in matlab

查看:122
本文介绍了代码不适用于matlab中的图像处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在matlab中计算这个公式

I wish to compute this formula in matlab

[m,n,d]=size(img1);
matrix1=sum(abs(img1-img2));
a= matrix1/ m*n ;
b=a*100;

其中img1,img2是两个尺寸为512 * 512 * 3的图像

where img1,img2 are two images of dimension 512*512*3

目标是获得单个数字值,但我得到一个矩阵。在具有i行和j列的矩阵A,B的情况下的actaul公式是
=(求和[abs(A(i,j) - B(i,j))] / m * n)* 100 获得百分位数
我知道它很简单,但我想我错过了什么!!!

The objective is to obtain a single numeral value, but i am getting a matrix. The actaul formula in case of a matrix A,B with i rows and j columns is = (summation [abs(A(i,j)- B(i,j))] / m*n ) * 100 to obtain a percentile I know its very simple, but i think i am missing something !!!

推荐答案

你的图像矩阵是3-D的事实表明它们是 truecolor RGB图像。对于矩阵 M ,第一个颜色平面 M(:,:,1)是红色成分,第二个颜色plane M(:,:,2)是绿色组件,第三个颜色平面 M(:,:,3)是蓝色组件。由于您只讨论公式中前两个维度的求和,因此您必须弄清楚如何处理第三维度。以下是几个选项:

The fact that your image matrices are 3-D indicates that they are truecolor RGB images. For matrix M, the first color plane M(:,:,1) is the red component, the second color plane M(:,:,2) is the green component, and the third color plane M(:,:,3) is the blue component. Since you only discuss summing over the first two dimensions in your formula, you're going to have to figure out how you want to deal with the third dimension. Here are a couple of options:


  • 将公式应用于每个颜色平面:你可以做通过两次调用函数 SUM ,一次对各列进行求和然后再次对行中的结果求和。结果 matrix1 将是1乘1乘3的矩阵,可以使用函数 SQUEEZE 。向量中的每个元素将是每个颜色平面的总和:

  • Apply your formula to each color plane: You can do this by calling the function SUM twice, once to sum across the columns then again to sum that result across the rows. The result matrix1 will be a 1-by-1-by-3 matrix, which can be reshaped to a 3-element column vector using the function SQUEEZE. Each element in the vector will be a summation across each color plane:

matrix1 = squeeze(sum(sum(abs(img1-img2),1),2));

现在你可以使用按元素乘法和除法运算符 。* 。 / 计算最终结果:

Now you can use the element-wise multiply and divide operators .* and ./ to compute the final results:

a = matrix1./(m*n);
b = a.*100;


  • 将图像转换为灰度:如果你只关心颜色强度,您可以将真彩色RGB图像转换为2-D 灰度强度图像使用 RGB2GRAY 来自图像处理工具箱

  • Convert the images to grayscale: If you only care about the color intensity, you can convert the truecolor RGB images to 2-D grayscale intensity images using the function RGB2GRAY from the Image Processing Toolbox:

    img1 = rgb2gray(img1);
    img2 = rgb2gray(img2);
    

    然后你可以打电话给 SUM 两次以对行和列求和,或者使用单冒号索引将每个图像重新整形为列向量并调用 SUM 一次:

    Then you can either call SUM twice to sum across the rows and columns, or use single-colon indexing to reshape each image to a column vector and call SUM once:

    matrix1 = sum(sum(abs(img1-img2)));
    %# OR...
    matrix1 = sum(abs(img1(:)-img2(:)));
    


  • 另外一张便条......

    如果您的图像数据存储为整数类型,您可能希望将图像数据转换为双精度浮点数首先执行以下操作:

    If your image data is stored as an integer type, you will probably want to convert the image data to double precision floating point first by doing the following:

    img1 = double(img1);
    img2 = double(img2);
    

    这将确保求和步骤的结果不会在整数的最大值处饱和类型可以保留。

    This will ensure that the result from the summation step doesn't saturate at the maximum value that the integer type can hold.

    这篇关于代码不适用于matlab中的图像处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆