varData = var(imgData, 0, [1 2]);
时间: 2024-04-03 12:34:03 浏览: 8
`varData = var(imgData, 0, [1 2]);` 是计算 `imgData` 中每张图片的方差。
其中,`var` 函数的第一个参数 `imgData` 是待计算的数据,第二个参数 `0` 表示使用样本方差计算,第三个参数 `[1 2]` 表示按照第一和第二个维度(即前两个索引)计算方差,也就是对每张图片的每个像素点逐个计算方差,得到一个新的矩阵。
具体来说,`imgData` 是一个三维矩阵,大小为 `(height, width, 50)`,其中 `height` 和 `width` 分别表示每张图片的高度和宽度。第三个维度大小为 `50`,表示有50张图片。
使用 `var` 函数计算方差时,指定第二个参数为 `0`,表示使用样本方差计算。指定第三个参数为 `[1 2]`,则表示对第一和第二个维度(即前两个索引)进行方差计算。这样,就得到了一个一维矩阵 `varData`,大小为 `(1, 50)`,其中每个元素的值表示相应图片的方差。
最后,`varData` 中的每个元素都是一个实数,表示该图片的方差。
相关问题
uni.request({ url: config.baseUrl + '/API/Task/getToken', method: 'GET', success(res) { that.baseUrl = res.data.domain that.token = res.data.token; // 获取到 token 后再进行上传 that.imgList.push(e.tempFilePaths[0]); const fileName = e.tempFilePaths[0].substr(e.tempFilePaths[0] .lastIndexOf('/') + 1); // 获取文件名 const fileExt = fileName.substr(fileName.lastIndexOf('.') + 1) .toLowerCase(); // 获取文件拓展名,并转换为小写 const today = new Date().toISOString().slice(0, 10); // 获取当天日期,格式为 yyyy-mm-dd const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate(); const hour = now.getHours(); const minute = now.getMinutes(); const currentDateTime = ${year}-${month}-${day}-${hour}:${minute}:00; var nums = []; that.uploadCount++; // 上传次数加一 const newFileName = currentDateTime + '' + that.uploadCount + '.' + fileExt; // 生成新的文件名,以上传次数为前缀 const path = that.fromData.c_name + '/' + that.fromData.a_name + '/' + that.fromData.m_name + '/' + that.fromData.mold + '/' + that.fromData.m_title + '/' + that.fromData.r_name + '-' + that.uploadCount + '/' + that.fromData.o_name + '/' + today; const key = path + '/' + newFileName that.imgData.push(key) uni.uploadFile({ url: 'https://upload-z1.qiniup.com', filePath: e.tempFilePaths[0], name: 'file', formData: { token: res.data.token, key: path + '/' + newFileName // 将路径和文件名拼接起来 }, success(res) { const data = JSON.parse(res.data); const key = data.key const parts = key.split("."); const KeynewName = parts.slice(0, -1).join(".") + ".jpg"; that.baseList.push(that.baseUrl + KeynewName) that.Keyname.push(KeynewName) for (var i = 0; i < that.Keyname.length; i++) { var num = that.Keyname[i].split('/').slice(-1)[0].split('.')[0].split('')[1]; nums.push(num); } console.log('存在的文件名:'+nums) }, fail(erro) { console.log(erro); } }); }, fail(err) { console.log("获取 Token 失败:", err); } });我想让这里的uploadCount先判断nums中存在哪些,uploadCount只能为1,2,3 如果nums存在1,2那么uploadCount只能为3
可以在上传之前先判断 nums 数组中是否存在 1 和 2,并将结果保存在两个变量中,比如 hasOne 和 hasTwo。然后根据这两个变量来判断 uploadCount 的值,如果 hasOne 和 hasTwo 都为 true,那么 uploadCount 只能为 3,否则 uploadCount 可以为 1、2 或 3。可以参考下面的代码:
```
// 判断 nums 数组中是否存在 1 和 2
var hasOne = nums.includes('1');
var hasTwo = nums.includes('2');
// 根据 hasOne 和 hasTwo 来判断 uploadCount 的值
if (hasOne && hasTwo) {
that.uploadCount = 3;
} else {
that.uploadCount = Math.floor(Math.random() * 2) + 1;
}
```
这段代码中,使用了 includes 方法来判断 nums 数组中是否存在 1 和 2,如果存在则将 hasOne 和 hasTwo 设置为 true,否则为 false。然后根据 hasOne 和 hasTwo 的值来判断 uploadCount 的值,如果都为 true,则 uploadCount 只能为 3,否则可以随机生成 1 或 2。
addPic: function () { let that = this; wx.chooseImage({ count: 1, sizeType: ['original'], success: function(res) { var filePath = res.tempFilePaths[0]; wx.getFileSystemManager().readFile({ filePath: filePath, success: function(res) { wx.cloud .callFunction({ name: "checkImageSec", data: { //使用wx.cloud.CDN避免传输的图片过大 imgData: wx.cloud.CDN({ type: "filePath", filePath: filePath, }), }, }) .then((res) => { if (res.result.errCode === 87014) { wx.showToast({ title: "图片可能违规,请仔细检查后再试!", icon: "none", duration: 5000, }); } else { wx.showToast({ title: "图片检测通过", icon: "none", duration: 1000, }) } }); } }) } }) },这段函数中var filePath = res.tempFilePaths[0];图片可能超过1M会导致云函数报错,所以需要对图片进行压缩在上传云函数,怎么这个函数中添加图片压缩这个功能
可以使用第三方库node-image-resizer来实现图片压缩功能。首先需要安装node-image-resizer,在云函数中引入该库:
```
const imageResizer = require('image-resizer');
```
然后在success回调函数中添加图片压缩代码:
```
wx.getFileSystemManager().readFile({
filePath: filePath,
success: function(res) {
imageResizer(res.data, {
width: 800, // 设置压缩后的宽度
quality: 80 // 设置压缩质量,值越大质量越高
}, function(err, buffer, image) {
if (err) {
console.error(err);
return;
}
wx.cloud.callFunction({
name: "checkImageSec",
data: {
imgData: wx.cloud.CDN({
type: "fileBuffer",
buffer: buffer,
}),
},
}).then((res) => {
if (res.result.errCode === 87014) {
wx.showToast({
title: "图片可能违规,请仔细检查后再试!",
icon: "none",
duration: 5000,
});
} else {
wx.showToast({
title: "图片检测通过",
icon: "none",
duration: 1000,
})
}
});
});
}
})
```
在这个代码中,我们使用了image-resizer库来压缩图片,并将压缩后的图片以buffer形式传递给云函数。注意,使用buffer传递图片时,需要将wx.cloud.CDN的type设置为"fileBuffer"。