shokai/scansnap_adjust_images_kindle — Issue #1: OpenCV 2.3.1
Author: sky-y
Created: 2011-12-03 01:50:50+00:00
State: open

HomebrewでOpenCV 2.3.1 をインストールするといろいろ変更点が出たので、修正版を貼っておきます。

kindlize_images.cpp
-------------------------------------------------------------------------------
#include "cv.h"
#include "highgui.h"
#include
#include
#include
#include
#include
using namespace boost;
using namespace std;
namespace fs = boost::filesystem;

IplImage *adjust_image(IplImage *img, program_options::variables_map argmap){
int cleft = argmap["cleft"].as();
int cright = argmap["cright"].as();
int ctop = argmap["ctop"].as();
int cbottom = argmap["cbottom"].as();

const int w = img->width - cleft - cright;
const int h = img->height - ctop - cbottom;

cvSetImageROI(img, cvRect(cleft, ctop, w, h));

IplImage *img_gray = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 1);
cvCvtColor(img, img_gray, CV_BGR2GRAY);

// 2値化
IplImage *img_bin = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 1);
cvThreshold(img_gray, img_bin, argmap["threshold"].as(), 255, CV_THRESH_BINARY);

// 文字を太らせる
cvErode(img_bin, img_bin, NULL, 1);

// リサイズ
int width = argmap["width"].as();
int height = argmap["height"].as();
IplImage *img_resized;
double scale = ((double)h)/w;
if(((double)height)/width < scale){ // 縦長
img_resized = cvCreateImage(cvSize((int)(height/scale), height), IPL_DEPTH_8U, 1);
}
else{ // 横長
img_resized = cvCreateImage(cvSize(width, (int)(scale*width)), IPL_DEPTH_8U, 1);
}
cvResize(img_bin, img_resized, CV_INTER_LINEAR);

cvReleaseImage(&img_bin);
cvReleaseImage(&img_gray);
return img_resized;
}

int main(int argc, char* argv[]) {
program_options::options_description opts("options");
opts.add_options()
("help", "ヘルプを表示")
("width,w", program_options::value(), "output width")
("height,h", program_options::value(), "output height")
("threshold,t", program_options::value(), "binarize threshold")
("input,i", program_options::value(), "input directory name")
("output,o", program_options::value(), "output directory name")
("cleft", program_options::value(), "crop left (pixel)")
("cright", program_options::value(), "crop right (pixel)")
("ctop", program_options::value(), "crop top (pixel)")
("cbottom", program_options::value(), "crop bottom (pixel)");
program_options::variables_map argmap;
program_options::store(parse_command_line(argc, argv, opts), argmap);
program_options::notify(argmap);
if (argmap.count("help") || !argmap.count("input") || !argmap.count("output") ||
!argmap.count("threshold")) {
cerr << "[input, output] required" << endl;
cerr << opts << endl;
return 1;
}

string in_dir = argmap["input"].as();
fs::path path = complete(fs::path(in_dir));
fs::directory_iterator end;
for (fs::directory_iterator i(path); i!=end; i++){
// string img_fullname = in_dir + i->path().string();
string img_fullname = in_dir + "/" + i->path().filename().string();
cout << img_fullname << endl;
IplImage *img, *img_result;
img = cvLoadImage(img_fullname.c_str());
if(!img){
cerr << "image file load error" << endl;
}
else{
img_result = adjust_image(img, argmap);
string out_filename = argmap["output"].as() + "/" + i->path().string();
cvSaveImage(out_filename.c_str(), img_result);
cvReleaseImage(&img);
cvReleaseImage(&img_result);
}
}
}

-------------------------------------------------------------------------------

Makefile.macosx
-------------------------------------------------------------------------------
# Mac用Makefile
SRC = kindlize_images.cpp
DST = kindlize_images

prefix=/usr/local
INCPATH=$(prefix)/include
LIBPATH=$(prefix)/lib

CV_LIBS= -lopencv_core -lopencv_imgproc -lopencv_legacy -lopencv_highgui
BOOST_LIBS= $(LIBPATH)/libboost_program_options-mt.a $(LIBPATH)/libboost_system-mt.a $(LIBPATH)/libboost_filesystem-mt.a

all:
g++ -O $(SRC) -o $(DST) -I$(INCPATH)/opencv -L. -L$(LIBPATH) $(CV_LIBS) -I$(INCPATH)/boost $(BOOST_LIBS)

-------------------------------------------------------------------------------