使用opencv.js处理图像连通域
项目上用到了segmentAnything,得到的蒙版有可能会有一些小的干扰区域,这个时候可以通过计算连通域,剔除掉脱离主体的干扰部分,主要用到opencv的 connectedComponentsWithStats函数
js中的函数声明 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* This is an overloaded member function, provided for convenience. It differs from the above function
* only in what argument(s) it accepts.
*
* @param image the 8-bit single-channel image to be labeled
*
* @param labels destination labeled image
*
* @param stats statistics output for each label, including the background label, see below for
* available statistics. Statistics are accessed via stats(label, COLUMN) where COLUMN is one of
* ConnectedComponentsTypes. The data type is CV_32S.
*
* @param centroids centroid output for each label, including the background label. Centroids are
* accessed via centroids(label, 0) for x and centroids(label, 1) for y. The data type CV_64F.
*
* @param connectivity 8 or 4 for 8-way or 4-way connectivity respectively
*
* @param ltype output image label type. Currently CV_32S and CV_16U are supported.
*/
export declare function connectedComponentsWithStats(image: InputArray, labels: OutputArray, stats: OutputArray, centroids: OutputArray, connectivity?: int, ltype?: int): int;
假设输入是1024*768的图像
image
是输入图像, Uint8的Mat,尺寸为1024*768*4
labels
是输出结果,Int32的Mat, 尺寸为1024*768
stats
是每个连通域的信息,大小为 连通域数量
*
5,每个row
分别是长度为5的数组,分别记录了连通域外接矩形左上角像素点的x
,y
,宽度
,高度
和连通域的面积
centroids
是连通域的中心点坐标
1 | // 1. 将原始蒙版转化为灰度图像 |