如何访问图像的段 [英] How To Access The Segments Of The Image
问题描述
我想提取图像的超像素片段的颜色,形状和纹理特征。然后,我想要可视化这些功能,以选择重要的功能。
I would like to extract color, shape and texture features of superpixel segments of an image . Then, I would like to visualize those features in order to select the important features.
我使用此链接的代码:
https://github.com/np-csu/SLIC-superpixel
I am using the code at this link: https://github.com/np-csu/SLIC-superpixel
我想访问分段图像的每个集群像这项研究:
http:// www.pyimagesearch.com/2014/12/29/accessing-individual-superpixel-segmentations-python/
I would like to access each cluster of the segmented image like this study: http://www.pyimagesearch.com/2014/12/29/accessing-individual-superpixel-segmentations-python/
但是,我找不到相关的部分代码。
However, I couldn' t find the related part of the code.
推荐答案
方法 int * SLIC :: GetLabel()
标签。您可以为 int *
创建头
以方便访问:
The method int* SLIC::GetLabel()
returns the label for each pixel. You can create a Mat
header for the int*
for easy access:
Mat1i labelImg(img.rows, img.cols, slic.GetLabel());
<
Then you can create a mask for each superpixel (label):
Mat1b superpixel_mask = labelImg == label;
并检索原始图片中的超像素:
and retrieve the superpixel in the original image:
Mat3b superpixel_in_img;
img.copyTo(superpixel_in_img, superpixel_mask);
然后,您可以计算所需的任何统计信息。
Then you can compute whatever statistic you need.
这里是完整的参考代码:
Here the full code for reference:
#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;
}
这篇关于如何访问图像的段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!