最近在做一些图片补全的工作。本以为补全就只有photoshop的patchmatch这一种方法,但后来发现不是这样。有一款号称linux下的ps的软件,名字叫做gimp。他也提供类似功能。细细一查,这款软件确实很有来头,连gtk在刚出现时都是专门为此软件写界面的。当然,现在gtk发展起来了。
这款软件很多功能都是靠插件在工作,他只提供一个通讯用的接口。靠着各种开源的插件,他实现了众多功能,好不好不敢说,不过和ps媲美是可以的。
好了,言归正传,resynthesizer就是一款用于补全的插件。网址在http://www.logarithmic.net/pfh/resynthesizer。大家可以去看看效果。作者也提供源代码,不过是插件的源代码。所以我修改了一下,使之能直接用在程序里。需要用opencv。
但遗憾的是,这个程序我并没有深入了解,不懂算法,只是会用而已。不想patchmatch那样,从算法到程序都搞懂了。希望有人能讲解一下其中的算法,不胜感激!
程序:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <time.h>
using namespace cv;
using namespace std;
//考虑3种情况,back, search, hole
//back是不理会的区域
//search是想要用之填补的区域
//hole是要补的区域
#define back 0
#define search 100
#define hole 255
//本程序的目的是修改gimp插件
//证实gimp的像素索引从(0,0)开始
//证实gimp的像素分布是rgb
typedef unsigned char Pixelel;
const int max_neighbours = 1000;
const int max_trys_per_pixel = 10000;
/***********************/
/***********************/
/*** x,y Coordinates ***/
/***********************/
/***********************/
struct Coordinates {
int x, y;
Coordinates(int _x, int _y) :
x(_x), y(_y) {
}
Coordinates() {
}
bool operator <(const Coordinates other) const {
return y * y + x * x < (other.y * other.y + other.x * other.x);
}
Coordinates operator +(const Coordinates a) const {
return Coordinates(x + a.x, y + a.y);
}
Coordinates operator -(const Coordinates a) const {
return Coordinates(x - a.x, y - a.y);
}
};
/**************/
/**************/
/*** Bitmap ***/
/**************/
/**************/
//Bitmap class with three dimensions (width, height, number of channels)
template<class t>
struct Bitmap {
int width, height, depth;
t *data;
Bitmap() {
data = 0;
}
~Bitmap() {
delete data;
}
void size(int w, int h, int d) {
width = w;
height = h;
depth = d;
delete data;
data = new t[w * h * d];
}
t *at(int x, int y) const {
return data + (y * width + x) * depth;
}
t *at(const Coordinates position) const {
return at(position.x, position.y)