Matrix 基本使用方法(一)

本文详细介绍了 Android 中 Matrix 类的使用方法,包括旋转、缩放、倾斜和平移等图像变换操作。通过实例展示了如何利用 Matrix 实现复杂的图像变换效果,并提供了一种简便的组合变换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

java.lang.Object    ↳

 

android.graphics.Matrix

 

 

Public Constructors
Matrix()
Create an identity matrix
Matrix(Matrix src)
Create a matrix that is a (deep) copy of src

 

 

 

 

 

 


 

旋转

 

void setRotate(float degrees)
Set the matrix to rotate about (0,0) by the specified number of degrees.
void setRotate(float degrees, float px, float py)
Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).

围绕点px, py 旋转 degrees度, 如果没设置坐标,默认以0,0点旋转.

例子: setRotate(45, 180, 120);


 

 

 

缩放,翻转

 

void setScale(float sx, float sy)
Set the matrix to scale by sx and sy.
void setScale(float sx, float sy, float px, float py)
Set the matrix to scale by sx and sy, with a pivot point at (px, py).

以点px,py为原点缩放 >=0   1为正常大小  

如果是负数,图形就会翻转

如果没设置原点坐标,默认以0,0点缩放(如果发现图片不见了,检查一下是不是翻转出了屏幕)

例子:setScale(-0.5f, 1,180, 120);  //左右翻转并缩放到一半大小


 

 

倾斜

 

void setSkew(float kx, float ky, float px, float py)
Set the matrix to skew by sx and sy, with a pivot point at (px, py).
void setSkew(float kx, float ky)
Set the matrix to skew by sx and sy.

以点px,py为原点倾斜如果没有设置原点,则以0,0点为原点.

例子:setSkew(0, 1, 180, 120); //Y 方向拉伸


 

坐标

void setTranslate(float dx, float dy)
Set the matrix to translate by (dx, dy).

是图片移动到某一个位置

 

 

 

注意

Matrix中带有pre, post的函数需要考虑先后顺序

例如:想要旋转45度,然后平移到100,100的位置需要

 

 

Matrix matrix = new Matrix();
matrix.postRotate(45);
matrix.postTranslate(100, 100);  

 

或者

 

Matrix matrix = new Matrix();
matrix.setTranslate(100, 100);
matrix.preRotate(45);

 

 

这就要考虑到矩阵的前乘和后乘了,不然的话你会发现可能坐标位置不是你想要的,可能图像都不见了.

如果在复杂一些,需要选择,缩放,倾斜同时起作用,并且还要设置坐标到屏幕指定位置你会发现很麻烦,需要自己计算出各个方法的参数,然后考虑调用的先后顺序.

 

但这里有一种更简便的方法,叫系统帮我们计算

 

boolean setConcat(Matrix a, Matrix b)
Set the matrix to the concatenation of the two specified matrices, returning true if the the result can be represented.

这个方法的意思是帮我们把两个 Matrix对象计算并连接起来.

这样的话我们就可以这样使用了

 

 

	Matrix mRotateMatrix = new Matrix();	//控制旋转
	Matrix mScaleMatrix = new Matrix();	//控制缩放
	Matrix mSkewMatrix = new Matrix();	//控制倾斜
	Matrix mPosMatrix = new Matrix();		//控制坐标
	Matrix mMatrix = new Matrix();		//合并
		
	mMatrix.setConcat(mRotateMatrix, mScaleMatrix);
	mMatrix.setConcat(mMatrix, mSkewMatrix);
	mMatrix.setConcat(mMatrix, mPosMatrix);
		
	canvas.drawBitmap(mBitmap, mMatrix, mPaint);
 

注意:合并的第一步不能直接用mMatrix自身去连接其他的Matrix,我试过几次结果图像又飞了,大家再试试

 

例子:

同时设置

setRotate(45, 180, 120);

setScale(-0.5f, 1,180, 120);  //左右翻转并缩放到一半大小

setSkew(0, 1, 180, 120);      //Y 方向拉伸


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<think>好的,用户想了解如何在机器学习中使用confusion_matrix计算混淆矩阵。首先,我需要回忆下混淆矩阵的基本概念和相关库的使用方法。混淆矩阵是分类模型评估的重要工具,通常用sklearn的metrics模块里的confusion_matrix函数。 接下来,用户可能想知道具体的步骤,比如如何导入库、准备数据、生成预测结果,然后计算混淆矩阵。需要分步骤说明,并提供示例代码。同时,用户可能对可视化混淆矩阵感兴趣,可以提到使用ConfusionMatrixDisplay或者Seaborn来绘图,这样更直观。 还要注意不同的引用来源,比如引用[2]和[3]提到了sklearn的ConfusionMatrixDisplay和multilabel_confusion_matrix,需要确保信息准确。可能需要解释二分类和多分类的情况,以及如何解读矩阵中的TP、TN、FP、FN。此外,引用[4]提到了保存混淆矩阵到CSV文件,这也是个有用的点,可以包括在回答中。 要检查是否需要区分多标签和多分类的情况,根据引用[3],multilabel_confusion_matrix适用于多标签,但也可以用于多分类。可能需要简要说明这点,但用户主要问题可能集中在常规使用上,所以重点放在基本用法。 最后,确保回答结构清晰,步骤明确,代码正确,并自然地融入引用标记。同时,生成相关问题时要覆盖常见疑问,比如可视化、多分类处理、性能指标等。</think>在机器学习中使用`confusion_matrix`计算混淆矩阵的步骤如下: --- ### 1. **导入相关库** 首先需要导入`sklearn.metrics`中的混淆矩阵函数: ```python from sklearn.metrics import confusion_matrix ``` --- ### 2. **准备真实标签和预测结果** 假设已有真实标签`y_true`和模型预测结果`y_pred`,要求两者是长度相同的数组或列表: ```python y_true = [0, 1, 0, 1, 1, 0] y_pred = [0, 1, 0, 0, 1, 1] ``` --- ### 3. **计算混淆矩阵** 调用`confusion_matrix`函数: ```python cm = confusion_matrix(y_true, y_pred) ``` 此时`cm`将输出个二维数组: $$ \begin{bmatrix} TN & FP \\ FN & TP \end{bmatrix} $$ 例如上述数据的结果为: $$ \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} $$ --- ### 4. **可视化混淆矩阵(可选)** 使用`ConfusionMatrixDisplay`或自定义方法可视化: ```python from sklearn.metrics import ConfusionMatrixDisplay disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=["Class 0", "Class 1"]) disp.plot() ``` --- ### 5. **处理多分类问题** 对于多分类任务,混淆矩阵维度扩展为$n \times n$($n$为类别数)。例如3分类: ```python cm_multi = confusion_matrix(y_true_multi, y_pred_multi) ``` --- ### 6. **多标签混淆矩阵** 使用`multilabel_confusion_matrix`,采用**one-vs-rest策略**,每个类别单独生成个二分类矩阵[^3]: ```python from sklearn.metrics import multilabel_confusion_matrix mcm = multilabel_confusion_matrix(y_true, y_pred) ``` --- ### 7. **保存结果(可选)** 可将混淆矩阵转换为DataFrame并保存为CSV[^4]: ```python import pandas as pd cm_df = pd.DataFrame(cm, index=["True 0", "True 1"], columns=["Pred 0", "Pred 1"]) cm_df.to_csv("confusion_matrix.csv") ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值