Grow Cut 区域生长图像分割Matlab演示

  • v9_413086
    了解作者
  • 2.8MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 13:00
    上传日期
通过Matlab演示Grow cut算法的每个步骤和算法说明文档
growcut.rar
  • growcut
  • growcut.m
    858B
  • hs_err_pid2088.log
    20.2KB
  • test1.m
    3.5KB
  • growcutmex.cpp
    4.5KB
  • test_data.mat
    1.7MB
  • test1.asv
    3.4KB
  • growcutmex.mexmaci
    12.6KB
  • GrowthCut.docx
    1.1MB
  • growcutmex.mexglx
    6.9KB
内容介绍
/******************************************************************* * GrowCut algorithm * from "GrowCut" - Interactive Multi-Label N-D Image Segmentation * By Cellular Autonoma * by Vladimir Vezhnevets and Vadim Konouchine * * coded by: Shawn Lankton (www.shawnlankton.com) * * usage: [labels, strengths] = growcutmex(image, labels) * image must be a double matrix (RGB or grayscale) * labels must be a double matrix with values: * -1 (background), 1 (foreground), or 0 (undefined) * * resulting labels will be either 0 (bg) or 1 (fg) * resulting strengths will be between 0 and 1 ******************************************************************/ #include <math.h> #include <matrix.h> #include <mex.h> /* Definitions to keep compatibility with earlier versions of ML */ #ifndef MWSIZE_MAX typedef int mwSize; typedef int mwIndex; typedef int mwSignedIndex; #if (defined(_LP64) || defined(_WIN64)) && !defined(MX_COMPAT_32) /* Currently 2^48 based on hardware limitations */ # define MWSIZE_MAX 281474976710655UL # define MWINDEX_MAX 281474976710655UL # define MWSINDEX_MAX 281474976710655L # define MWSINDEX_MIN -281474976710655L #else # define MWSIZE_MAX 2147483647UL # define MWINDEX_MAX 2147483647UL # define MWSINDEX_MAX 2147483647L # define MWSINDEX_MIN -2147483647L #endif #define MWSIZE_MIN 0UL #define MWINDEX_MIN 0UL #endif void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { //declare variables mxArray *strens_m, *labels_m, *labelsn_m, *I_m; mwSize numdims; const mwSize *dims; double *strens, *strensn, *labels, *labelsn, *I; int i,j,k,l,m; int Nx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; //8-neighbors int Ny[] = {0, 0, -1, 1, 1, -1, 1, -1}; int dimx,dimy,dimxy,colors; double C,g; int idxq, idxp, idxp1, idxp2, idxq1, idxq2; double maxC = 441.673; int converged; int MAX_ITS = 100; //uncomment below if you want to use this int its = 0; //gets image dimensions numdims = mxGetNumberOfDimensions(prhs[0]); dims = mxGetDimensions(prhs[0]); dimy = (int)dims[0]; dimx = (int)dims[1]; dimxy = dimx*dimy; if(numdims>2) colors = (int)dims[2]; else colors = 1; //sets up pointers I_m = mxDuplicateArray(prhs[0]); labels_m = mxDuplicateArray(prhs[1]); labelsn_m = plhs[0] = mxDuplicateArray(prhs[1]); strens_m = plhs[1] = mxCreateDoubleMatrix(dimy,dimx,mxREAL); I = mxGetPr(I_m); labels = mxGetPr(labels_m); labelsn = mxGetPr(labelsn_m); strens = mxGetPr(strens_m); //initilize seeds for(i=0;i<dimxy;i++) if(labels[i]!=0) strens[i] = 1;; //start main loop converged = 0; while(!converged){ its++; converged = 1; //unless we make a change //copy prev result for(i=0;i<dimxy;i++) labelsn[i] = labels[i]; //for every pixel p for(i=1;i<dimx-1;i++) { for(j=1;j<dimy-1;j++) { idxp = j+i*dimy; idxp1 = idxp+dimxy; idxp2 = idxp+dimxy*2; //for every neighbor q for(m=0;m<8;m++) { idxq = idxp + Nx[m]*dimy + Ny[m]; idxq1 = idxq+dimxy; idxq2 = idxq+dimxy*2; if(labels[idxq] == 0) continue; //wimps don't attack if(colors>1) C = sqrt( (I[idxp ] - I[idxq ])*(I[idxp ] - I[idxq ]) + (I[idxp1] - I[idxq1])*(I[idxp1] - I[idxq1]) + (I[idxp2] - I[idxq2])*(I[idxp2] - I[idxq2]) ); else C = sqrt((I[idxp ] - I[idxq ])*(I[idxp ] - I[idxq ])); g = 1-(C/maxC); //attack force if(g*strens[idxq]>strens[idxp]) //attack succeeds { strens[idxp] = g*strens[idxq]; labelsn[idxp] = labels[idxq]; converged = 0; // keep iterating } } } } //copy prev result for(i=0;i<dimxy;i++) labels[i] = labelsn[i];; //lets not go crazy... //if(its == MAX_ITS) break; } //condition final result for(i=0;i<dimxy;i++) if(labelsn[i]==-1) labelsn[i]=0;; //mexPrintf("total iterations=%d\n",its);mexw32 mxDestroyArray(I_m); mxDestroyArray(labels_m); return; }
评论
    相关推荐
    • 遗传算法Matlab程序
      应用数字群算法设计数字滤波器的matlab程序,遗传算法是最近几年流行的优化算法
    • A*算法Matlab 代码
      A*算法Matlab 代码,输入为自定义地图或图片,图片中默认绿色为起始地址,红色为终点,二值化化后进行路径规划。本文件中共有7张测试图,从20*20-640*480像素。
    • Q算法MATLAB仿真
      移动机器人路径规划MATLAB仿真,使用Q-Learning算法
    • 遗传算法MATLAB应用
      老师上课的参考书 主要是关于遗传算法的一些MATLAB应用,十分有意思 希望大家能喜欢
    • retinex算法MATLAB代码
      retinex算法是目前比较流行且重要的图像增强算法,主要是通过将影响图像质量的照度分量去除,得到图像本来面目的原理!利用了人类视觉系统的特性!
    • 遗传算法matlab
      matlab程序下的遗传算法
    • PSO算法matlab程序
      PSO算法的标准程序,程序使用matlab语言
    • 遗传算法matlab
      遗传算法是自然选择和群体学基础上的非数值优化方法。提供了些matlab实例计算
    • 遗传算法matlab
      matlab是科学计算常用的工具,遗传算法也是一种目前比较流行的一种算法,利用matlab写的遗传算法代码,供大家参考。
    • 遗传算法matlab代码
      遗传算法matlab代码,能够正常运行,对于刚开始接触遗传算法的人员有很好的帮助