我正在使用opencv中的超像素编写C ++程序,我真的需要能够访问特定的像素(特别是迭代图像中的每个超像素),我在opencv中使用内置的超像素分割额外模块'opencv2 / ximgproc.hpp'。
以下是与超像素分割有关的代码摘录:
/* preceding code */
int num_iterations = 6;
int prior = 2;
bool double_step = false;
int num_superpixels = 200;
int num_levels = 4;
int num_histogram_bins = 5;
bool init = false;
Mat result, mask;
Ptr<SuperpixelSEEDS> seeds;
int display_mode = 0;
/* Unrelated code... */
while(true) // Feed in video data
{
if(!init)
{
seeds = createSuperpixelSEEDS(frame.size().width, frame.size().height, frame.channels(), num_superpixels, num_levels, prior, num_histogram_bins, double_step);
init = true;
}
seeds->iterate(frame,num_iterations);
seeds->getLabelContourMask(mask,false);
/* More unrelated code... */
Mat labels;
seeds->getLabels(labels);
/* End of superpixel code */
}
文档根本没有帮助我,我真的需要专门访问每个超像素,最好是通过某种标签。
提前致谢
答案 0 :(得分:1)
使用getLabels
,您会得到CV_32SC1
(Mat_<int>
)图像,其中属于同一超像素的像素具有相同的值。标签位于[0, getNumberOfSuperpixels()].
因此,您可以轻松访问每个超像素,迭代标签值,并创建相应的掩码:
//...
Mat labels;
seeds->getLabels(labels);
int N = seeds->getNumberOfSuperpixels();
for(int i=0; i<N; ++i)
{
Mat1b mask_for_ith_sp = (labels == i);
// Now you have the mask of the i-th superpixel.
// You can do whatever you want with it.
//...
}