cv_core.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #include <stdio.h>
  2. #include <opencv2/opencv.hpp>
  3. #include <chrono>
  4. /***************** Mat转vector **********************/
  5. template<typename _Tp>
  6. std::vector<_Tp> convertMat2Vector(const cv::Mat &mat)
  7. {
  8. return (std::vector<_Tp>)(mat.reshape(1, 1));//通道数不变,按行转为一行
  9. }
  10. /****************** vector转Mat *********************/
  11. template<typename _Tp>
  12. cv::Mat convertVector2Mat(std::vector<_Tp> v, int channels, int rows)
  13. {
  14. cv::Mat mat = cv::Mat(v);//将vector变成单列的mat
  15. cv::Mat dest = mat.reshape(channels, rows).clone();//PS:必须clone()一份,否则返回出错
  16. return dest;
  17. }
  18. // 锐化矩阵
  19. // j-1 -1
  20. // j-nChannels j j+nChannels -1 5 -1
  21. // j+1 -1
  22. void Sharpen(const cv::Mat& myImage,cv::Mat& Result)
  23. {
  24. CV_Assert(myImage.depth() == CV_8U); // accept only uchar images
  25. const int nChannels = myImage.channels();
  26. Result.create(myImage.size(),myImage.type());
  27. for(int j = 1 ; j < myImage.rows-1; ++j)
  28. {
  29. const uchar* previous = myImage.ptr<uchar>(j - 1);
  30. const uchar* current = myImage.ptr<uchar>(j );
  31. const uchar* next = myImage.ptr<uchar>(j + 1);
  32. uchar* output = Result.ptr<uchar>(j);
  33. for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
  34. {
  35. *output++ = cv::saturate_cast<uchar>(5*current[i]
  36. -current[i-nChannels] - current[i+nChannels] - previous[i] - next[i]);
  37. }
  38. }
  39. Result.row(0).setTo(cv::Scalar(0));
  40. Result.row(Result.rows-1).setTo(cv::Scalar(0));
  41. Result.col(0).setTo(cv::Scalar(0));
  42. Result.col(Result.cols-1).setTo(cv::Scalar(0));
  43. }
  44. class MyData
  45. {
  46. public:
  47. MyData() : A(0), X(0), id()
  48. {
  49. }
  50. explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") // explicit to avoid implicit conversion
  51. {
  52. }
  53. void write(cv::FileStorage &fs) const //Write serialization for this class
  54. {
  55. fs << "{"
  56. << "A" << A << "X" << X << "id" << id << "}";
  57. }
  58. void read(const cv::FileNode &node) //Read serialization for this class
  59. {
  60. A = (int)node["A"];
  61. X = (double)node["X"];
  62. id = (std::string)node["id"];
  63. }
  64. public: // Data Members
  65. int A;
  66. double X;
  67. std::string id;
  68. };
  69. //These write and read functions must be defined for the serialization in FileStorage to work
  70. static void write(cv::FileStorage &fs, const std::string &, const MyData &x)
  71. {
  72. x.write(fs);
  73. }
  74. static void read(const cv::FileNode &node, MyData &x, const MyData &default_value = MyData())
  75. {
  76. if (node.empty())
  77. x = default_value;
  78. else
  79. x.read(node);
  80. }
  81. // This function will print our custom class to the console
  82. static std::ostream &operator<<(std::ostream &out, const MyData &m)
  83. {
  84. out << "{ id = " << m.id << ", ";
  85. out << "X = " << m.X << ", ";
  86. out << "A = " << m.A << "}";
  87. return out;
  88. }
  89. int main(int argc, char** argv )
  90. {
  91. char * addr = "./src/cv_test/backGround.jpg";
  92. cv::String imageName(addr);
  93. //创建Mat
  94. cv::Mat image, foreground;
  95. //读取图片
  96. image = cv::imread(imageName, 1 );
  97. foreground = cv::imread("./src/cv_test/foreGround.png", 1);
  98. //判断图片是否存在
  99. if(!image.empty() && image.data && !foreground.empty()){
  100. // //创建窗口
  101. // cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE );
  102. // //显示图片
  103. // cv::imshow("Display Image", image);
  104. //彩图转灰度图
  105. cv::Mat gray_image;
  106. cv::cvtColor(image, gray_image, cv::COLOR_BGR2GRAY);
  107. // cv::namedWindow("Display Image2", cv::WINDOW_FREERATIO );
  108. // cv::imshow("Display Image2", gray_image);
  109. // //保存图片
  110. // cv::imwrite( "./src/cv_test/Gray_Image.jpg", gray_image);
  111. {//mat 与 vector 转换
  112. // //https://blog.csdn.net/guyuealian/article/details/80253066
  113. // std::vector<uchar> v = convertMat2Vector<uchar>(gray_image);
  114. // cv::Mat dest = convertVector2Mat<uchar>(v, 1, 352); //把数据转为1通道,4行的Mat数据
  115. // cv::namedWindow("Display Image dest", cv::WINDOW_KEEPRATIO );
  116. // cv::imshow("Display Image dest", dest);
  117. }
  118. {//创建Mat
  119. // //浅拷贝
  120. // cv::Mat C(gray_image);
  121. // cv::Mat D(C, cv::Rect(0, 0, 400, 400));
  122. // cv::Mat E = C(cv::Range::all(), cv::Range(1,100));
  123. // std::cout<<*C.data<<", "<<*D.data<<std::endl;
  124. // // cv::namedWindow("Display Image D", cv::WINDOW_AUTOSIZE );
  125. // // cv::imshow("Display Image D", D);
  126. // // cv::namedWindow("Display Image E", cv::WINDOW_AUTOSIZE );
  127. // // cv::imshow("Display Image E", E);
  128. // //深拷贝
  129. // cv::Mat F = D.clone();
  130. // cv::Mat G; E.copyTo(G);
  131. // std::cout<<*D.data<<", "<<*F.data<<std::endl;
  132. // // cv::namedWindow("Display Image F", cv::WINDOW_AUTOSIZE);
  133. // // cv::imshow("Display Image F", F);
  134. // // cv::namedWindow("Display Image G", cv::WINDOW_AUTOSIZE );
  135. // // cv::imshow("Display Image G", G);
  136. // cv::Mat M(6, 6, CV_8UC3, cv::Scalar(255, 0, 255));//BGR
  137. // std::cout << "M = " << M << std::endl;
  138. // cv::namedWindow("Display Image M", cv::WINDOW_KEEPRATIO );
  139. // cv::imshow("Display Image M", M);
  140. // M.create(12, 3, CV_8UC3);
  141. // std::cout << "M = " << M << std::endl;
  142. // cv::namedWindow("Display Image N", cv::WINDOW_KEEPRATIO );
  143. // cv::imshow("Display Image N", M);
  144. // cv::Mat K = cv::Mat::eye(4, 4, CV_64F);
  145. // std::cout << "K = " << K << std::endl;
  146. // cv::Mat O = cv::Mat::ones(5, 5, CV_32F);
  147. // std::cout << "O = " << O << std::endl;
  148. // cv::Mat Z = cv::Mat::zeros(6, 6, CV_8UC1);
  149. // std::cout << "Z = " << Z << std::endl;
  150. }
  151. {//平滑
  152. // cv::Mat result, result2;
  153. // //手动锐化
  154. // Sharpen(image, result);
  155. // cv::namedWindow("Display sharpen 1", cv::WINDOW_KEEPRATIO);
  156. // cv::imshow("Display sharpen 1", result);
  157. // //封装方法
  158. // cv::Mat kernel = (cv::Mat_<char>(3, 3) << 1, 1, 1,
  159. // 1, -9, 1,
  160. // 1, 1, 1);
  161. // cv::filter2D( image, result2, image.depth(), kernel );
  162. // cv::namedWindow("Display sharpen 2", cv::WINDOW_KEEPRATIO);
  163. // cv::imshow("Display sharpen 2", result2);
  164. }
  165. {//图像操作
  166. // // //计时
  167. // // double t = (double)cv::getTickCount();
  168. // // int x = 5, y = 2;
  169. // // cv::Scalar intensity = gray_image.at<uchar>(y, x);
  170. // // cv::Scalar intensity2 = gray_image.at<uchar>(cv::Point(x, y));
  171. // // cv::Vec3b intensity3 = image.at<cv::Vec3b>(y, x);
  172. // // uchar blue = intensity3.val[0];
  173. // // uchar green = intensity3.val[1];
  174. // // uchar red = intensity3.val[2];
  175. // // std::cout << "intensity: " << intensity << ", " << intensity2 << ", " << intensity3 << std::endl;
  176. // // std::cout << "color: " << int(blue) << ", " << int(green) << ", " << int(red) << std::endl;
  177. // if (image.channels() == 3)
  178. // {
  179. // for (int j = 0; j < image.rows; j++)
  180. // {
  181. // for (int i = 0; i < image.cols; i++)
  182. // {
  183. // cv::Vec3b point = image.at<cv::Vec3b>(j, i);
  184. // uchar temp = point.val[2];
  185. // point.val[2] = point.val[1];
  186. // point.val[1] = point.val[0];
  187. // point.val[0] = temp;
  188. // image.at<cv::Vec3b>(j, i) = point;
  189. // }
  190. // }
  191. // cv::namedWindow("Display manipulated image", cv::WINDOW_KEEPRATIO);
  192. // cv::imshow("Display manipulated image", image);
  193. // }
  194. // // t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
  195. // // std::cout << "Times passed in seconds: " << t << std::endl;
  196. }
  197. {//融合
  198. // double alpha = 0.5;
  199. // cv::Mat result;
  200. // cv::addWeighted(image,alpha, foreground, 1-alpha, 0.0, result);
  201. // cv::imshow("linear blend", result);
  202. }
  203. {//对比度与亮度, gamma correction
  204. // // When γ<1, the original dark regions will be brighter and
  205. // // the histogram will be shifted to the right whereas it will be the opposite with γ>1
  206. // double alpha = 1.25, beta = -50, gamma = 2, temp = 0;
  207. // cv::Mat result = cv::Mat::zeros(image.size(), image.type());
  208. // for (int i = 0; i < image.rows; i++)
  209. // {
  210. // for (int j = 0; j < image.cols; j++)
  211. // {
  212. // for (int ch = 0; ch < image.channels(); ch++)
  213. // {
  214. // temp = alpha * image.at<cv::Vec3b>(i, j)[ch] + beta;
  215. // //std::cout<<temp<<std::endl;
  216. // result.at<cv::Vec3b>(i, j)[ch] = cv::saturate_cast<uchar>( pow( temp/255, gamma)*255 );
  217. // }
  218. // }
  219. // }
  220. // cv::imshow("contrast & lightness",result);
  221. }
  222. {//drawing基本类型
  223. // // cv::Point p0(50,50), p1(100,100);
  224. // // double b = 255, g = 0, r = 255;
  225. // // cv::Scalar vector4(b,g,r);
  226. // int width = 500;
  227. // cv::Mat atom = cv::Mat::zeros(width, width, CV_8UC3);
  228. // // cv::line(atom,
  229. // // p0,
  230. // // p1,
  231. // // cv::Scalar(255, 0, 0),
  232. // // 2,
  233. // // cv::LINE_8);
  234. // // cv::rectangle(atom,
  235. // // cv::Point(210,200),
  236. // // p1,
  237. // // cv::Scalar(0, 255, 255),
  238. // // cv::FILLED,
  239. // // cv::LINE_8);
  240. // // cv::circle(atom,
  241. // // p1,
  242. // // width / 32,
  243. // // cv::Scalar(0, 0, 255),
  244. // // cv::FILLED,
  245. // // cv::LINE_8);
  246. // // cv::ellipse(atom,
  247. // // p0,
  248. // // cv::Size(width / 4, width / 16),
  249. // // M_PI_4,
  250. // // 0,
  251. // // 360,
  252. // // cv::Scalar(255, 0, 0),
  253. // // 2,
  254. // // cv::LINE_8);
  255. // // cv::Point rook_points[1][20];
  256. // // rook_points[0][0] = cv::Point(width / 4, 7 * width / 8);
  257. // // rook_points[0][1] = cv::Point(3 * width / 4, 7 * width / 8);
  258. // // rook_points[0][2] = cv::Point(3 * width / 4, 13 * width / 16);
  259. // // rook_points[0][3] = cv::Point(11 * width / 16, 13 * width / 16);
  260. // // rook_points[0][4] = cv::Point(19 * width / 32, 3 * width / 8);
  261. // // rook_points[0][5] = cv::Point(3 * width / 4, 3 * width / 8);
  262. // // rook_points[0][6] = cv::Point(3 * width / 4, width / 8);
  263. // // rook_points[0][7] = cv::Point(26 * width / 40, width / 8);
  264. // // rook_points[0][8] = cv::Point(26 * width / 40, width / 4);
  265. // // rook_points[0][9] = cv::Point(22 * width / 40, width / 4);
  266. // // rook_points[0][10] = cv::Point(22 * width / 40, width / 8);
  267. // // rook_points[0][11] = cv::Point(18 * width / 40, width / 8);
  268. // // rook_points[0][12] = cv::Point(18 * width / 40, width / 4);
  269. // // rook_points[0][13] = cv::Point(14 * width / 40, width / 4);
  270. // // rook_points[0][14] = cv::Point(14 * width / 40, width / 8);
  271. // // rook_points[0][15] = cv::Point(width / 4, width / 8);
  272. // // rook_points[0][16] = cv::Point(width / 4, 3 * width / 8);
  273. // // rook_points[0][17] = cv::Point(13 * width / 32, 3 * width / 8);
  274. // // rook_points[0][18] = cv::Point(5 * width / 16, 13 * width / 16);
  275. // // rook_points[0][19] = cv::Point(width / 4, 13 * width / 16);
  276. // // const cv::Point *ppt[1] = {rook_points[0]};
  277. // // int npt[] = {20};
  278. // // fillPoly(atom,
  279. // // ppt,
  280. // // npt,
  281. // // 1,
  282. // // cv::Scalar(255, 255, 255),
  283. // // cv::LINE_8);
  284. // cv::RNG rnd;
  285. // cv::Size textsize = cv::getTextSize("OpenCV forever!", cv::FONT_HERSHEY_COMPLEX, 3, 5, 0);
  286. // cv::Point org(150, 150);
  287. // cv::putText(atom, "OpenCV forever!", org, cv::FONT_HERSHEY_COMPLEX, 1,
  288. // cv::Scalar(rnd.uniform(0,255), rnd.uniform(0,255), rnd.uniform(0,255)), 3, cv::LINE_8);
  289. // cv::namedWindow("atom", cv::WINDOW_FREERATIO);
  290. // cv::moveWindow("atom", 50, 50);
  291. // cv::imshow("atom", atom);
  292. }
  293. {//DFT & IDFT
  294. // // https://blog.csdn.net/abcjennifer/article/details/7622228
  295. // cv::Mat padded; //expand input image to optimal size
  296. // int m = cv::getOptimalDFTSize(gray_image.rows);
  297. // int n = cv::getOptimalDFTSize(gray_image.cols); // on the border add zero values
  298. // cv::copyMakeBorder(gray_image, padded, 0, m - gray_image.rows, 0, n - gray_image.cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));
  299. // cv::Mat planes[] = {cv::Mat_<float>(padded), cv::Mat::zeros(padded.size(), CV_32F)};
  300. // cv::Mat complexI;
  301. // cv::merge(planes, 2, complexI); // Add to the expanded another plane with zeros
  302. // cv::dft(complexI, complexI); // this way the result may fit in the source matrix
  303. // // compute the magnitude and switch to logarithmic scale
  304. // // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
  305. // cv::split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
  306. // cv::Mat magI;
  307. // cv::magnitude(planes[0], planes[1], magI); // planes[0] = magnitude
  308. // magI += cv::Scalar::all(1); // switch to logarithmic scale
  309. // cv::log(magI, magI);
  310. // // crop the spectrum, if it has an odd number of rows or columns, -2 = fffffffe, ignore the LSB
  311. // magI = magI(cv::Rect(0, 0, magI.cols & -2, magI.rows & -2));
  312. // // rearrange the quadrants of Fourier image so that the origin is at the image center
  313. // int cx = magI.cols / 2;
  314. // int cy = magI.rows / 2;
  315. // // q0 q1
  316. // // q2 q3
  317. // cv::Mat q0(magI, cv::Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
  318. // cv::Mat q1(magI, cv::Rect(cx, 0, cx, cy)); // Top-Right
  319. // cv::Mat q2(magI, cv::Rect(0, cy, cx, cy)); // Bottom-Left
  320. // cv::Mat q3(magI, cv::Rect(cx, cy, cx, cy)); // Bottom-Right
  321. // cv::Mat tmp; // swap quadrants (Top-Left with Bottom-Right)
  322. // q0.copyTo(tmp);
  323. // q3.copyTo(q0);
  324. // tmp.copyTo(q3);
  325. // q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
  326. // q2.copyTo(q1);
  327. // tmp.copyTo(q2);
  328. // // q3 q2
  329. // // q1 q0
  330. // cv::normalize(magI, magI, 0, 1, cv::NORM_MINMAX); // Transform the matrix with float values into a
  331. // // viewable image form (float between values 0 and 1).
  332. // cv::Mat mask = cv::Mat::zeros(magI.size(), CV_8UC1);
  333. // cv::Mat img, img2;
  334. // // 中心:基本轮廓特征,总体灰度信息;
  335. // // 内圈:分块与区域特征
  336. // // 次外圈:边缘、线条、纹理细节
  337. // // 外圈:高频噪声
  338. // double ratio = 3.0;
  339. // // 保留高频细节
  340. // // mask.setTo(255);
  341. // // mask(cv::Rect(magI.cols/ratio,magI.rows/ratio,magI.cols*(ratio-2.0)/ratio, magI.rows*(ratio-2.0)/ratio)).setTo(0);
  342. // // 保留低频特征
  343. // mask(cv::Rect(magI.cols / ratio, magI.rows / ratio, magI.cols * (ratio - 2.0) / ratio, magI.rows * (ratio - 2.0) / ratio)).setTo(255);
  344. // //cv::imshow("mask",mask);
  345. // magI.copyTo(img, mask);
  346. // cv::namedWindow("spectrum magnitude", cv::WINDOW_FREERATIO);
  347. // cv::imshow("spectrum magnitude", img);
  348. // // std::cout<<img(cv::Rect(img.cols/2.0-5,img.rows/2.0-5,10,10))<<std::endl;
  349. // complexI.copyTo(img2, mask);
  350. // cv::idft(img2, img2);
  351. // cv::split(img2, planes);
  352. // cv::Mat magR = planes[0];
  353. // magR += cv::Scalar::all(1); // switch to logarithmic scale
  354. // cv::log(magR, magR);
  355. // cv::normalize(magR, magR, -1.0, 0.5, cv::NORM_MINMAX);
  356. // cv::namedWindow("spatial domain", cv::WINDOW_FREERATIO);
  357. // cv::imshow("spatial domain", magR);
  358. }
  359. {//yaml & xml
  360. // std::string filename = "./src/cv_test/my.yaml";
  361. // { //write
  362. // cv::Mat R = cv::Mat_<uchar>::eye(3, 3),
  363. // T = cv::Mat_<double>::zeros(3, 1);
  364. // MyData m(1);
  365. // cv::FileStorage fs(filename, cv::FileStorage::WRITE);
  366. // fs << "iterationNr" << 100;
  367. // fs << "strings"
  368. // << "["; // text - string sequence
  369. // fs << "image1.jpg"
  370. // << "Awesomeness"
  371. // << "./src/cv_test/baboon.jpg";
  372. // fs << "]"; // close sequence
  373. // fs << "Mapping"; // text - mapping
  374. // fs << "{"
  375. // << "One" << 1;
  376. // fs << "Two" << 2 << "}";
  377. // fs << "R" << R; // cv::Mat
  378. // fs << "T" << T;
  379. // fs << "MyData" << m; // your own data structures
  380. // fs.release(); // explicit close
  381. // std::cout << "Write Done." << std::endl;
  382. // }
  383. // { //read
  384. // std::cout << std::endl
  385. // << "Reading: " << std::endl;
  386. // cv::FileStorage fs;
  387. // fs.open(filename, cv::FileStorage::READ);
  388. // int itNr;
  389. // //fs["iterationNr"] >> itNr;
  390. // itNr = (int)fs["iterationNr"];
  391. // std::cout << itNr;
  392. // if (!fs.isOpened())
  393. // {
  394. // std::cerr << "Failed to open " << filename << std::endl;
  395. // return 1;
  396. // }
  397. // cv::FileNode n = fs["strings"]; // Read string sequence - Get node
  398. // if (n.type() != cv::FileNode::SEQ)
  399. // {
  400. // std::cerr << "strings is not a sequence! FAIL" << std::endl;
  401. // return 1;
  402. // }
  403. // cv::FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node
  404. // for (; it != it_end; ++it)
  405. // std::cout << (std::string)*it << std::endl;
  406. // n = fs["Mapping"]; // Read mappings from a sequence
  407. // std::cout << "Two " << (int)(n["Two"]) << "; ";
  408. // std::cout << "One " << (int)(n["One"]) << std::endl
  409. // << std::endl;
  410. // MyData m;
  411. // cv::Mat R, T;
  412. // fs["R"] >> R; // Read cv::Mat
  413. // fs["T"] >> T;
  414. // fs["MyData"] >> m; // Read your own structure_
  415. // std::cout << std::endl
  416. // << "R = " << R << std::endl;
  417. // std::cout << "T = " << T << std::endl
  418. // << std::endl;
  419. // std::cout << "MyData = " << std::endl
  420. // << m << std::endl
  421. // << std::endl;
  422. // //Show default behavior for non existing nodes
  423. // std::cout << "Attempt to read NonExisting (should initialize the data structure with its default).";
  424. // fs["NonExisting"] >> m;
  425. // std::cout << std::endl
  426. // << "NonExisting = " << std::endl
  427. // << m << std::endl;
  428. // }
  429. }
  430. //中断;
  431. cv::waitKey(0);
  432. }else
  433. {
  434. std::cout<<imageName<<std::endl;
  435. std::cout<<"empty image"<<std::endl;
  436. }
  437. {/*
  438. char filename[255], img_name[128];
  439. sprintf(filename, "/home/youchen/Documents/catkin_ws/my1.yaml");
  440. sprintf(img_name, "/home/youchen/Documents/catkin_ws/my1.jpg");
  441. cv::FileStorage fs;
  442. fs.open(filename, cv::FileStorage::READ);
  443. if (!fs.isOpened())
  444. {
  445. std::cerr << "Failed to open " << std::endl;
  446. return 1;
  447. }
  448. cv::Mat R;
  449. fs["R"] >> R; // Read cv::Mat
  450. //cv::namedWindow("grid", cv::WINDOW_FREERATIO);
  451. //cv::imshow("grid", R);
  452. //cv::waitKey(0);
  453. fs.release();
  454. cv::imwrite(img_name, R*255);
  455. cv::namedWindow("grid", cv::WINDOW_FREERATIO);
  456. cv::imshow("grid", R);
  457. cv::waitKey(0);
  458. for(int i=550;i<2000;i+=50){
  459. memset(filename, 0, 255);
  460. memset(img_name, 0, 128);
  461. sprintf(filename, "/home/youchen/Documents/catkin_ws/my%d.yaml", i);
  462. sprintf(img_name, "/home/youchen/Documents/catkin_ws/my%d.jpg", i);
  463. cv::FileStorage fs;
  464. fs.open(filename, cv::FileStorage::READ);
  465. if (!fs.isOpened())
  466. {
  467. std::cerr << "Failed to open " << std::endl;
  468. return 1;
  469. }
  470. cv::Mat R;
  471. fs["R"] >> R; // Read cv::Mat
  472. //cv::namedWindow("grid", cv::WINDOW_FREERATIO);
  473. //cv::imshow("grid", R);
  474. //cv::waitKey(0);
  475. fs.release();
  476. cv::imwrite(img_name, R*255);
  477. }
  478. */
  479. }
  480. // 生成随机数
  481. typedef std::chrono::duration<int,std::ratio<1,1000>> milli_type;
  482. std::chrono::steady_clock::time_point current_time = std::chrono::steady_clock::now();
  483. std::chrono::time_point<std::chrono::steady_clock, milli_type> current_time_in_milliseconds = std::chrono::time_point_cast<milli_type>(current_time);
  484. int now_in_milliseconds = current_time_in_milliseconds.time_since_epoch().count();
  485. uint64 seed = uint64(now_in_milliseconds);
  486. cv::RNG rnd = cv::RNG(seed);
  487. int count = rnd.uniform(4,9);
  488. char buf[1024]={0};
  489. sprintf(buf, "today's code is:\n");
  490. for (size_t i = 0; i < count; i++)
  491. {
  492. unsigned long basic_code = 0;
  493. sprintf(buf+strlen(buf), "basic: [");
  494. for (size_t j = 0; j < 6; j++)
  495. {
  496. int current_code = -1;
  497. do
  498. {
  499. current_code = rnd.uniform(1, 41);
  500. // std::cout<<"current: "<<current_code<<std::endl;
  501. } while (current_code <0 || (((unsigned long)1 << current_code) & basic_code) > 0);
  502. basic_code |= ((unsigned long)1<<current_code);
  503. // std::cout<<basic_code<<std::endl;
  504. sprintf(buf + strlen(buf), "%3d ", current_code);
  505. }
  506. sprintf(buf + strlen(buf), "] power: [%2d]\n", rnd.uniform(1, 11));
  507. }
  508. // std::cout<<strlen(buf)<<std::endl;
  509. std::cout<<buf<<std::endl;
  510. return 0;
  511. }