{"issue":{"state":"open","created_at":"2011-12-03T01:50:50Z","updated_at":"2011-12-12T10:56:43Z","comments":2,"user":{"gravatar_id":"afc8687d0f839a61d3fafa9b38adb885","id":1078033,"url":"https://api.github.com/users/sky-y","avatar_url":"https://secure.gravatar.com/avatar/afc8687d0f839a61d3fafa9b38adb885?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"sky-y"},"body":"HomebrewでOpenCV 2.3.1 をインストールするといろいろ変更点が出たので、修正版を貼っておきます。\r\n\r\n\r\nkindlize_images.cpp\r\n-------------------------------------------------------------------------------\r\n#include \"cv.h\"\r\n#include \"highgui.h\"\r\n#include <boost/program_options.hpp>\r\n#include <boost/filesystem/operations.hpp>\r\n#include <boost/filesystem/path.hpp>\r\n#include <boost/filesystem/fstream.hpp>\r\n#include <iostream>\r\nusing namespace boost;\r\nusing namespace std;\r\nnamespace fs = boost::filesystem;\r\n\r\nIplImage *adjust_image(IplImage *img, program_options::variables_map argmap){\r\n  int cleft = argmap[\"cleft\"].as<int>();\r\n  int cright = argmap[\"cright\"].as<int>();\r\n  int ctop = argmap[\"ctop\"].as<int>();\r\n  int cbottom = argmap[\"cbottom\"].as<int>();\r\n\r\n  const int w = img->width - cleft - cright;\r\n  const int h = img->height - ctop - cbottom;\r\n  \r\n  cvSetImageROI(img, cvRect(cleft, ctop, w, h));\r\n\r\n  IplImage *img_gray = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 1);\r\n  cvCvtColor(img, img_gray, CV_BGR2GRAY);\r\n\r\n  // 2値化\r\n  IplImage *img_bin = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 1);\r\n  cvThreshold(img_gray, img_bin, argmap[\"threshold\"].as<int>(), 255, CV_THRESH_BINARY);\r\n\r\n  // 文字を太らせる\r\n  cvErode(img_bin, img_bin, NULL, 1);\r\n\r\n  // リサイズ\r\n  int width = argmap[\"width\"].as<int>();\r\n  int height = argmap[\"height\"].as<int>();\r\n  IplImage *img_resized;\r\n  double scale = ((double)h)/w;\r\n  if(((double)height)/width < scale){ // 縦長\r\n    img_resized = cvCreateImage(cvSize((int)(height/scale), height), IPL_DEPTH_8U, 1);\r\n  }\r\n  else{ // 横長\r\n    img_resized = cvCreateImage(cvSize(width, (int)(scale*width)), IPL_DEPTH_8U, 1);\r\n  }\r\n  cvResize(img_bin, img_resized, CV_INTER_LINEAR);\r\n\r\n  cvReleaseImage(&img_bin);\r\n  cvReleaseImage(&img_gray);\r\n  return img_resized;\r\n}\r\n\r\nint main(int argc, char* argv[]) {\r\n  program_options::options_description opts(\"options\");\r\n  opts.add_options()\r\n    (\"help\", \"ヘルプを表示\")\r\n    (\"width,w\", program_options::value<int>(), \"output width\")\r\n    (\"height,h\", program_options::value<int>(), \"output height\")\r\n    (\"threshold,t\", program_options::value<int>(), \"binarize threshold\")\r\n    (\"input,i\", program_options::value<string>(), \"input directory name\")\r\n    (\"output,o\", program_options::value<string>(), \"output directory name\")\r\n    (\"cleft\", program_options::value<int>(), \"crop left (pixel)\")\r\n    (\"cright\", program_options::value<int>(), \"crop right (pixel)\")\r\n    (\"ctop\", program_options::value<int>(), \"crop top (pixel)\")\r\n    (\"cbottom\", program_options::value<int>(), \"crop bottom (pixel)\");\r\n  program_options::variables_map argmap;\r\n  program_options::store(parse_command_line(argc, argv, opts), argmap);\r\n  program_options::notify(argmap);\r\n  if (argmap.count(\"help\") || !argmap.count(\"input\") || !argmap.count(\"output\") ||\r\n      !argmap.count(\"threshold\")) {\r\n    cerr << \"[input, output] required\" << endl;\r\n    cerr << opts << endl;\r\n    return 1;\r\n  }\r\n\r\n  string in_dir = argmap[\"input\"].as<string>();\r\n  fs::path path = complete(fs::path(in_dir));\r\n  fs::directory_iterator end;\r\n  for (fs::directory_iterator i(path); i!=end; i++){\r\n    // string img_fullname = in_dir + i->path().string();\r\n    string img_fullname = in_dir + \"/\" + i->path().filename().string();\r\n    cout << img_fullname << endl;\r\n    IplImage *img, *img_result;\r\n    img = cvLoadImage(img_fullname.c_str());\r\n    if(!img){\r\n      cerr << \"image file load error\" << endl;\r\n    }\r\n    else{\r\n      img_result = adjust_image(img, argmap);\r\n      string out_filename = argmap[\"output\"].as<string>() + \"/\" + i->path().string();\r\n      cvSaveImage(out_filename.c_str(), img_result);\r\n      cvReleaseImage(&img);\r\n      cvReleaseImage(&img_result);\r\n    }    \r\n  }\r\n}\r\n\r\n-------------------------------------------------------------------------------\r\n\r\n\r\n\r\nMakefile.macosx\r\n-------------------------------------------------------------------------------\r\n# Mac用Makefile\r\nSRC = kindlize_images.cpp\r\nDST = kindlize_images\r\n\r\nprefix=/usr/local\r\nINCPATH=$(prefix)/include\r\nLIBPATH=$(prefix)/lib\r\n\r\nCV_LIBS= -lopencv_core -lopencv_imgproc -lopencv_legacy -lopencv_highgui\r\nBOOST_LIBS= $(LIBPATH)/libboost_program_options-mt.a $(LIBPATH)/libboost_system-mt.a $(LIBPATH)/libboost_filesystem-mt.a\r\n\r\nall:\r\n\tg++ -O $(SRC) -o $(DST) -I$(INCPATH)/opencv -L. -L$(LIBPATH) $(CV_LIBS) -I$(INCPATH)/boost $(BOOST_LIBS)\r\n\r\n-------------------------------------------------------------------------------","assignee":null,"number":1,"id":2435200,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"labels":[],"title":"OpenCV 2.3.1","html_url":"https://github.com/shokai/scansnap_adjust_images_kindle/issues/1","closed_at":null,"url":"https://api.github.com/repos/shokai/scansnap_adjust_images_kindle/issues/1","milestone":null},"action":"created","comment":{"created_at":"2011-12-12T10:56:43Z","updated_at":"2011-12-12T10:56:43Z","user":{"gravatar_id":"40cb96fcda5fcc78cbbb5fa0d0535a35","id":34204,"url":"https://api.github.com/users/shokai","avatar_url":"https://secure.gravatar.com/avatar/40cb96fcda5fcc78cbbb5fa0d0535a35?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"shokai"},"body":"はい。","id":3104597,"url":"https://api.github.com/repos/shokai/scansnap_adjust_images_kindle/issues/comments/3104597"}}