matlab图像的矩阵点乘后,所得的值溢出问题,该怎么解决?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab图像的矩阵点乘后,所得的值溢出问题,该怎么解决?相关的知识,希望对你有一定的参考价值。
一幅灰度图像的灰度值的范围是0到255,两幅相同大小的图像相点乘,得到的大部分结果都是大于255,比如60乘以60,得到3600,这些点因为大于255而被规定为255,结果远远超出255,画出来的新图像是绝大部分为白点,请问该怎么做才能确保每个数字都在255以内,让两个图像相乘变得有意义或者能不太失真地显示出来。是不是有什么算法?或者函数
第一个图就是后两个点乘得到的
g2 = rgb2gray(imread('wm02.jpg'));
subplot(131); imshow(g1);
subplot(132); imshow(g2);
g3 = sqrt(double(g1)).*sqrt(double(g2));
g3 = uint8(g3);
subplot(133); imshow(g3); 参考技术A 假设两个图分别存在两个变量a,b中
a,b应该i啊是uint8类型的数据,也就是8为无符号整数
c=double(a).*double(b); %化为浮点数相乘
c=uint8(c./max(c(:))*255); %将范围变为0~255转换为unit8类型
得到的c的范围就在0~255之间,是按原来乘的结果按比例转换的结果
matlab矩阵中的 */.*
说实话,我知道今天才会意识到其实这些运算困扰了我不止一次两次,每次都会去查Help文档,真的是困惑的不行不行,因此,打算今天彻底的将其捋一遍。
文章目录
点乘(·*)与星乘 *
点乘(·*):矩阵A和B对应项相乘
类型:(1) A:m×n; B: m×n (2) A:m×n; B:1×n (列要一致)(3)A:m×n;B:m×1 (行一致),且后两种情况为行或者列向量时才可成立,否则需要两个矩阵具有相同的行列维度
例:
x3 =
2 3
3 4
y3 =
2 1
x3.*y3
ans =
4 3
6 4
y4 =
2
1
x3.*y4
ans =
4 6
3 4
星乘(*):矩阵A的列项数与矩阵B的行项数一致
类型:A:m×n; B:n×l
例:
x3 =
2 3
3 4
y4 =
2
1
x3*y4
ans =
7
10
右除(/)与左除(\\)
左除:Solve systems of linear equations Ax = B for x (mldivide)
矩阵左除有以下几种情况:
x = A\\B solves the system of linear equations A*x = B. The matrices A and B must have the same number of rows. MATLAB® displays a warning message if A is badly scaled or nearly singular, but performs the calculation regardless.
If A is a scalar, then A\\B is equivalent to A.\\B.
If A is a square n-by-n matrix and B is a matrix with n rows, then x = A\\B is a solution to the equation A*x = B, if it exists.
If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with m rows, then A\\B returns a least-squares solution to the system of equations A*x= B.
也就是说若A是矩阵,则矩阵B必须要与A有相同的行数
例:
A = [1 2 0; 0 4 3];
b = [8; 18];
x = A\\b
x =
0
4.0000
0.6667
这个函数实则是求解线性方程的,即,
3
x
1
+
2
x
2
+
1
=
6
3
x
2
+
1
=
7
\\begincases 3x1+2x2+1=6 \\\\ 3x2+1=7 \\endcases
3x1+2x2+1=63x2+1=7
这里A=[3 2 1;0 3 1] ;X=[x1;x2;1],B=[6;7],因此X=A\\B
那么对于回归分析来说,则系数矩阵代表的是X,变量则是A(样本值),因此X=A\\B,求得的是系数矩阵
右除:Solve systems of linear equations xA = B for x (mrdivide)
矩阵右除有三种情况:
x = B/A solves the system of linear equations x*A = B for x. The matrices A and B must contain the same number of columns. MATLAB® displays a warning message if A is badly scaled or nearly singular, but performs the calculation regardless.
If A is a scalar, then B/A is equivalent to B./A.
If A is a square n-by-n matrix and B is a matrix with n columns, then x = B/A is a solution to the equation x*A = B, if it exists.
If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with n columns, then x = B/A returns a least-squares solution of the system of equations x*A = B
右除当A为矩阵时,要求B为与A同列项数的矩阵
例:
A = [1 1 3; 2 0 4; -1 6 -1];
B = [2 19 8];
x = B/A
x =
1.0000 2.0000 3.0000
以上是关于matlab图像的矩阵点乘后,所得的值溢出问题,该怎么解决?的主要内容,如果未能解决你的问题,请参考以下文章