gpt4 book ai didi

c++ - 如何访问图像的片段

转载 作者:行者123 更新时间:2023-11-28 02:20:09 24 4
gpt4 key购买 nike

我想提取图像超像素片段的颜色、形状和纹理特征。然后,我想可视化这些特征以便选择重要的特征。

我正在使用此链接中的代码: https://github.com/np-csu/SLIC-superpixel

我想像这项研究一样访问分割图像的每个集群: http://www.pyimagesearch.com/2014/12/29/accessing-individual-superpixel-segmentations-python/

但是,我找不到代码的相关部分。

最佳答案

int* SLIC::GetLabel() 方法返回每个像素的标签。您可以为 int* 创建一个 Mat header 以便于访问:

Mat1i labelImg(img.rows, img.cols, slic.GetLabel());

然后你可以为每个超像素(标签)创建一个掩码:

Mat1b superpixel_mask = labelImg == label;

并检索原始图像中的超像素:

Mat3b superpixel_in_img;
img.copyTo(superpixel_in_img, superpixel_mask);

然后您可以计算您需要的任何统计数据。

完整代码供引用:

#include <opencv2/opencv.hpp>
#include "slic.h"

int main()
{
// Load an image
Mat3b img = imread("path_to_image");

// Set the maximum number of superpixels
UINT n_of_superpixels = 200;
SLIC slic;

// Compute the superpixels
slic.GenerateSuperpixels(img, n_of_superpixels);

// Visualize superpixels
//Mat3b res = slic.GetImgWithContours(Scalar(0,0,255));

// Get the labels
Mat1i labelImg(img.rows, img.cols, slic.GetLabel());

// Get the actual number of labels
// may be less that n_of_superpixels
double max_dlabel;
minMaxLoc(labelImg, NULL, &max_dlabel);
int max_label = int(max_dlabel);

// Iterate over each label
for (int label = 0; label <= max_label; ++label)
{
// Mask for each label
Mat1b superpixel_mask = labelImg == label;

// Superpixel in original image
Mat3b superpixel_in_img;
img.copyTo(superpixel_in_img, superpixel_mask);

// Now you have the binary mask of each superpixel: superpixel_mask
// and the superpixel in the original image: superpixel_in_img
}

return 0;
}

关于c++ - 如何访问图像的片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32822856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com