SLIC超像素分割算法和目前超像素算法的比较代码实现

合集下载

超分辨率算法代码

超分辨率算法代码

超分辨率算法代码超分辨率算法是一种能够提高图像分辨率的技术,通常用于将低分辨率图像转换为高分辨率图像。

下面是一个简单的超分辨率算法代码示例,使用Python和OpenCV库实现:```pythonimport cv2import numpy as np读取低分辨率图像img_low = ('low_res_', _GRAYSCALE)定义超分辨率算法的参数scale = 4 放大倍数kernel_size = 5 卷积核大小sigma = 高斯模糊的标准差对低分辨率图像进行双线性插值放大img_upsampled = (img_low, None, fx=scale, fy=scale, interpolation=_LINEAR)对放大后的图像进行高斯模糊处理img_blurred = (img_upsampled, (kernel_size, kernel_size), sigma)对模糊后的图像进行边缘检测edges = (img_blurred, 50, 150)对边缘检测后的图像进行反投影,得到反投影图像backproject = (edges, 0) img_blurred对反投影图像进行双线性插值放大,得到最终的高分辨率图像final_img = (backproject, None, fx=scale, fy=scale, interpolation=_LINEAR)显示最终的高分辨率图像('Final Image', final_img)(0)()```这个代码示例使用了双线性插值、高斯模糊、边缘检测和反投影等技术,实现了简单的超分辨率算法。

你可以根据自己的需求调整参数和算法。

语义分割中的图像超像素算法研究

语义分割中的图像超像素算法研究

语义分割中的图像超像素算法研究概述:语义分割是计算机视觉领域的重要研究方向,其目标是将图像中的每个像素分配给不同的语义类别。

图像超像素算法能够有效地将图像分割成具有语义关联的区域,进一步提高语义分割的准确性和效率。

本文将研究语义分割中的图像超像素算法,并探讨其在计算机视觉领域的应用前景。

1. 引言语义分割是图像分割的一种高级形式,其目标是将图像中的每个像素分配给不同的语义类别,如人、车、建筑物等。

然而,传统的图像分割方法存在一些问题,如过度分割、缺乏语义上下文等。

为了解决这些问题,图像超像素算法成为了一种有效的解决方案。

2. 图像超像素算法的原理图像超像素算法将图像分割成具有语义关联的区域,每个区域被称为超像素。

它基于图像中的颜色、纹理和边缘等特征将相邻像素组合在一起,形成具有语义一致性的区域。

常见的图像超像素算法包括SLIC、Felzenszwalb和Huttunen等。

3. SLIC算法SLIC算法是一种基于k-means聚类的图像超像素算法。

它将图像分割成具有较为均匀大小的超像素,同时保持超像素与语义区域的一致性。

SLIC算法采用了两个重要的策略:超像素初始种子的选择和超像素的紧凑性约束。

该算法的时间复杂度较低,并且在提高图像分割的准确性方面表现出色。

4. Felzenszwalb算法Felzenszwalb算法是一种基于图的聚类的图像超像素算法。

它使用图的分割和合并策略将像素组合成连通的区域,并且考虑了在颜色、纹理和边缘等方面的相似性。

该算法的主要优势在于能够处理复杂的图像背景和不规则的目标形状。

5. Huttunen算法Huttunen算法是一种基于支持向量机的图像超像素算法。

它使用支持向量机来学习图像中像素的语义关系,并根据学习到的模型将图像分割成具有语义一致性的超像素。

该算法在保持超像素边界清晰的同时,能够更好地捕捉图像中的语义信息。

6. 图像超像素算法的应用图像超像素算法在计算机视觉领域有着广泛的应用。

SLIC算法范文

SLIC算法范文

SLIC算法范文SLIC(Simple Linear Iterative Clustering)算法是一种常用的超像素分割算法,用于将图像分割成具有相似属性的区域。

SLIC算法简单、高效,可以应用于各种计算机视觉和图像处理任务中。

本文将详细介绍SLIC算法的原理、步骤和应用。

一、SLIC算法原理SLIC算法的原理基于K-means聚类算法,其目标是将图像划分为尽可能均匀的超像素,即每个超像素内的像素具有相似的颜色和纹理特征。

SLIC算法的输入是一幅RGB彩色图像和所需的超像素数量,输出是一组超像素区域。

1.初始化:根据所需的超像素数量,确定每个超像素的大小。

通过均匀地放置一组初始种子点来初始化每个超像素的中心。

2.聚类:对于每个种子点,计算其周围一定范围内的像素与该种子点的相似度。

相似度可以根据像素之间的颜色差异、位置差异、纹理差异等来度量。

3.更新超像素中心:根据每个超像素内所有像素的位置和颜色信息,重新计算超像素的中心点。

4.重复步骤2和步骤3,直到达到迭代的停止条件。

停止条件可以是达到最大迭代次数或者超像素的中心点的变化量小于一个阈值。

5.合并边界:根据超像素的边界信息,将相邻的超像素进行合并,使得超像素区域更加连续。

二、SLIC算法优势1.超像素形状紧凑:通过限制超像素的均匀分布和大小,SLIC算法能够生成具有较紧凑形状的超像素区域。

2.保持边界信息:SLIC算法使用了像素之间的空间距离和颜色相似度来计算超像素中心,从而可以保持图像中的边界信息。

3. 快速计算:SLIC算法通过将图像转换为Lab颜色空间,从而减少了颜色相似度计算的复杂度,大大提高了算法的计算速度。

4.参数灵活:SLIC算法的参数设置灵活,可以根据实际需求来调整超像素的大小和数量。

三、SLIC算法应用1.图像分割:SLIC算法可以将图像划分为具有相似属性的区域,便于后续的目标检测、图像分析等任务。

2.物体识别:SLIC算法可以提取出具有显著性的超像素区域,用于物体的识别和跟踪。

融合局部纹理特征的颗粒图像SLIC超像素分割方法

融合局部纹理特征的颗粒图像SLIC超像素分割方法

融合局部纹理特征的颗粒图像SLIC超像素分割方法李啸宇;张秋菊【摘要】将 SLIC超像素分割的方法引入颗粒图像检测的分割过程中,将颗粒图像分割成感兴趣的超像素块,可降低后续图像处理过程的复杂度。

由于 SLIC超像素分割在聚类过程中计算相似度时没有考虑图像的纹理特征,一定程度上会影响颗粒目标外轮廓分割的细节。

利用CRLBP局部纹理算子纹理特征,改进 SLIC分割中聚类相似度的计算,并按照符合颗粒形状的圆形邻域搜索相似点,保证了分割速度。

通过对棉种颗粒图像的分割试验,与传统分水岭算法和SLIC 超像素算法进行比较,结果表明改进的 SLIC 超像素分割方法能更有效地分割出颗粒目标。

%This paper adopts SLIC-based superpixel segmentation method in the granular image detection.SLIC method segments the granular image into superpixel block which will reduce the complexity of the subsequent image processing.As SLIC superpixel segmentation method doesn’t use the texture feature in the distance calculation,the detail of the outline for the granular object is lost. This paper adopts the CRLBP local texture operator as the texture feature to improve the SLIC segmentation’s distance calculation and searches the similar pixel in circle neighborhood pixels to guarantee the processing speed.The test on cotton seed image shows that the improved SLIC superpixel segmentation method is more efficient than watershedand original SLIC method.【期刊名称】《食品与机械》【年(卷),期】2016(032)012【总页数】5页(P31-34,39)【关键词】颗粒图像;超像素分割;SLIC;局部纹理算子;CRLBP【作者】李啸宇;张秋菊【作者单位】江南大学机械工程学院,江苏无锡 214122; 江苏省食品先进制造装备与技术重点实验室,江苏无锡 214122;江南大学机械工程学院,江苏无锡214122; 江苏省食品先进制造装备与技术重点实验室,江苏无锡 214122【正文语种】中文在农业生产过程中,经常需要对颗粒状农产品进行视觉检测与品质分选[1]。

融合FPGA技术的改进SLIC超像素分割算法

融合FPGA技术的改进SLIC超像素分割算法

融合FPGA技术的改进SLIC超像素分割算法作者:韩剑辉吕郅强来源:《哈尔滨理工大学学报》2020年第01期摘要:针对众多图像分割算法中计算量大,算法过程复杂,算法运行速率較慢等问题,文中采用将超像素图像分割技术与FPGA并行处理技术相结合的方法,完成了一种在FPGA平台上的超像素图像分割算法的设计。

SLIC是一种快速的超像素分割算法,相较于传统的图像分割算法,SLIC算法拥有较高的实时性与良好的分割效果。

通过研究SLIC算法的原理,优化原算法操作并对原图像执行降尺度操作大幅度降低了算法的运算量,在降尺度图像的分割结果上使用K近邻分类算法还原对原图像的分割结果。

最终结合FPGA技术对改进后的算法进行模块化功能设计,完成了在FPGA平台上的算法实现。

实验仿真结果表明,本算法的分割效果优秀,相较于原算法处理速度约有40%的提升,具有更高的实时性。

关键词:SLIC算法;超像素分割;FPGA;K近邻分类DOI:10.15938/j.jhust.2020.01.009中图分类号: TP391-41文献标志码: A文章编号: 1007-2683(2020)01-0059-07Abstract:In view of the large amount of calculations, complexity of algorithm and the implementation is slowThe paper combines superpixel segmentation technology with FPGA parallel processing technology, and puts forward a method to realize the image segmentation algorithm on FPGA platformSLIC is a kind of fast image segmentation algorithmSLIC has a lot of improvements in efficiency, costing and segmentation results compared with traditional image segmentation algorithmOn the basis of the principle of SLIC segmentation algorithm, we made a further improvement algorithm by optimizing the operation and extracting a small number of pixels of the original image to reduce computational complexityFinally, the last of the original image segmentation was achieved by K nearest neighbor classification processWe completed the algorithm design on FPGA platformThe simulation results show that the improved algorithm has a better segmentation results and the processing speed has about 40% promotionAnd the improved algorithm has a higher real-time performance-Keywords:simple linear iterative cluster; uperpixel egmentation; field-programmable gate array; K nearest neighbour0 引言目前,数字图像处理技术随着计算机水平的快速发展,其中图像分析处理的过程已慢慢发展成了一套独立的系统。

超像素图像分割的研究与优化

超像素图像分割的研究与优化

超像素图像分割的研究与优化随着计算机技术的飞速发展,图像处理技术也日趋成熟。

已经涌现出了许多基于人工神经网络、深度学习、卷积神经网络等算法的图像处理方法,其中之一就是超像素图像分割。

超像素图像分割能够将原图像分割成多个相似区域,可广泛应用于图像处理领域,如图像分析、特征提取、目标跟踪等。

本文将重点讨论超像素图像分割的研究与优化。

一、超像素图像分割的基本概念超像素图像分割是指将多个像素集合成一个相似的区域,即将图像分成几个块来进行处理,这些块称之为超像素。

超像素图像分割能够消除图像中的噪音点和冗余信息,降低图像处理的复杂度,保持图像的边缘特征。

超像素图像分割由于将像素分成一组相邻的像素,因此更能保留图像的本质。

二、超像素图像分割的优点和缺点超像素图像分割优点在于保留了图像的结构和边缘特征,同时还能提高图像处理的速度和质量。

但是,超像素图像分割也存在一些缺点。

对于图像中的平滑区域,生成的超像素有时候并不稳定,会造成图像失真。

在边缘处,由于多个超像素相邻,边缘效果可能被过度削弱。

三、超像素图像分割的优化方法为了优化超像素图像分割质量,研究者们提出了一系列的算法与技术,其中有一些值得我们关注和学习。

1. 多尺度超像素图像分割多尺度超像素图像分割是以不同尺度下超像素信息进行图像分割。

它能够较好地处理图像的不同尺度和多重结构,提高图像分割的准确度和速率。

在实际应用中,多尺度超像素图像分割可以优化图像的分割过程,增强对小结构的处理效果。

2. 非均匀超像素图像分割非均匀超像素图像分割是将不同的像素分为不同的超像素。

这种方法能够进一步提高超像素图像分割的效果,因为它可以根据图像中不同区域的特性来选择不同的超像素大小和形状。

非均匀超像素图像分割可以提高超像素的质量和准确度,从而提高图像分割的效果。

3. 超像素合并技术超像素合并技术是将图像中不同的超像素再次合并,使得生成的超像素数量较少,从而提取图像特征和处理图像时所需的时间也会减少。

slic超像素分割原理

slic超像素分割原理

slic超像素分割原理宝子们!今天咱们来唠唠这个超有趣的SLIC超像素分割原理。

咱先得知道啥是超像素呢?简单说呀,超像素就是把图像里那些看起来比较相近的小像素点给它抱团儿,形成一个个小的区域,就像把一群小伙伴按照某些相似的特点分成一个个小团体一样。

这超像素分割可是图像处理里特别厉害的一招哦。

那SLIC这个超像素分割方法呢,它的想法特别妙。

它是从图像里的像素点出发的。

想象一下,图像里的每个像素点就像一个小居民,住在一个大大的图像城市里。

SLIC呢,就像是一个特别聪明的城市规划师。

SLIC开始工作的时候,它会先在这个图像城市里均匀地撒下一些种子点。

这些种子点就像是每个小团体的核心人物。

比如说,在一幅风景图像里,可能就有种子点落在天空部分、草地部分、大树部分等等。

这些种子点可不是随便撒的哦,它们是按照一定的间距均匀分布的。

就好像是在城市里按照一定的距离规划出一个个小社区中心一样。

撒下种子点之后呢,每个种子点就开始招募自己的成员啦。

它怎么招募呢?它就看周围的像素点哪个和自己比较像。

那怎么判断像不像呢?这里面就有学问了。

它会看像素点的颜色呀,空间位置这些信息。

比如说,天空中的种子点周围那些蓝色的、离它比较近的像素点就很可能被它招募进来。

这就好比社区中心会把周围房子颜色差不多、距离近的居民吸纳到自己的社区里。

而且哦,这个招募过程还有个限制范围,不能跑太远去招人。

这就保证了每个超像素小团体不会太大太乱。

每个超像素小团体在这个图像城市里都有自己的小地盘,不会随便侵犯别人的领地。

在这个过程中,每个像素点也只能被一个种子点招募,就像一个居民只能属于一个社区一样。

这样呢,慢慢地,整个图像就被这些超像素小团体给瓜分完了。

这个SLIC超像素分割还有个好处呢。

它形成的超像素呀,形状比较规整。

不像有些其他的分割方法,搞出来的超像素形状歪七扭八的。

这就好比是规划出来的社区都是方方正正的,看着就很整齐舒服。

宝子们,你想啊,通过SLIC超像素分割,原本复杂的图像一下子就变得有组织有纪律了。

超像素分割公式

超像素分割公式

超像素分割公式Superpixel segmentation is an essential task in computer vision that aims to group pixels into perceptually meaningful regions. This process helps to reduce the complexity of an image by grouping pixels with similar properties together. One popular technique for superpixel segmentation is the watershed algorithm, which partitions an image into segments based on the gradients of intensity values. However, the watershed algorithm tends to oversegment an image, leading to an excessive number of superpixels.超像素分割是计算机视觉中的一个重要任务,旨在将像素分组成具有感知意义的区域。

这个过程通过将具有相似属性的像素分组在一起,有助于降低图像的复杂性。

超像素分割的一种流行技术是分水岭算法,它基于强度值的梯度将图像划分为段。

然而,分水岭算法往往对图像进行过分割,从而导致超像素数量过多。

Another popular method for superpixel segmentation is the simple linear iterative clustering (SLIC) algorithm. SLIC is a faster alternative to the watershed algorithm and produces more regular superpixels. By taking into account both spatial proximity and color similarity,SLIC segments an image into a compact set of superpixels. This algorithm has gained popularity in the computer vision community due to its efficiency and effectiveness in generating high-quality superpixels.超像素分割的另一种流行方法是简单线性迭代聚类(SLIC)算法。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Resource.h//{{NO_DEPENDENCIES}}// Microsoft Visual C++ generated include file.// Used by SLICSuperpixels.rc//#define IDD_SLICSUPERPIXELS_DIALOG 102#define IDR_MAINFRAME 128#define IDC_BUTTON_CREATESUPERPIXELS 1000#define IDC_EDIT_SPCOUNT 1001#define IDC_EDIT2 1002#define IDC_EDIT_COMPACTNESS 1002// Next default values for new objects//#ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_VALUE 129#define _APS_NEXT_COMMAND_VALUE 32771#define _APS_NEXT_CONTROL_VALUE 1003#define _APS_NEXT_SYMED_VALUE 101#endif#endifStdafx.h// stdafx.h : include file for standard system include files,// or project specific include files that are used frequently,// but are changed infrequently#pragma once#ifndef _SECURE_ATL#define _SECURE_ATL 1#endif#ifndef VC_EXTRALEAN#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #endif#include"targetver.h"#define_ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit// turns off MFC's hiding of some common and often safely ignored warning messages#define _AFX_ALL_WARNINGS#include<afxwin.h>// MFC core and standard components#include<afxext.h>// MFC extensions#ifndef _AFX_NO_OLE_SUPPORT#include<afxdtctl.h>// MFC support for Internet Explorer 4 Common Controls#endif#ifndef _AFX_NO_AFXCMN_SUPPORT#include<afxcmn.h>// MFC support for Windows Common Controls#endif// _AFX_NO_AFXCMN_SUPPORT#include<afxcontrolbars.h>// MFC support for ribbons and control barsSLIC.h// SLIC.h: interface for the SLIC class.//===========================================================================// This code implements the superpixel method described in://// Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua, and Sabine Susstrunk,// "SLIC Superpixels",// EPFL Technical Report no. 149300, June 2010.//===========================================================================// Copyright (c) 2012 Radhakrishna Achanta [EPFL]. All rights reserved.//===========================================================================//////////////////////////////////////////////////////////////////////#if !defined(_SLIC_H_INCLUDED_)#define _SLIC_H_INCLUDED_#include<vector>#include<string>#include<algorithm>using namespace std;class SLIC{public:SLIC();virtual ~SLIC();//============================================================================// Superpixel segmentation for a given step size (superpixel size ~= step*step)//============================================================================ void DoSuperpixelSegmentation_ForGivenSuperpixelSize(const unsigned int* ubuff,//Each 32 bit unsigned int contains ARGB pixel values.const int width,const int height,int*& klabels,int& numlabels,const int& superpixelsize,const double& compactness);//============================================================================// Superpixel segmentation for a given number of superpixels//============================================================================ void DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(const unsigned int* ubuff,const int width,const int height,int*& klabels,int& numlabels,const int& K,//required number of superpixelsconst double& compactness);//10-20 is a good value for CIELAB space//============================================================================// Supervoxel segmentation for a given step size (supervoxel size ~= step*step*step) //============================================================================void DoSupervoxelSegmentation(unsigned int**& ubuffvec,const int& width,const int& height,const int& depth,int**& klabels,int& numlabels,const int& supervoxelsize,const double& compactness);//============================================================================ // Save superpixel labels in a text file in raster scan order//============================================================================ void SaveSuperpixelLabels(const int*& labels,const int& width,const int& height,const string& filename,const string& path);//============================================================================ // Save supervoxel labels in a text file in raster scan, depth order//============================================================================ void SaveSupervoxelLabels(const int**& labels,const int& width,const int& height,const int& depth,const string& filename,const string& path);//============================================================================ // Function to draw boundaries around superpixels of a given 'color'.// Can also be used to draw boundaries around supervoxels, i.e layer by layer.//============================================================================ void DrawContoursAroundSegments(unsigned int*& segmentedImage,int*& labels,const int& width,const int& height,const unsigned int& color );private://============================================================================ // The main SLIC algorithm for generating superpixels//============================================================================void PerformSuperpixelSLIC(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,int*& klabels,const int& STEP,const vector<double>& edgemag,const double& m = 10.0);//============================================================================ // The main SLIC algorithm for generating supervoxels//============================================================================ void PerformSupervoxelSLIC(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,vector<double>& kseedsz,int**& klabels,const int& STEP,const double& compactness);//============================================================================ // Pick seeds for superpixels when step size of superpixels is given.//============================================================================ void GetLABXYSeeds_ForGivenStepSize(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,const int& STEP,const bool& perturbseeds,const vector<double>& edgemag);//============================================================================ // Pick seeds for supervoxels//============================================================================ void GetKValues_LABXYZ(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,vector<double>& kseedsz,const int& STEP);//============================================================================ // Move the superpixel seeds to low gradient positions to avoid putting seeds // at region boundaries.//============================================================================ void PerturbSeeds(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,const vector<double>& edges);//============================================================================ // Detect color edges, to help PerturbSeeds()//============================================================================ void DetectLabEdges(const double* lvec,const double* avec,const double* bvec,const int& width,const int& height,vector<double>& edges);//============================================================================ // sRGB to XYZ conversion; helper for RGB2LAB()//============================================================================ void RGB2XYZ(const int& sR,const int& sG,const int& sB,double& X,double& Y,double& Z);//============================================================================ // sRGB to CIELAB conversion (uses RGB2XYZ function)//============================================================================ void RGB2LAB(const int& sR,const int& sB,double& lval,double& aval,double& bval);//============================================================================// sRGB to CIELAB conversion for 2-D images//============================================================================void DoRGBtoLABConversion(const unsigned int*& ubuff,double*& lvec,double*& avec,double*& bvec);//============================================================================// sRGB to CIELAB conversion for 3-D volumes//============================================================================void DoRGBtoLABConversion(unsigned int**& ubuff,double**& lvec,double**& avec,double**& bvec);//============================================================================// Post-processing of SLIC segmentation, to avoid stray labels.//============================================================================void EnforceLabelConnectivity(const int* labels,const int width,const int height,int*& nlabels,//input labels that need to be corrected to remove stray labelsint& numlabels,//the number of labels changes in the end if segments are removedconst int& K); //the number of superpixels desired by the user //============================================================================// Post-processing of SLIC supervoxel segmentation, to avoid stray labels.//============================================================================void EnforceSupervoxelLabelConnectivity(int**& labels,//input - previous labels, output - new labelsconst int& width,const int& height,const int& depth,const int& STEP);private:int m_width;int m_height;int m_depth;double* m_lvec;double* m_avec;double* m_bvec;double** m_lvecvec;double** m_avecvec;double** m_bvecvec;};#endif// !defined(_SLIC_H_INCLUDED_)SLICSuperpixels.h// SLICSuperpixels.h : main header file for the PROJECT_NAME application //#pragma once#ifndef __AFXWIN_H__#error "include 'stdafx.h' before including this file for PCH"#endif#include"resource.h"// main symbols// CSLICSuperpixelsApp:// See SLICSuperpixels.cpp for the implementation of this class//class CSLICSuperpixelsApp : public CWinAppEx{public:CSLICSuperpixelsApp();// Overridespublic:virtual BOOL InitInstance();// ImplementationDECLARE_MESSAGE_MAP()};extern CSLICSuperpixelsApp theApp;SLICSuperpixelsDlg.h// SLICSuperpixelsDlg.h : header file//#include<vector>#include<string>using namespace std;#pragma once// CSLICSuperpixelsDlg dialogclass CSLICSuperpixelsDlg : public CDialog{// Constructionpublic:CSLICSuperpixelsDlg(CWnd* pParent = NULL); // standard constructor// Dialog Dataenum { IDD = IDD_SLICSUPERPIXELS_DIALOG };protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementationprotected:HICON m_hIcon;// Generated message map functionsvirtual BOOL OnInitDialog();afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()public:afx_msg void OnBnClickedButtonCreatesuperpixels();private:bool BrowseForFolder(string& folderpath);void GetPictures(vector<string>& picvec);void DoSupervoxelVideoSegmentation();public:afx_msg void OnEnChangeEditSpcount();afx_msg void OnEnChangeEditCompactness();int m_spcount;double m_compactness;};SLIC.cpp// SLIC.cpp: implementation of the SLIC class.//// Copyright (C) Radhakrishna Achanta 2012// All rights reserved//Email:**************************////////////////////////////////////////////////////////////////////// #include"stdafx.h"#include<cfloat>#include<cmath>#include<iostream>#include<fstream>#include"SLIC.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////SLIC::SLIC(){m_lvec = NULL;m_avec = NULL;m_bvec = NULL;m_lvecvec = NULL;m_avecvec = NULL;m_bvecvec = NULL;}SLIC::~SLIC(){if(m_lvec) delete [] m_lvec;if(m_avec) delete [] m_avec;if(m_bvec) delete [] m_bvec;if(m_lvecvec){for( int d = 0; d < m_depth; d++ ) delete [] m_lvecvec[d];delete [] m_lvecvec;}if(m_avecvec){for( int d = 0; d < m_depth; d++ ) delete [] m_avecvec[d];delete [] m_avecvec;}if(m_bvecvec){for( int d = 0; d < m_depth; d++ ) delete [] m_bvecvec[d];delete [] m_bvecvec;}}//============================================================================== /// RGB2XYZ////// sRGB (D65 illuninant assumption) to XYZ conversion//==============================================================================void SLIC::RGB2XYZ(const int& sR,const int& sG,const int& sB,double& X,double& Y,double& Z){double R = sR/255.0;double G = sG/255.0;double B = sB/255.0;double r, g, b;if(R <= 0.04045) r = R/12.92;else r = pow((R+0.055)/1.055,2.4);if(G <= 0.04045) g = G/12.92;else g = pow((G+0.055)/1.055,2.4);if(B <= 0.04045) b = B/12.92;else b = pow((B+0.055)/1.055,2.4);X = r*0.4124564 + g*0.3575761 + b*0.1804375;Y = r*0.2126729 + g*0.7151522 + b*0.0721750;Z = r*0.0193339 + g*0.1191920 + b*0.9503041;}//===========================================================================/// RGB2LAB//===========================================================================void SLIC::RGB2LAB(const int& sR, const int& sG, const int& sB, double& lval, double& aval, double& bval){//------------------------// sRGB to XYZ conversion//------------------------double X, Y, Z;RGB2XYZ(sR, sG, sB, X, Y, Z);//------------------------// XYZ to LAB conversion//------------------------double epsilon = 0.008856; //actual CIE standarddouble kappa = 903.3; //actual CIE standarddouble Xr = 0.950456; //reference whitedouble Yr = 1.0; //reference whitedouble Zr = 1.088754; //reference whitedouble xr = X/Xr;double yr = Y/Yr;double zr = Z/Zr;double fx, fy, fz;if(xr > epsilon) fx = pow(xr, 1.0/3.0);else fx = (kappa*xr + 16.0)/116.0;if(yr > epsilon) fy = pow(yr, 1.0/3.0);else fy = (kappa*yr + 16.0)/116.0;if(zr > epsilon) fz = pow(zr, 1.0/3.0);else fz = (kappa*zr + 16.0)/116.0;lval = 116.0*fy-16.0;aval = 500.0*(fx-fy);bval = 200.0*(fy-fz);}//=========================================================================== /// DoRGBtoLABConversion////// For whole image: overlaoded floating point version//=========================================================================== void SLIC::DoRGBtoLABConversion(const unsigned int*& ubuff,double*& lvec,double*& avec,double*& bvec){int sz = m_width*m_height;lvec = new double[sz];avec = new double[sz];bvec = new double[sz];for( int j = 0; j < sz; j++ ){int r = (ubuff[j] >> 16) & 0xFF;int g = (ubuff[j] >> 8) & 0xFF;int b = (ubuff[j] ) & 0xFF;RGB2LAB( r, g, b, lvec[j], avec[j], bvec[j] );}}//===========================================================================/// DoRGBtoLABConversion////// For whole volume//===========================================================================void SLIC::DoRGBtoLABConversion(unsigned int**& ubuff,double**& lvec,double**& avec,double**& bvec){int sz = m_width*m_height;for( int d = 0; d < m_depth; d++ ){for( int j = 0; j < sz; j++ ){int r = (ubuff[d][j] >> 16) & 0xFF;int g = (ubuff[d][j] >> 8) & 0xFF;int b = (ubuff[d][j] ) & 0xFF;RGB2LAB( r, g, b, lvec[d][j], avec[d][j], bvec[d][j] );}}}//================================================================================= /// DrawContoursAroundSegments////// Internal contour drawing option exists. One only needs to comment the if/// statement inside the loop that looks at neighbourhood.//=================================================================================void SLIC::DrawContoursAroundSegments(unsigned int*& ubuff,int*& labels,const int& width,const int& height,const unsigned int& color ){const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};/* int sz = width*height;vector<bool> istaken(sz, false);int mainindex(0);for( int j = 0; j < height; j++ ){for( int k = 0; k < width; k++ ){int np(0);for( int i = 0; i < 8; i++ ){int x = k + dx8[i];int y = j + dy8[i];if( (x >= 0 && x < width) && (y >= 0 && y < height) ){int index = y*width + x;if( false == istaken[index] )//comment this to obtain internal contours{if( labels[mainindex] != labels[index] ) np++;}}}if( np > 1 )//change to 2 or 3 for thinner lines{ubuff[mainindex] = color;istaken[mainindex] = true;}mainindex++;}}*/int sz = width*height;vector<bool> istaken(sz, false);vector<int> contourx(sz);vector<int> contoury(sz);int mainindex(0);int cind(0);for( int j = 0; j < height; j++ ){for( int k = 0; k < width; k++ ){int np(0);for( int i = 0; i < 8; i++ ){int x = k + dx8[i];int y = j + dy8[i];if( (x >= 0 && x < width) && (y >= 0 && y < height) ){int index = y*width + x;//if( false == istaken[index] )//comment this to obtain internal contours{if( labels[mainindex] != labels[index] ) np++;}}}if( np > 1 ){contourx[cind] = k;contoury[cind] = j;istaken[mainindex] = true;//img[mainindex] = color;cind++;}mainindex++;}}int numboundpix = cind;//int(contourx.size());for( int j = 0; j < numboundpix; j++ ){int ii = contoury[j]*width + contourx[j];ubuff[ii] = 0xffffff;for( int n = 0; n < 8; n++ ){int x = contourx[j] + dx8[n];int y = contoury[j] + dy8[n];if( (x >= 0 && x < width) && (y >= 0 && y < height) ){int ind = y*width + x;if(!istaken[ind]) ubuff[ind] = 0;}}}}//============================================================================== /// DetectLabEdges//============================================================================== void SLIC::DetectLabEdges(const double* lvec,const double* avec,const double* bvec,const int& width,const int& height,vector<double>& edges){int sz = width*height;edges.resize(sz,0);for( int j = 1; j < height-1; j++ ){for( int k = 1; k < width-1; k++ ){int i = j*width+k;double dx = (lvec[i-1]-lvec[i+1])*(lvec[i-1]-lvec[i+1]) +(avec[i-1]-avec[i+1])*(avec[i-1]-avec[i+1]) +(bvec[i-1]-bvec[i+1])*(bvec[i-1]-bvec[i+1]);double dy = (lvec[i-width]-lvec[i+width])*(lvec[i-width]-lvec[i+width]) +(avec[i-width]-avec[i+width])*(avec[i-width]-avec[i+width]) +(bvec[i-width]-bvec[i+width])*(bvec[i-width]-bvec[i+width]);//edges[i] = fabs(dx) + fabs(dy);edges[i] = dx*dx + dy*dy;}}}//===========================================================================/// PerturbSeeds//===========================================================================void SLIC::PerturbSeeds(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,const vector<double>& edges){const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};int numseeds = kseedsl.size();for( int n = 0; n < numseeds; n++ ){int ox = kseedsx[n];//original xint oy = kseedsy[n];//original yint oind = oy*m_width + ox;int storeind = oind;for( int i = 0; i < 8; i++ ){int nx = ox+dx8[i];//new xint ny = oy+dy8[i];//new yif( nx >= 0 && nx < m_width && ny >= 0 && ny < m_height){int nind = ny*m_width + nx;if( edges[nind] < edges[storeind]){storeind = nind;}}}if(storeind != oind){kseedsx[n] = storeind%m_width;kseedsy[n] = storeind/m_width;kseedsl[n] = m_lvec[storeind];kseedsa[n] = m_avec[storeind];kseedsb[n] = m_bvec[storeind];}}}//=========================================================================== /// GetLABXYSeeds_ForGivenStepSize////// The k seed values are taken as uniform spatial pixel samples.//=========================================================================== void SLIC::GetLABXYSeeds_ForGivenStepSize(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,const int& STEP,const bool& perturbseeds,const vector<double>& edgemag){const bool hexgrid = false;int numseeds(0);int n(0);//int xstrips = m_width/STEP;//int ystrips = m_height/STEP;int xstrips = (0.5+double(m_width)/double(STEP));int ystrips = (0.5+double(m_height)/double(STEP));int xerr = m_width - STEP*xstrips;if(xerr < 0){xstrips--;xerr = m_width - STEP*xstrips;} int yerr = m_height - STEP*ystrips;if(yerr < 0){ystrips--;yerr = m_height- STEP*ystrips;}double xerrperstrip = double(xerr)/double(xstrips);double yerrperstrip = double(yerr)/double(ystrips);int xoff = STEP/2;int yoff = STEP/2;//-------------------------numseeds = xstrips*ystrips;//-------------------------kseedsl.resize(numseeds);kseedsa.resize(numseeds);kseedsb.resize(numseeds);kseedsx.resize(numseeds);kseedsy.resize(numseeds);for( int y = 0; y < ystrips; y++ ){int ye = y*yerrperstrip;for( int x = 0; x < xstrips; x++ ){int xe = x*xerrperstrip;int seedx = (x*STEP+xoff+xe);if(hexgrid){ seedx = x*STEP+(xoff<<(y&0x1))+xe; seedx =min(m_width-1,seedx); }//for hex grid samplingint seedy = (y*STEP+yoff+ye);int i = seedy*m_width + seedx;kseedsl[n] = m_lvec[i];kseedsa[n] = m_avec[i];kseedsb[n] = m_bvec[i];kseedsx[n] = seedx;kseedsy[n] = seedy;n++;}}if(perturbseeds){PerturbSeeds(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, edgemag);}}//===========================================================================/// GetKValues_LABXYZ////// The k seed values are taken as uniform spatial pixel samples.//===========================================================================void SLIC::GetKValues_LABXYZ(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,vector<double>& kseedsz,const int& STEP){const bool hexgrid = false;int numseeds(0);int n(0);int xstrips = (0.5+double(m_width)/double(STEP));int ystrips = (0.5+double(m_height)/double(STEP));int zstrips = (0.5+double(m_depth)/double(STEP));int xerr = m_width - STEP*xstrips;if(xerr < 0){xstrips--;xerr = m_width - STEP*xstrips;} int yerr = m_height - STEP*ystrips;if(yerr < 0){ystrips--;yerr = m_height- STEP*ystrips;} int zerr = m_depth - STEP*zstrips;if(zerr < 0){zstrips--;zerr = m_depth - STEP*zstrips;}double xerrperstrip = double(xerr)/double(xstrips);double yerrperstrip = double(yerr)/double(ystrips);double zerrperstrip = double(zerr)/double(zstrips);int xoff = STEP/2;int yoff = STEP/2;int zoff = STEP/2;//-------------------------numseeds = xstrips*ystrips*zstrips;//-------------------------kseedsl.resize(numseeds);kseedsa.resize(numseeds);kseedsb.resize(numseeds);kseedsx.resize(numseeds);kseedsy.resize(numseeds);kseedsz.resize(numseeds);for( int z = 0; z < zstrips; z++ ){int ze = z*zerrperstrip;int d = (z*STEP+zoff+ze);for( int y = 0; y < ystrips; y++ ){int ye = y*yerrperstrip;for( int x = 0; x < xstrips; x++ ){int xe = x*xerrperstrip;int i = (y*STEP+yoff+ye)*m_width + (x*STEP+xoff+xe);kseedsl[n] = m_lvecvec[d][i];kseedsa[n] = m_avecvec[d][i];kseedsb[n] = m_bvecvec[d][i];kseedsx[n] = (x*STEP+xoff+xe);kseedsy[n] = (y*STEP+yoff+ye);kseedsz[n] = d;n++;}}}}//=========================================================================== /// PerformSuperpixelSLIC////// Performs k mean segmentation. It is fast because it looks locally, not/// over the entire image.//=========================================================================== void SLIC::PerformSuperpixelSLIC(vector<double>& kseedsl,vector<double>& kseedsa,vector<double>& kseedsb,vector<double>& kseedsx,vector<double>& kseedsy,int*& klabels,const int& STEP,const vector<double>& edgemag,const double& M){int sz = m_width*m_height;const int numk = kseedsl.size();//----------------int offset = STEP;//if(STEP < 8) offset = STEP*1.5;//to prevent a crash due to a very small step size //----------------vector<double> clustersize(numk, 0);vector<double> inv(numk, 0);//to store 1/clustersize[k] valuesvector<double> sigmal(numk, 0);vector<double> sigmaa(numk, 0);vector<double> sigmab(numk, 0);vector<double> sigmax(numk, 0);vector<double> sigmay(numk, 0);vector<double> distvec(sz, DBL_MAX);double invwt = 1.0/((STEP/M)*(STEP/M));int x1, y1, x2, y2;double l, a, b;double dist;double distxy;for( int itr = 0; itr < 10; itr++ ){distvec.assign(sz, DBL_MAX);for( int n = 0; n < numk; n++ ){y1 = max(0.0, kseedsy[n]-offset);y2 = min((double)m_height, kseedsy[n]+offset);x1 = max(0.0, kseedsx[n]-offset);。

相关文档
最新文档