OpenCV 调整 python 和 javascript 之间的结果大不相同
OpenCV resize substantially different results between python and javascript
我目前正在处理一个处理热图像的项目,我必须对生成的图像进行一些调整。我目前正在 javascript 中调整大小(从网络服务器发送数据后),这非常有效,不幸的是,一个新的要求意味着我不得不将这个调整转移到网络服务器上,使用 python.
我已经这样做了,但是当我调整大小时,我现在得到一个 'triple overlapping image',我没有在 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次。
编辑:将图像更改为更清晰的图像
愚蠢的错误,但如果其他人像我一样在失眠后遇到它,我会 post 答案:
我在数据中接收到一个展平的字节数组,然后重新整形为形状(宽度、高度)。这在 javascript 中进行调整大小时效果很好,因为我再次将展平数组发送到 javascript 并且调整大小已完成(高度,宽度)。这就是 opencv 取值的方式。
移动到 python 时,我忘记将整形更改为(高度、宽度),而是保持(宽度、高度)。因此,当 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 中调整大小(从网络服务器发送数据后),这非常有效,不幸的是,一个新的要求意味着我不得不将这个调整转移到网络服务器上,使用 python.
我已经这样做了,但是当我调整大小时,我现在得到一个 'triple overlapping image',我没有在 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次。
编辑:将图像更改为更清晰的图像
愚蠢的错误,但如果其他人像我一样在失眠后遇到它,我会 post 答案:
我在数据中接收到一个展平的字节数组,然后重新整形为形状(宽度、高度)。这在 javascript 中进行调整大小时效果很好,因为我再次将展平数组发送到 javascript 并且调整大小已完成(高度,宽度)。这就是 opencv 取值的方式。
移动到 python 时,我忘记将整形更改为(高度、宽度),而是保持(宽度、高度)。因此,当 opencv 调整大小时,我们在错误的方向上插值,导致线条和重复图像。
所以解决方案,
换行
temps = temparr.reshape(int(temp_frame["width"]), int(temp_frame["height"]))
temps = temparr.reshape(int(temp_frame["height"]), int(temp_frame["width"]))
瞧,opencv 表现不错