惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Announcements
Recent Announcements
IT之家
IT之家
Google DeepMind News
Google DeepMind News
罗磊的独立博客
爱范儿
爱范儿
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
U
Unit 42
MongoDB | Blog
MongoDB | Blog
S
SegmentFault 最新的问题
B
Blog
博客园 - 叶小钗
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 凤凰_1

多边形近似:最小周长多边形算法MPP 多阈值otsu的opencv实现 在Opencv中自定义了一个相位函数,获取复数矩阵的相位 cvIsInf与cvIsNan函数 Mandelbrot set 以parallel_for_实现 opencv的并行操作parallel_for_的问题 CV_WRAP和CV_EXPORTS_W opencv中自定义的双线性二次插值的图像旋转及缩放 在Qt中,如何在sheets末端添加新的sheet opencv4.x 中的plot函数绘制二维Mat C++版DNN最简主体框架 C++版全连接神经网络 C++版加载MNIST图像集合 C++版的神经网络训练用的图像集合加载 c\c++宏定义,四个参数求最大值 - 凤凰_1 - 博客园 自定义c++二维动态数组 用Cmake 编译OpenCV常见的错误 Opencv中的dft()和idft()示例 C++仿Matlab的bsxfun函数
opencv超级像素示例
凤凰_1 · 2024-05-26 · via 博客园 - 凤凰_1
/*
 * createSuperpixelLSC(cv::InputArray img,int region_size,float ratio)
其中各个参数意义如下:
    image:输入图像
    region_size :平均超像素大小,默认10
    ratio:超像素紧凑度因子,默认0.075
*/
void superpixelLSC(const Mat& img)
{
    cv::Ptr<cv::ximgproc::SuperpixelLSC> lsc=cv::ximgproc::createSuperpixelLSC(img);
    lsc->iterate(10);
    Mat labels;
    lsc->enforceLabelConnectivity();
//    int numOfSuperpixels=lsc->getNumberOfSuperpixels();
//    cout<<"numOfSuperpixels="<<numOfSuperpixels<<endl;
    lsc->getLabels(labels);
    Mat mask_lsc;
    lsc->getLabelContourMask(mask_lsc);//获取像素分割边界映射图,该图与原图像一样尺寸
    Mat mask_inv_lsc;
    cv::bitwise_not(mask_lsc,mask_inv_lsc);//将分割边界映射图按位取反
    Mat imgSeg;
    cv::bitwise_and(img,img,imgSeg,mask_inv_lsc);//将边界映射图叠加到原图像上
    imshow(" segmentation",imgSeg);
}
/*
 * ximgproc::createSuperpixelSLIC(cv::inputArray image, int algorithm=SLICO, int region_size,float ruler    )
其中各个参数意义如下:
   image :输入图像
   algorithm:选择要使用的算法变体:SLIC、SLICO(默认)和MSLIC三种可选
   region_size:平均超像素大小,默认10
   ruler:超像素平滑度,默认10
*/
void superpixelSLIC(const Mat&img)
{
    //初始化slic项,超像素平均尺寸20(默认为10),平滑因子20
    Mat imgLab;
    cvtColor(img,imgLab,COLOR_BGR2Lab);
    cv::Ptr<cv::ximgproc::SuperpixelSLIC> slic=cv::ximgproc::createSuperpixelSLIC(imgLab,cv::ximgproc::SLIC,20,20);
    slic->iterate(10);//迭代次数,越大效果越好
    Mat mask_slic,mask_inv_slic,label_slic;
    slic->getLabelContourMask(mask_slic);//获取Mask,超像素边缘Mask==1
    cv::bitwise_not(mask_slic,mask_inv_slic);
    Mat imgSeg;
    cv::bitwise_and(img,img,imgSeg,mask_inv_slic);
    slic->getLabels(label_slic);//获取超像素标签
    imshow("SLIC segmentation",imgSeg);
//    int numOfSuperpixels=slic->getNumberOfSuperpixels();
//    cout<<"numOfSuperpixels="<<numOfSuperpixels<<endl;


}
/*
 * createSuperpixelSEEDS(int image_width,int image_height, int image_channels,
 *                       int num_superpixels,int prior
其中各个参数意义如下:
    image_width :输入图像宽度
    image_height: 输入图像高度
    image_channels :输入图像通道数
    num_superpixels :期望超像素数目
    num_levels :块级别数,值越高,分段越准确,形状越平滑,但需要更多的内存和CPU时间。
    histogram_bins: 直方图bins数,默认5
    double_step: 如果为true,则每个块级别重复两次以提高准确性默认false。

原文链接:https://blog.csdn.net/qq_40268412/article/details/103915197
*/
void superpixelSeeds(const Mat&img)
{
    int num_superpixels=2000;
    int num_levels=3;
    cv::Ptr<cv::ximgproc::SuperpixelSEEDS> seeds=
            cv::ximgproc::createSuperpixelSEEDS(img.cols,img.rows,img.channels()
                                                 ,num_superpixels,num_levels);
    Mat mask_seeds,mask_inv_seeds,label_seeds,img_seeds;
    seeds->iterate(img,10);
    seeds->getLabels(label_seeds);
    seeds->getLabelContourMask(mask_seeds);
    cv::bitwise_not(mask_seeds,mask_inv_seeds);
    cv::bitwise_and(img,img,img_seeds,mask_inv_seeds);
    cout<<"label_seeds.channels()="<<mask_seeds.channels()<<endl;
    cout<<"label_seeds.size="<<mask_seeds.size()<<endl;
    imshow("seeds image",img_seeds);
}

int main()
{
    Mat img=imread("D:/Qt/MyImage/baboon.jpg",1);

    superpixelSeeds(img);
//    superpixelLSC(img);


    waitKey();
    return 0;
}