在opencv4nodejs中如何在调用drawRectangle时设置Alpha

问题描述

我想要一个透明的PNG,除了在正方形上绘制。我可以使用Python来实现,但是我想使用Node来实现。

如何使用opencv4nodejs将Python代码转换为Node代码?

import cv2
import numpy as np

blank = np.zeros((400,500,4))
cv2.rectangle(blank,(10,10),(50,50),(0,255,255) )
cv2.imwrite("blankpy.png",blank)

blankpy.png(透明和绘制的正方形):

enter image description here

这是我的尝试,但输出并不完全相同

const cv = require('./lib/opencv4nodejs');

const blank = new cv.Mat(400,cv.CV_8UC4);
blank.drawRectangle(new cv.Rect(10,10,50,new cv.Vec4(0,255),2,cv.LINE_8);
cv.imwrite('blankjs.png',blank);

blankjs.png(透明,但没有正方形):

enter image description here

如果我像这样略微更改它,则输出不是透明的,但是正方形在那里。

const cv = require('./lib/opencv4nodejs');

const emptyMat = new cv.Mat(400,cv.CV_8UC3);
emptyMat.drawRectangle(new cv.Rect(10,new cv.Vec3(0,0),emptyMat);

blankjs.png(绿色正方形,但背景不透明):

enter image description here

解决方法

我的解决方法:

const cv = require('./lib/opencv4nodejs');

const emptyMat = new cv.Mat(400,500,cv.CV_8UC4,[0,255]);
emptyMat.drawRectangle(new cv.Rect(10,10,50,50),new cv.Vec3(255,255),2);
const invertedEmptyMat = emptyMat.bitwiseNot();

cv.imwrite('blankjs.png',invertedEmptyMat);

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...