资源简介
用于图像分割的grabcut, 而且非opencv版本,是c++源码,并有max-flow源码,可以用于其它图求解。
程序支持mask和矩形框两种输入,并附有样图和结果图。详细原理请参考文献:"GrabCut" - Interactive Foreground Extraction using Iterated Graph Cuts
代码片段和文件信息
/*
* GrabCut implementation source code Copyright(c) 2005-2006 Justin Talbot
*
* All Rights Reserved.
* For educational use only; commercial use expressly forbidden.
* NO WARRANTY express or implied for this software.
*/
#include “Color.h“
Real distance2(const Color &c1 const Color &c2)
{
return ((c1.r - c2.r) * (c1.r - c2.r) +
(c1.g - c2.g) * (c1.g - c2.g) +
(c1.b - c2.b) * (c1.b - c2.b));
}
//-------------------------------------------------------------------------
void ConvertImage(Image *image unsigned char *ptrRGB)
{
int width height i j;
Color c;
width = image->width();
height = image->height();
for ( j = 0; j < height; j++)
{
for ( i = 0; i < width; i++)
{
Real r g b;
r = (Real)(*ptrRGB++)/255;
g = (Real)(*ptrRGB++)/255;
b = (Real)(*ptrRGB++)/255;
(*image)(i j) = Color(r g b);
}
}
}
void SavePPM(char* file_name Image &image)
{
Color Imagedata;
int width height i j;
unsigned char r g b;
FILE *fp = fopen(file_name “wb“);
if(!fp)
return;
width = image.width();
height = image.height();
fprintf(fp “P6\n“);
fprintf(fp “%d %d\n“ width height);
fprintf(fp “255\n“);
for ( j = 0; j < height; j++)
{
for ( i = 0; i < width; i++)
{
Imagedata = image(i j);
r = (unsigned char)(Imagedata.r * 255.0);
g = (unsigned char)(Imagedata.g * 255.0);
b = (unsigned char)(Imagedata.b * 255.0);
fwrite(&r 1 1 fp);
fwrite(&g 1 1 fp);
fwrite(&b 1 1 fp);
}
}
fclose(fp);
}
void SaveMaskAndImage(char* file_name Image *image Image maskimage)
{
Color Imagedata;
Real maskdata;
Real *data;
int width height i j;
unsigned char r g b;
FILE *fp = fopen(file_name “wb“);
if(!fp)
return;
width = (*image).width();
height = (*image).height();
fprintf(fp “P6\n“);
fprintf(fp “%d %d\n“ width height);
fprintf(fp “255\n“);
// data = maskimage.ptr();
for ( j = 0; j < height; j++)
{
for ( i = 0; i < width; i++)
{
Imagedata = (*image)(i j);
maskdata = maskimage(i j);
if( maskdata == 0)
{
r = (unsigned char)(Imagedata.r * 255.0);
g = (unsigned char)(Imagedata.g * 255.0);
b = (unsigned char)(Imagedata.b * 255.0);
}
else
{
(*image)(i j) = Color(1 11);
}
}
}
fclose(fp);
}
void CropMaskAndImage(char* file_name Image image Image maskimage)
{
Color Imagedata;
Real maskdata;
Real *data;
int width height i j;
unsigned char r g b;
width = image.width();
height = image.height();
// data = maskimage.ptr();
for ( j = 0; j < height; j++)
{
for ( i = 0; i < width; i++)
{
Imagedata = image(i j);
maskdata = maskimage(i j);
if( maskdata == 0)
{
r = (unsigned char)(Imagedata.r * 255.0);
g = (unsigned char)(Imagedata.g * 255.0);
b = (un
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 234300 2015-08-05 18:33 grabcut\0.jpg
文件 27075 2015-02-25 14:16 grabcut\1.jpg
....... 3753 2015-07-06 20:47 grabcut\grabcut\Color.cpp
....... 900 2015-07-06 20:47 grabcut\grabcut\Color.h
....... 1453 2015-07-06 20:47 grabcut\grabcut\Global.h
....... 21837 2015-07-06 20:47 grabcut\grabcut\GMM.cpp
....... 3474 2015-07-06 20:47 grabcut\grabcut\GMM.h
....... 23600 2015-07-06 20:47 grabcut\grabcut\GrabCut.cpp
....... 3442 2015-07-06 20:47 grabcut\grabcut\GrabCut.h
....... 64000 2015-07-06 20:47 grabcut\grabcut\GrabCut.opt
....... 1429 2015-07-06 20:47 grabcut\grabcut\GrabCut.plg
....... 8107 2015-07-06 20:47 grabcut\grabcut\GrabCut1.vcproj
文件 18410 2015-08-05 18:47 grabcut\grabcut\GrabCutImage.cpp
....... 705 2015-07-06 20:47 grabcut\grabcut\GrabCutImage.h
....... 5779 2015-07-06 20:47 grabcut\grabcut\Image.cpp
....... 3724 2015-07-06 20:47 grabcut\grabcut\Image.h
....... 10355 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\block.h
....... 66560 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\graph.bsc
....... 217206 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\graph.exe
....... 213492 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\graph.ilk
....... 14727 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\graph.obj
....... 256480 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\graph.pch
....... 492544 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\graph.pdb
....... 0 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\graph.sbr
....... 19261 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\maxflow.obj
....... 0 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\maxflow.sbr
....... 6423 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\tGraph.obj
....... 0 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\tGraph.sbr
....... 41984 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\vc60.idb
....... 53248 2015-07-06 20:47 grabcut\grabcut\maxflow-v2.1.src\adjacency_list\Debug\vc60.pdb
............此处省略45个文件信息
- 上一篇:VC++ CALLBACK
- 下一篇:循环码编码器
评论
共有 条评论