videocapture_gphoto2_autofocus.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (c) 2015, Piotr Dobrowolski dobrypd[at]gmail[dot]com
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  22. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  24. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #include <cstdlib>
  28. #include <cstdio>
  29. #include <iostream>
  30. #include <algorithm>
  31. #include <opencv2/core.hpp>
  32. #include <opencv2/imgproc.hpp>
  33. #include <opencv2/highgui.hpp>
  34. using namespace std;
  35. using namespace cv;
  36. const char * windowOriginal = "Captured preview";
  37. const int FOCUS_STEP = 1024;
  38. const int MAX_FOCUS_STEP = 32767;
  39. const int FOCUS_DIRECTION_INFTY = 1;
  40. const int DEFAULT_BREAK_LIMIT = 5;
  41. const int DEFAULT_OUTPUT_FPS = 20;
  42. const double epsylon = 0.0005; // compression, noise, etc.
  43. struct Args_t
  44. {
  45. string deviceName;
  46. string output;
  47. int fps;
  48. int minimumFocusStep;
  49. int breakLimit;
  50. bool measure;
  51. bool verbose;
  52. } GlobalArgs;
  53. struct FocusState
  54. {
  55. int step;
  56. int direction;
  57. int minFocusStep;
  58. int lastDirectionChange;
  59. int stepToLastMax;
  60. double rate;
  61. double rateMax;
  62. };
  63. static ostream & operator<<(ostream & os, FocusState & state)
  64. {
  65. return os << "RATE=" << state.rate << "\tSTEP="
  66. << state.step * state.direction << "\tLast change="
  67. << state.lastDirectionChange << "\tstepToLastMax="
  68. << state.stepToLastMax;
  69. }
  70. static FocusState createInitialState()
  71. {
  72. FocusState state;
  73. state.step = FOCUS_STEP;
  74. state.direction = FOCUS_DIRECTION_INFTY;
  75. state.minFocusStep = 0;
  76. state.lastDirectionChange = 0;
  77. state.stepToLastMax = 0;
  78. state.rate = 0;
  79. state.rateMax = 0;
  80. return state;
  81. }
  82. static void focusDriveEnd(VideoCapture & cap, int direction)
  83. {
  84. while (cap.set(CAP_PROP_ZOOM, (double) MAX_FOCUS_STEP * direction))
  85. ;
  86. }
  87. /**
  88. * Minimal focus step depends on lens
  89. * and I don't want to make any assumptions about it.
  90. */
  91. static int findMinFocusStep(VideoCapture & cap, unsigned int startWith,
  92. int direction)
  93. {
  94. int lStep, rStep;
  95. lStep = 0;
  96. rStep = startWith;
  97. focusDriveEnd(cap, direction * FOCUS_DIRECTION_INFTY);
  98. while (lStep < rStep)
  99. {
  100. int mStep = (lStep + rStep) / 2;
  101. cap.set(CAP_PROP_ZOOM, direction * FOCUS_DIRECTION_INFTY * FOCUS_STEP);
  102. if (cap.set(CAP_PROP_ZOOM, -direction * mStep))
  103. {
  104. rStep = mStep;
  105. }
  106. else
  107. {
  108. lStep = mStep + 1;
  109. }
  110. }
  111. cap.set(CAP_PROP_ZOOM, direction * FOCUS_DIRECTION_INFTY * MAX_FOCUS_STEP);
  112. if (GlobalArgs.verbose)
  113. {
  114. cout << "Found minimal focus step = " << lStep << endl;
  115. }
  116. return lStep;
  117. }
  118. /**
  119. * Rate frame from 0/blury/ to 1/sharp/.
  120. */
  121. static double rateFrame(Mat & frame)
  122. {
  123. unsigned long int sum = 0;
  124. unsigned long int size = frame.cols * frame.rows;
  125. Mat edges;
  126. cvtColor(frame, edges, COLOR_BGR2GRAY);
  127. GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
  128. Canny(edges, edges, 0, 30, 3);
  129. MatIterator_<uchar> it, end;
  130. for (it = edges.begin<uchar>(), end = edges.end<uchar>(); it != end; ++it)
  131. {
  132. sum += *it != 0;
  133. }
  134. return (double) sum / (double) size;
  135. }
  136. static int correctFocus(bool lastSucceeded, FocusState & state, double rate)
  137. {
  138. if (GlobalArgs.verbose)
  139. {
  140. cout << "RATE=" << rate << endl;
  141. }
  142. state.lastDirectionChange++;
  143. double rateDelta = rate - state.rate;
  144. if (rate >= state.rateMax + epsylon)
  145. {
  146. // Update Max
  147. state.stepToLastMax = 0;
  148. state.rateMax = rate;
  149. // My local minimum is now on the other direction, that's why:
  150. state.lastDirectionChange = 0;
  151. }
  152. if (!lastSucceeded)
  153. {
  154. // Focus at limit or other problem, change the direction.
  155. state.direction *= -1;
  156. state.lastDirectionChange = 0;
  157. state.step /= 2;
  158. }
  159. else
  160. {
  161. if (rate < epsylon)
  162. { // It's hard to say anything
  163. state.step = FOCUS_STEP;
  164. }
  165. else if (rateDelta < -epsylon)
  166. { // Wrong direction ?
  167. state.direction *= -1;
  168. state.step = static_cast<int>(static_cast<double>(state.step) * 0.75);
  169. state.lastDirectionChange = 0;
  170. }
  171. else if ((rate + epsylon < state.rateMax)
  172. && ((state.lastDirectionChange > 3)
  173. || ((state.step < (state.minFocusStep * 1.5))
  174. && state.stepToLastMax > state.step)))
  175. { // I've done 3 steps (or I'm finishing) without improvement, go back to max.
  176. state.direction = state.stepToLastMax >= 0 ? 1 : -1;
  177. state.step = static_cast<int>(static_cast<double>(state.step) * 0.75);
  178. int stepToMax = abs(state.stepToLastMax);
  179. state.stepToLastMax = 0;
  180. state.lastDirectionChange = 0; // Like reset.
  181. state.rate = rate;
  182. return stepToMax;
  183. }
  184. }
  185. // Update state.
  186. state.rate = rate;
  187. state.stepToLastMax -= state.direction * state.step;
  188. return state.step;
  189. }
  190. static void showHelp(const char * pName, bool welcomeMsg)
  191. {
  192. cout << "This program demonstrates usage of gPhoto2 VideoCapture.\n\n"
  193. "With OpenCV build without gPhoto2 library support it will "
  194. "do nothing special, just capture.\n\n"
  195. "Simple implementation of autofocus is based on edges detection.\n"
  196. "It was tested (this example) only with Nikon DSLR (Nikon D90).\n"
  197. "But shall work on all Nikon DSLRs, and with little effort with other devices.\n"
  198. "Visit http://www.gphoto.org/proj/libgphoto2/support.php\n"
  199. "to find supported devices (need Image Capture at least).\n"
  200. "Before run, set your camera autofocus ON.\n\n";
  201. if (!welcomeMsg)
  202. {
  203. cout << "usage " << pName << ": [OPTIONS] DEVICE_NAME\n\n"
  204. "OPTIONS:\n"
  205. "\t-h\t\treturns this help message,\n"
  206. "\t-o=<FILENAME>\tsave output video in file (MJPEG only),\n"
  207. "\t-f=FPS\t\tframes per second in output video,\n"
  208. "\t-m\t\tmeasure exposition\n"
  209. "\t\t\t(returns rates from closest focus to INTY\n"
  210. "\t\t\tfor every minimum step),\n"
  211. "\t-d=<INT>\t\tset minimum focus step,\n"
  212. "\t-v\t\tverbose mode.\n\n\n"
  213. "DEVICE_NAME\t\tis your digital camera model substring.\n\n\n"
  214. "On runtime you can use keys to control:\n";
  215. }
  216. else
  217. {
  218. cout << "Actions:\n";
  219. }
  220. cout << "\tk:\t- focus out,\n"
  221. "\tj:\t- focus in,\n"
  222. "\t,:\t- focus to the closest point,\n"
  223. "\t.:\t- focus to infinity,\n"
  224. "\tr:\t- reset autofocus state,\n"
  225. "\tf:\t- switch autofocus on/off,\n"
  226. "\tq:\t- quit.\n";
  227. }
  228. static bool parseArguments(int argc, char ** argv)
  229. {
  230. cv::CommandLineParser parser(argc, argv, "{h help ||}{o||}{f||}{m||}{d|0|}{v||}{@device|Nikon|}");
  231. if (parser.has("help"))
  232. return false;
  233. GlobalArgs.breakLimit = DEFAULT_BREAK_LIMIT;
  234. if (parser.has("o"))
  235. GlobalArgs.output = parser.get<string>("o");
  236. else
  237. GlobalArgs.output = "";
  238. if (parser.has("f"))
  239. GlobalArgs.fps = parser.get<int>("f");
  240. else
  241. GlobalArgs.fps = DEFAULT_OUTPUT_FPS;
  242. GlobalArgs.measure = parser.has("m");
  243. GlobalArgs.verbose = parser.has("v");
  244. GlobalArgs.minimumFocusStep = parser.get<int>("d");
  245. GlobalArgs.deviceName = parser.get<string>("@device");
  246. if (!parser.check())
  247. {
  248. parser.printErrors();
  249. return false;
  250. }
  251. if (GlobalArgs.fps < 0)
  252. {
  253. cerr << "Invalid fps argument." << endl;
  254. return false;
  255. }
  256. if (GlobalArgs.minimumFocusStep < 0)
  257. {
  258. cerr << "Invalid minimum focus step argument." << endl;
  259. return false;
  260. }
  261. return true;
  262. }
  263. int main(int argc, char ** argv)
  264. {
  265. if (!parseArguments(argc, argv))
  266. {
  267. showHelp(argv[0], false);
  268. return -1;
  269. }
  270. VideoCapture cap(GlobalArgs.deviceName);
  271. if (!cap.isOpened())
  272. {
  273. cout << "Cannot find device " << GlobalArgs.deviceName << endl;
  274. showHelp(argv[0], false);
  275. return -1;
  276. }
  277. VideoWriter videoWriter;
  278. Mat frame;
  279. FocusState state = createInitialState();
  280. bool focus = true;
  281. bool lastSucceeded = true;
  282. namedWindow(windowOriginal, 1);
  283. // Get settings:
  284. if (GlobalArgs.verbose)
  285. {
  286. if ((cap.get(CAP_PROP_GPHOTO2_WIDGET_ENUMERATE) == 0)
  287. || (cap.get(CAP_PROP_GPHOTO2_WIDGET_ENUMERATE) == -1))
  288. {
  289. // Some VideoCapture implementations can return -1, 0.
  290. cout << "This is not GPHOTO2 device." << endl;
  291. return -2;
  292. }
  293. cout << "List of camera settings: " << endl
  294. << (const char *) (intptr_t) cap.get(CAP_PROP_GPHOTO2_WIDGET_ENUMERATE)
  295. << endl;
  296. cap.set(CAP_PROP_GPHOTO2_COLLECT_MSGS, true);
  297. }
  298. cap.set(CAP_PROP_GPHOTO2_PREVIEW, true);
  299. cap.set(CAP_PROP_VIEWFINDER, true);
  300. cap >> frame; // To check PREVIEW output Size.
  301. if (!GlobalArgs.output.empty())
  302. {
  303. Size S = Size((int) cap.get(CAP_PROP_FRAME_WIDTH), (int) cap.get(CAP_PROP_FRAME_HEIGHT));
  304. int fourCC = VideoWriter::fourcc('M', 'J', 'P', 'G');
  305. videoWriter.open(GlobalArgs.output, fourCC, GlobalArgs.fps, S, true);
  306. if (!videoWriter.isOpened())
  307. {
  308. cerr << "Cannot open output file " << GlobalArgs.output << endl;
  309. showHelp(argv[0], false);
  310. return -1;
  311. }
  312. }
  313. showHelp(argv[0], true); // welcome msg
  314. if (GlobalArgs.minimumFocusStep == 0)
  315. {
  316. state.minFocusStep = findMinFocusStep(cap, FOCUS_STEP / 16, -FOCUS_DIRECTION_INFTY);
  317. }
  318. else
  319. {
  320. state.minFocusStep = GlobalArgs.minimumFocusStep;
  321. }
  322. focusDriveEnd(cap, -FOCUS_DIRECTION_INFTY); // Start with closest
  323. char key = 0;
  324. while (key != 'q' && key != 27 /*ESC*/)
  325. {
  326. cap >> frame;
  327. if (frame.empty())
  328. {
  329. break;
  330. }
  331. if (!GlobalArgs.output.empty())
  332. {
  333. videoWriter << frame;
  334. }
  335. if (focus && !GlobalArgs.measure)
  336. {
  337. int stepToCorrect = correctFocus(lastSucceeded, state, rateFrame(frame));
  338. lastSucceeded = cap.set(CAP_PROP_ZOOM,
  339. max(stepToCorrect, state.minFocusStep) * state.direction);
  340. if ((!lastSucceeded) || (stepToCorrect < state.minFocusStep))
  341. {
  342. if (--GlobalArgs.breakLimit <= 0)
  343. {
  344. focus = false;
  345. state.step = state.minFocusStep * 4;
  346. cout << "In focus, you can press 'f' to improve with small step, "
  347. "or 'r' to reset." << endl;
  348. }
  349. }
  350. else
  351. {
  352. GlobalArgs.breakLimit = DEFAULT_BREAK_LIMIT;
  353. }
  354. }
  355. else if (GlobalArgs.measure)
  356. {
  357. double rate = rateFrame(frame);
  358. if (!cap.set(CAP_PROP_ZOOM, state.minFocusStep))
  359. {
  360. if (--GlobalArgs.breakLimit <= 0)
  361. {
  362. break;
  363. }
  364. }
  365. else
  366. {
  367. cout << rate << endl;
  368. }
  369. }
  370. if ((focus || GlobalArgs.measure) && GlobalArgs.verbose)
  371. {
  372. cout << "STATE\t" << state << endl;
  373. cout << "Output from camera: " << endl
  374. << (const char *) (intptr_t) cap.get(CAP_PROP_GPHOTO2_FLUSH_MSGS) << endl;
  375. }
  376. imshow(windowOriginal, frame);
  377. switch (key = static_cast<char>(waitKey(30)))
  378. {
  379. case 'k': // focus out
  380. cap.set(CAP_PROP_ZOOM, 100);
  381. break;
  382. case 'j': // focus in
  383. cap.set(CAP_PROP_ZOOM, -100);
  384. break;
  385. case ',': // Drive to closest
  386. focusDriveEnd(cap, -FOCUS_DIRECTION_INFTY);
  387. break;
  388. case '.': // Drive to infinity
  389. focusDriveEnd(cap, FOCUS_DIRECTION_INFTY);
  390. break;
  391. case 'r': // reset focus state
  392. focus = true;
  393. state = createInitialState();
  394. break;
  395. case 'f': // focus switch on/off
  396. focus ^= true;
  397. break;
  398. }
  399. }
  400. if (GlobalArgs.verbose)
  401. {
  402. cout << "Captured " << (int) cap.get(CAP_PROP_FRAME_COUNT) << " frames"
  403. << endl << "in " << (int) (cap.get(CAP_PROP_POS_MSEC) / 1e2)
  404. << " seconds," << endl << "at avg speed "
  405. << (cap.get(CAP_PROP_FPS)) << " fps." << endl;
  406. }
  407. return 0;
  408. }