我目前正在处理一个处理热图像的项目,并且必须对结果图像进行一些调整。我目前正在用 javascript 调整大小(在从 web 服务器发送数据之后),这非常有效,不幸的是,一个新的要求意味着我必须使用 python 将这个调整大小转移到 web 服务器。
我已经这样做了,但是当我调整大小时,我现在得到了一个“三重重叠图像”,我没有在 javascript 端得到它(见截图)
Python(工作版)
nparr = np.frombuffer(temps, dtype=np.float32)
temparr = np.copy(nparr)
temparr *= (1.0/temparr.max())
// array comes in single dimension, reshapre to (width,height)
temps = temparr.reshape(int(temp_frame["width"]), int(temp_frame["height"]))
socketio.emit("thermal_image", {'data':b''+bytearray(temps),'width':temps.shape[0], 'height':temps.shape[1]}, broadcast=True)
Javascript(工作版)
let bytes = new Float32Array(msg.data);
let width = msg.width;
let height = msg.height;
var mat = cv.matFromArray(height, width, cv.CV_32FC1, bytes);
let dst_mat = new cv.Mat();
let dsize = new cv.Size(1000,1000);
cv.resize(mat, dst_mat, dsize,0,0,cv.INTER_AREA);
cv.imshow(canvas_name, dst_mat);
以及新的 python 代码,以及用于显示的 javascript
Python(非工作版本)
nparr = np.frombuffer(temps, dtype=np.float32)
temparr = np.copy(nparr)
temparr *= (1.0/temparr.max())
temps = temparr.reshape(int(temp_frame["width"]), int(temp_frame["height"]))
resized_temps = np.zeros((1000,1000), np.float32)
cv2.resize(temps, dsize=(1000,1000), dst=resized_temps, fx=0, fy=0,interpolation=cv2.INTER_AREA)
socketio.emit("thermal_image", {'data':b''+bytearray(resized_temps),
'width':resized_temps.shape[0],
'height':resized_temps.shape[1]},
broadcast=True)
Javascript(不工作版本)
let bytes = new Float32Array(msg.data);
let width = msg.width;
let height = msg.height;
var mat = cv.matFromArray(height, width, cv.CV_32FC1, bytes);
cv.imshow(canvas_name, dst_mat);
正如您所看到的,唯一的区别是在 javascript 或 python 中调整大小,但结果介于非常好和几乎没用之间。
我知道这是我做错的事情,但我一生都无法弄清楚。
这些图像是同一个人,姿势略有不同。第二张图片是使用python,仍然可以看到运动,如果定位正确,可以看到场景中的对象.. 3次。
编辑:将图像更改为更清晰的图像
最佳答案
愚蠢的错误,但万一其他人在像我这样不眠不休后遇到它,我会发布答案:
我在数据中接收到一个扁平的字节数组,然后重新整形为形状(宽度,高度)。这在 javascript 中进行调整大小时效果很好,因为我再次将扁平数组发送到 javascript 并且调整大小已完成(高度,宽度)。这就是 opencv 取值的方式。
移动到 python 时,我忘记将 reshape 更改为(height,width),而是保留(width,height)。因此,当 opencv 进行调整大小时,我们在错误的方向上进行插值,从而导致线条和重复的图像。
所以解决方案,
换线temps = temparr.reshape(int(temp_frame["width"]), int(temp_frame["height"]))
temps = temparr.reshape(int(temp_frame["height"]), int(temp_frame["width"]))
瞧,opencv 表现不错
关于javascript - OpenCV在python和javascript之间调整了大不相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62093616/