Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maktu6 committed Feb 8, 2019
0 parents commit 1ac7ff6
Show file tree
Hide file tree
Showing 31 changed files with 2,016 additions and 0 deletions.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
PROJECT(patchmatch_mat)

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

FIND_PACKAGE( OpenCV REQUIRED )
include_directories(source/include)
add_subdirectory (source)
set (EXTRA_LIBS ${EXTRA_LIBS} patchmatch)
# Declare the target (an executable)
#ADD_EXECUTABLE(patchmatch_mat inpaint_mat.cpp)
add_library (patchmatch_mat SHARED inpaint_mat)

TARGET_LINK_LIBRARIES(patchmatch_mat ${OpenCV_LIBS} ${EXTRA_LIBS})

#MESSAGE(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")



Empty file added README.md
Empty file.
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mkdir build
cd build
cmake ..
make
cd ..
python3 setup.py build_ext -i
320 changes: 320 additions & 0 deletions demo.ipynb

Large diffs are not rendered by default.

Binary file added image_files/forest/forest.bmp
Binary file not shown.
Binary file added image_files/forest/forest_pruned.bmp
Binary file not shown.
Binary file added image_files/forest/maskimage.bmp
Binary file not shown.
Binary file added image_files/women/997_34.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_files/women/997_34.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_files/women/997_6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_files/women/997_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions inpaint_mat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "inpaint_mat.hpp"
#include <iostream>

cv::Mat inpaintPatchMatch_mat(cv::Mat input_mat, cv::Mat mask_mat, int radius)
{
IplImage* output_img=NULL;
IplImage* input_img = cvCreateImage(cvSize(input_mat.cols, input_mat.rows), 8, 3);
IplImage ipltemp = input_mat;
cvCopy(&ipltemp, input_img);
IplImage* mask_img = cvCreateImage(cvSize(mask_mat.cols, mask_mat.rows), 8, 1);
ipltemp = mask_mat;
cvCopy(&ipltemp, mask_img);

output_img = inpaintPatchMatch(input_img, mask_img, radius);
cv::Mat output_mat = cv::Mat(output_img);

// cvReleaseImage(&input_img);
// cvReleaseImage(&mask_img);

return output_mat;
}

int main()
{
using namespace cv;
using namespace std;
Mat input, mask_rgb, mask, output;
input = imread("../image_files/forest/forest.bmp");
mask = imread("../image_files/forest/forest_pruned.bmp", 0);
// cvtColor(mask_rgb, mask, CV_BGR2GRAY);
cout << mask.type() << endl;
cout << mask.rows << endl;

output = inpaintPatchMatch_mat(input, mask, 2);

return 0;
}
6 changes: 6 additions & 0 deletions inpaint_mat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <opencv2/opencv.hpp>
extern "C" {
#include "patchmatch.h"
}

cv::Mat inpaintPatchMatch_mat(cv::Mat input_mat, cv::Mat mask_mat, int radius);
32 changes: 32 additions & 0 deletions patchmatch_inpaint.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cimport numpy as np
import numpy as np

# For cv::Mat usage
cdef extern from "core/core.hpp":
cdef int CV_WINDOW_AUTOSIZE
cdef int CV_8UC3
cdef int CV_8UC1

cdef extern from "core/core.hpp" namespace "cv":
cdef cppclass Mat:
Mat() except +
void create(int, int, int)
void* data
int rows
int cols
int channels()

# For Buffer usage
cdef extern from "Python.h":
ctypedef struct PyObject
object PyMemoryView_FromBuffer(Py_buffer *view)
int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags)
enum:
PyBUF_FULL_RO

cdef Mat np2Mat(np.ndarray ary)

cdef object Mat2np(Mat mat)

cdef extern from "inpaint_mat.hpp":
Mat inpaintPatchMatch_mat(Mat input_mat, Mat mask_mat, int radius)
92 changes: 92 additions & 0 deletions patchmatch_inpaint.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import numpy as np
cimport numpy as np # for np.ndarray
from libc.string cimport memcpy
from patchmatch_inpaint cimport *

# inspired and adapted from http://makerwannabe.blogspot.ch/2013/09/calling-opencv-functions-via-cython.html

cdef Mat np2Mat3D(np.ndarray ary):
assert ary.ndim==3 and ary.shape[2]==3, "ASSERT::3channel RGB only!!"
ary = np.dstack((ary[...,2], ary[...,1], ary[...,0])) #RGB -> BGR

cdef np.ndarray[np.uint8_t, ndim=3, mode ='c'] np_buff = np.ascontiguousarray(ary, dtype=np.uint8)
cdef unsigned int* im_buff = <unsigned int*> np_buff.data
cdef int r = ary.shape[0]
cdef int c = ary.shape[1]
cdef Mat m
m.create(r, c, CV_8UC3)
memcpy(m.data, im_buff, r*c*3)
return m

cdef Mat np2Mat2D(np.ndarray ary):
assert ary.ndim==2 , "ASSERT::1 channel grayscale only!!"

cdef np.ndarray[np.uint8_t, ndim=2, mode ='c'] np_buff = np.ascontiguousarray(ary, dtype=np.uint8)
cdef unsigned int* im_buff = <unsigned int*> np_buff.data
cdef int r = ary.shape[0]
cdef int c = ary.shape[1]
cdef Mat m
m.create(r, c, CV_8UC1)
memcpy(m.data, im_buff, r*c)
return m


cdef Mat np2Mat(np.ndarray ary):
if ary.ndim == 2:
return np2Mat2D(ary)
elif ary.ndim == 3:
return np2Mat3D(ary)


cdef object Mat2np(Mat m):
# Create buffer to transfer data from m.data
cdef Py_buffer buf_info
# Define the size / len of data
cdef size_t len = m.rows*m.cols*m.channels()*sizeof(CV_8UC3)
# Fill buffer
PyBuffer_FillInfo(&buf_info, NULL, m.data, len, 1, PyBUF_FULL_RO)
# Get Pyobject from buffer data
Pydata = PyMemoryView_FromBuffer(&buf_info)

# Create ndarray with data
shape_array = (m.rows, m.cols, m.channels())
ary = np.ndarray(shape=shape_array, buffer=Pydata, order='c', dtype=np.uint8)

# BGR -> RGB
ary = np.dstack((ary[...,2], ary[...,1], ary[...,0]))
# Convert to numpy array
pyarr = np.asarray(ary)
return pyarr


def np2Mat2np(nparray):
cdef Mat m

# Convert numpy array to cv::Mat
m = np2Mat(nparray)

# Convert cv::Mat to numpy array
pyarr = Mat2np(m)

return pyarr


cdef class PyMat:
cdef Mat mat

def __cinit__(self, np_mat):
self.mat = np2Mat(np_mat)

def get_mat(self):
return Mat2np(self.mat)

cpdef object PyPatchMatch_inpaint(np.ndarray input_img, np.ndarray mask_img, int radius):
cdef Mat input_mat
cdef Mat mask_mat
cdef Mat output_mat

input_mat = np2Mat(input_img)
mask_mat = np2Mat(mask_img)
output_mat = inpaintPatchMatch_mat(input_mat, mask_mat, radius)
output = Mat2np(output_mat)
return output
37 changes: 37 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import numpy
import sys
import os
import glob

# installation path of OpenCV
SYS_PREFIX = '/usr/local'
lib_folder = os.path.join(SYS_PREFIX, 'lib')

# Find opencv libraries in lib_folder
cvlibs = list()
for file in glob.glob(os.path.join(lib_folder, 'libopencv_*')):
cvlibs.append(file.split('.')[0])
cvlibs = list(set(cvlibs))
cvlibs = ['opencv_{}'.format(lib.split(os.path.sep)[-1].split('libopencv_')[-1]) for lib in cvlibs]
cvlibs += ['patchmatch']
# print('cvlibs: %s'%cvlibs)
# print('lib_folder: %s'%lib_folder)
setup(
cmdclass={'build_ext': build_ext},
ext_modules=cythonize(Extension("patchmatch_inpaint",
sources=["patchmatch_inpaint.pyx", "inpaint_mat.cpp"],
language="c++",
include_dirs=[numpy.get_include(),
os.path.join(SYS_PREFIX, 'include', 'opencv2'),
os.path.join(SYS_PREFIX, 'include', 'opencv'),
'source/include'
],
library_dirs=[lib_folder, 'build/source'],
libraries=cvlibs,
)
)
)
15 changes: 15 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

FIND_PACKAGE(OpenCV REQUIRED)
include_directories(include)
aux_source_directory(. DIR_LIB_SRCS)
add_compile_options(-fPIC)
add_library (patchmatch STATIC ${DIR_LIB_SRCS})
TARGET_LINK_LIBRARIES(patchmatch ${OpenCV_LIBS})
#MESSAGE(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")



21 changes: 21 additions & 0 deletions source/include/defineall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __GLOBAL_INCLUDE_PATCHMATCH_H__
#define __GLOBAL_INCLUDE_PATCHMATCH_H__

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <opencv2/core/core_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <time.h>
#include "structdef.h"
#include "inpaint.h"
#include "maskedimage.h"
#include "nearestneighborfield.h"
#include "qualitymesures.h"
#include "patchmatch.h"

double max1(double a, double b);
double min1(double a, double b);

#endif
18 changes: 18 additions & 0 deletions source/include/inpaint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __INPAINT_H_
#define __INPAINT_H_
#include "defineall.h"

Inpaint_P initInpaint();

void addEltInpaintingPyramid(Inpaint_P imp, MaskedImage_P elt);
MaskedImage_P ExpectationMaximization(Inpaint_P imp, int level);
void ExpectationStep(NNF_P nnf, int sourceToTarget, double*** vote, MaskedImage_P source, int upscale);
void weightedCopy(MaskedImage_P src, int xs, int ys, double*** vote, int xd, int yd, double w);
void MaximizationStep(MaskedImage_P target, double*** vote);
IplImage* inpaint(Inpaint_P imp, IplImage* input, int ** mask, int radius);
void freeInpaintingPyramid(Inpaint_P imp);

// Variables globales
extern double* G_globalSimilarity;
extern int G_initSim;
#endif
25 changes: 25 additions & 0 deletions source/include/maskedimage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __MASKED_IMAGE_H_
#define __MASKED_IMAGE_H_
#include "defineall.h"

void initSimilarity();
MaskedImage_P initMaskedImage(IplImage* image, int** mask);
MaskedImage_P initNewMaskedImage(int width, int height);
void freeMaskedImage(MaskedImage_P mIm);
int isMasked(MaskedImage_P mIm, int x, int y);
void setMask(MaskedImage_P mIm, int x, int y, int value);
int constainsMasked(MaskedImage_P mIm, int x, int y, int S);
int distanceMaskedImage(MaskedImage_P source,int xs,int ys, MaskedImage_P target,int xt,int yt, int S);
MaskedImage_P downsample(MaskedImage_P source);
MaskedImage_P upscale(MaskedImage_P source, int newW,int newH);
MaskedImage_P copyMaskedImage(MaskedImage_P mIm);

int getSampleMaskedImage(MaskedImage_P mIm, int x, int y, int band);////////////
void setSampleMaskedImage(MaskedImage_P mIm, int x, int y, int band, int value);/////////////


// Variables globales
extern double* G_globalSimilarity;
extern int G_initSim;

#endif
16 changes: 16 additions & 0 deletions source/include/nearestneighborfield.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __NNF_H_
#define __NNF_H_
#include "defineall.h"

NNF_P initNNF(MaskedImage_P input, MaskedImage_P output, int patchsize);
void allocNNFField(NNF_P nnf);
void freeNNFField(NNF_P nnf);
void freeNNF(NNF_P nnf);
void randomize(NNF_P nnf);
void initializeNNFFromOtherNNF(NNF_P nnf, NNF_P otherNnf);
void initializeNNF(NNF_P nnf);
void minimizeNNF(NNF_P nnf, int pass);
void minimizeLinkNNF(NNF_P nnf, int x, int y, int dir);
int distanceNNF(NNF_P nnf, int x, int y, int xp,int yp);

#endif
19 changes: 19 additions & 0 deletions source/include/patchmatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __INPAINT_H_
#define __INPAINT_H_

#ifdef __cplusplus
extern "C" {
#endif
#include "defineall.h"
#include "patchmatch.h"
#include "inpaint.h"
IplImage* inpaintPatchMatch(IplImage* input_img, IplImage* maskimage, int radius);
#ifdef __cplusplus
}
#endif


// Variables globales
extern double* G_globalSimilarity;
extern int G_initSim;
#endif
7 changes: 7 additions & 0 deletions source/include/qualitymesures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __quality_mesure_H_
#define __quality_mesure_H_
#include "defineall.h"

double PSNR(IplImage *original,IplImage *distorted);
double SSIM(IplImage *original,IplImage *distorted);
#endif
Loading

0 comments on commit 1ac7ff6

Please sign in to comment.