Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.89 KB | None | 0 0
  1. // OpenCVApplication.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "common.h"
  6.  
  7. using namespace std;
  8.  
  9. #include <math.h>
  10. #include <queue>
  11. #include <random>
  12.  
  13. void testOpenImage()
  14. {
  15. char fname[MAX_PATH];
  16. while (openFileDlg(fname))
  17. {
  18. Mat src;
  19. src = imread(fname);
  20. imshow("image", src);
  21. waitKey();
  22. }
  23. }
  24.  
  25. void testOpenImagesFld()
  26. {
  27. char folderName[MAX_PATH];
  28. if (openFolderDlg(folderName) == 0)
  29. return;
  30. char fname[MAX_PATH];
  31. FileGetter fg(folderName, "bmp");
  32. while (fg.getNextAbsFile(fname))
  33. {
  34. Mat src;
  35. src = imread(fname);
  36. imshow(fg.getFoundFileName(), src);
  37. if (waitKey() == 27) //ESC pressed
  38. break;
  39. }
  40. }
  41.  
  42. void testImageOpenAndSave()
  43. {
  44. Mat src, dst;
  45.  
  46. src = imread("Images/Lena_24bits.bmp", CV_LOAD_IMAGE_COLOR); // Read the image
  47.  
  48. if (!src.data) // Check for invalid input
  49. {
  50. printf("Could not open or find the image\n");
  51. return;
  52. }
  53.  
  54. // Get the image resolution
  55. Size src_size = Size(src.cols, src.rows);
  56.  
  57. // Display window
  58. const char* WIN_SRC = "Src"; //window for the source image
  59. namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
  60. cvMoveWindow(WIN_SRC, 0, 0);
  61.  
  62. const char* WIN_DST = "Dst"; //window for the destination (processed) image
  63. namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
  64. cvMoveWindow(WIN_DST, src_size.width + 10, 0);
  65.  
  66. cvtColor(src, dst, CV_BGR2GRAY); //converts the source image to a grayscale one
  67.  
  68. imwrite("Images/Lena_24bits_gray.bmp", dst); //writes the destination to file
  69.  
  70. imshow(WIN_SRC, src);
  71. imshow(WIN_DST, dst);
  72.  
  73. printf("Press any key to continue ...\n");
  74. waitKey(0);
  75. }
  76.  
  77. void testNegativeImage()
  78. {
  79. char fname[MAX_PATH];
  80. while (openFileDlg(fname))
  81. {
  82. double t = (double)getTickCount(); // Get the current time [s]
  83.  
  84. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  85. int height = src.rows;
  86. int width = src.cols;
  87. Mat dst = Mat(height, width, CV_8UC1);
  88. // Asa se acceseaaza pixelii individuali pt. o imagine cu 8 biti/pixel
  89. // Varianta ineficienta (lenta)
  90. for (int i = 0; i<height; i++)
  91. {
  92. for (int j = 0; j<width; j++)
  93. {
  94. uchar val = src.at<uchar>(i, j);
  95. uchar neg = MAX_PATH - val;
  96. dst.at<uchar>(i, j) = neg;
  97. }
  98. }
  99.  
  100. // Get the current time again and compute the time difference [s]
  101. t = ((double)getTickCount() - t) / getTickFrequency();
  102. // Print (in the console window) the processing time in [ms]
  103. printf("Time = %.3f [ms]\n", t * 1000);
  104.  
  105. imshow("input image", src);
  106. imshow("negative image", dst);
  107. waitKey();
  108. }
  109. }
  110.  
  111. void testParcurgereSimplaDiblookStyle()
  112. {
  113. char fname[MAX_PATH];
  114. while (openFileDlg(fname))
  115. {
  116. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  117. int height = src.rows;
  118. int width = src.cols;
  119. Mat dst = src.clone();
  120.  
  121. double t = (double)getTickCount(); // Get the current time [s]
  122.  
  123. // the fastest approach using the “diblook style”
  124. uchar *lpSrc = src.data;
  125. uchar *lpDst = dst.data;
  126. int w = (int)src.step; // no dword alignment is done !!!
  127. for (int i = 0; i<height; i++)
  128. for (int j = 0; j < width; j++) {
  129. uchar val = lpSrc[i*w + j];
  130. lpDst[i*w + j] = 255 - val;
  131. }
  132.  
  133. // Get the current time again and compute the time difference [s]
  134. t = ((double)getTickCount() - t) / getTickFrequency();
  135. // Print (in the console window) the processing time in [ms]
  136. printf("Time = %.3f [ms]\n", t * 1000);
  137.  
  138. imshow("input image", src);
  139. imshow("negative image", dst);
  140. waitKey();
  141. }
  142. }
  143.  
  144. void testColor2Gray()
  145. {
  146. char fname[MAX_PATH];
  147. while (openFileDlg(fname))
  148. {
  149. Mat src = imread(fname);
  150.  
  151. int height = src.rows;
  152. int width = src.cols;
  153.  
  154. Mat dst = Mat(height, width, CV_8UC1);
  155.  
  156. // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
  157. // Varianta ineficienta (lenta)
  158. for (int i = 0; i<height; i++)
  159. {
  160. for (int j = 0; j<width; j++)
  161. {
  162. Vec3b v3 = src.at<Vec3b>(i, j);
  163. uchar b = v3[0];
  164. uchar g = v3[1];
  165. uchar r = v3[2];
  166. dst.at<uchar>(i, j) = (r + g + b) / 3;
  167. }
  168. }
  169.  
  170. imshow("input image", src);
  171. imshow("gray image", dst);
  172. waitKey();
  173. }
  174. }
  175.  
  176. void changeImageGray() {
  177. char frame[MAX_PATH];
  178. while (openFileDlg(frame)) {
  179. Mat_<uchar> src;
  180. src = imread(frame, CV_LOAD_IMAGE_GRAYSCALE);
  181. int height = src.rows;
  182. int width = src.cols;
  183.  
  184. for (int i = 0; i < height; i++) {
  185. for (int j = 0; j < width; j++) {
  186. int grayColor = src.at<uchar>(i, j) + 20;
  187. if (grayColor > 255) {
  188. src.at<uchar>(i, j) = 255;
  189. }
  190. else {
  191. if (grayColor < 0)
  192. src.at<uchar>(i, j) = 0;
  193. else
  194. src.at<uchar>(i, j) = (uchar)grayColor;
  195. }
  196. }
  197. }
  198. imshow("grayLevelAdition", src);
  199. waitKey();
  200. }
  201. }
  202. void changeImageGrayMultiplication() {
  203. char frame[MAX_PATH];
  204. while (openFileDlg(frame)) {
  205. Mat_<uchar> src;
  206. src = imread(frame, CV_LOAD_IMAGE_GRAYSCALE);
  207. int height = src.rows;
  208. int width = src.cols;
  209.  
  210. for (int i = 0; i < height; i++) {
  211. for (int j = 0; j < width; j++) {
  212. int grayColor = src.at<uchar>(i, j) * 2;
  213. if (grayColor > 255) {
  214. src.at<uchar>(i, j) = 255;
  215. }
  216. else {
  217. if (grayColor < 0)
  218. src.at<uchar>(i, j) = 0;
  219. else
  220. src.at<uchar>(i, j) = (uchar)grayColor;
  221. }
  222. }
  223. }
  224. imshow("grayLevelAdition", src);
  225. imwrite("C:\\Users\\Public\\Pictures\\MultiplicationGray.bmp", src);
  226. waitKey();
  227. }
  228. }
  229.  
  230. void create256(){
  231. Mat img = Mat(256, 256, CV_8UC3);
  232.  
  233. for (int i = 0; i<256; i++)
  234. {
  235. for (int j = 0; j<256; j++)
  236. {
  237. if (i < 128 && j < 128){
  238. Vec3b v3 = img.at<Vec3b>(i, j);
  239. v3[0] = 255;
  240. v3[1] = 255;
  241. v3[2] = 255;
  242. img.at<Vec3b>(i, j) = v3;
  243. }
  244. if (i > 128 && j < 128){
  245. Vec3b v3 = img.at<Vec3b>(i, j);
  246. v3[0] = 0;
  247. v3[1] = 255;
  248. v3[2] = 0;
  249. img.at<Vec3b>(i, j) = v3;
  250. }
  251. if (i > 128 && j > 128){
  252. Vec3b v3 = img.at<Vec3b>(i, j);
  253. v3[0] = 0;
  254. v3[1] = 255;
  255. v3[2] = 255;
  256. img.at<Vec3b>(i, j) = v3;
  257. }
  258. if (i < 128 && j > 128){
  259. Vec3b v3 = img.at<Vec3b>(i, j);
  260. v3[0] = 0;
  261. v3[1] = 0;
  262. v3[2] = 255;
  263. img.at<Vec3b>(i, j) = v3;
  264. }
  265. }
  266. }
  267. imshow("image", img);
  268. waitKey();
  269. }
  270. void testBGR2HSV()
  271. {
  272. char fname[MAX_PATH];
  273. while (openFileDlg(fname))
  274. {
  275. Mat src = imread(fname);
  276. int height = src.rows;
  277. int width = src.cols;
  278.  
  279. // Componentele d eculoare ale modelului HSV
  280. Mat H = Mat(height, width, CV_8UC1);
  281. Mat S = Mat(height, width, CV_8UC1);
  282. Mat V = Mat(height, width, CV_8UC1);
  283.  
  284. // definire pointeri la matricele (8 biti/pixeli) folosite la afisarea componentelor individuale H,S,V
  285. uchar* lpH = H.data;
  286. uchar* lpS = S.data;
  287. uchar* lpV = V.data;
  288.  
  289. Mat hsvImg;
  290. cvtColor(src, hsvImg, CV_BGR2HSV);
  291.  
  292. // definire pointer la matricea (24 biti/pixeli) a imaginii HSV
  293. uchar* hsvDataPtr = hsvImg.data;
  294.  
  295. for (int i = 0; i<height; i++)
  296. {
  297. for (int j = 0; j<width; j++)
  298. {
  299. int hi = i*width * 3 + j * 3;
  300. int gi = i*width + j;
  301.  
  302. lpH[gi] = hsvDataPtr[hi] * 510 / 360; // lpH = 0 .. 255
  303. lpS[gi] = hsvDataPtr[hi + 1]; // lpS = 0 .. 255
  304. lpV[gi] = hsvDataPtr[hi + 2]; // lpV = 0 .. 255
  305. }
  306. }
  307.  
  308. imshow("input image", src);
  309. imshow("H", H);
  310. imshow("S", S);
  311. imshow("V", V);
  312.  
  313. waitKey();
  314. }
  315. }
  316.  
  317. void inverse(){
  318. Mat src(3, 3, CV_32FC1);
  319. for (int i = 0; i < 3; i++)
  320. for (int j = 0; j < 3; j++){
  321. src.at<float>(i, j) = i + j;
  322. }
  323. for (int i = 0; i < 3; i++){
  324. for (int j = 0; j < 3; j++){
  325. printf("%lf", src.at<float>(i, j));
  326. }
  327. printf("\n");
  328. }
  329. printf("Matrix Inverse:\n");
  330. Mat inverse = src.inv();
  331. for (int i = 0; i < 3; i++){
  332. for (int j = 0; j < 3; j++){
  333. printf("%lf", inverse.at<float>(i, j));
  334. }
  335. printf("\n");
  336. }
  337. waitKey();
  338.  
  339. }
  340.  
  341. void testResize()
  342. {
  343. char fname[MAX_PATH];
  344. while (openFileDlg(fname))
  345. {
  346. Mat src;
  347. src = imread(fname);
  348. Mat dst1, dst2;
  349. //without interpolation
  350. resizeImg(src, dst1, 320, false);
  351. //with interpolation
  352. resizeImg(src, dst2, 320, true);
  353. imshow("input image", src);
  354. imshow("resized image (without interpolation)", dst1);
  355. imshow("resized image (with interpolation)", dst2);
  356. waitKey();
  357. }
  358. }
  359.  
  360. void testCanny()
  361. {
  362. char fname[MAX_PATH];
  363. while (openFileDlg(fname))
  364. {
  365. Mat src, dst, gauss;
  366. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  367. double k = 0.4;
  368. int pH = 50;
  369. int pL = (int)k*pH;
  370. GaussianBlur(src, gauss, Size(5, 5), 0.8, 0.8);
  371. Canny(gauss, dst, pL, pH, 3);
  372. imshow("input image", src);
  373. imshow("canny", dst);
  374. waitKey();
  375. }
  376. }
  377.  
  378. void testVideoSequence()
  379. {
  380. VideoCapture cap("Videos/rubic.avi"); // off-line video from file
  381. //VideoCapture cap(0); // live video from web cam
  382. if (!cap.isOpened()) {
  383. printf("Cannot open video capture device.\n");
  384. waitKey(0);
  385. return;
  386. }
  387.  
  388. Mat edges;
  389. Mat frame;
  390. char c;
  391.  
  392. while (cap.read(frame))
  393. {
  394. Mat grayFrame;
  395. cvtColor(frame, grayFrame, CV_BGR2GRAY);
  396. Canny(grayFrame, edges, 40, 100, 3);
  397. imshow("source", frame);
  398. imshow("gray", grayFrame);
  399. imshow("edges", edges);
  400. c = cvWaitKey(0); // waits a key press to advance to the next frame
  401. if (c == 27) {
  402. // press ESC to exit
  403. printf("ESC pressed - capture finished\n");
  404. break; //ESC pressed
  405. };
  406. }
  407. }
  408.  
  409. void testSnap()
  410. {
  411. VideoCapture cap(0); // open the deafult camera (i.e. the built in web cam)
  412. if (!cap.isOpened()) // openenig the video device failed
  413. {
  414. printf("Cannot open video capture device.\n");
  415. return;
  416. }
  417.  
  418. Mat frame;
  419. char numberStr[256];
  420. char fileName[256];
  421.  
  422. // video resolution
  423. Size capS = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
  424. (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
  425.  
  426. // Display window
  427. const char* WIN_SRC = "Src"; //window for the source frame
  428. namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
  429. cvMoveWindow(WIN_SRC, 0, 0);
  430.  
  431. const char* WIN_DST = "Snapped"; //window for showing the snapped frame
  432. namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
  433. cvMoveWindow(WIN_DST, capS.width + 10, 0);
  434.  
  435. char c;
  436. int frameNum = -1;
  437. int frameCount = 0;
  438.  
  439. for (;;)
  440. {
  441. cap >> frame; // get a new frame from camera
  442. if (frame.empty())
  443. {
  444. printf("End of the video file\n");
  445. break;
  446. }
  447.  
  448. ++frameNum;
  449.  
  450. imshow(WIN_SRC, frame);
  451.  
  452. c = cvWaitKey(10); // waits a key press to advance to the next frame
  453. if (c == 27) {
  454. // press ESC to exit
  455. printf("ESC pressed - capture finished");
  456. break; //ESC pressed
  457. }
  458. if (c == 115){ //'s' pressed - snapp the image to a file
  459. frameCount++;
  460. fileName[0] = NULL;
  461. sprintf(numberStr, "%d", frameCount);
  462. strcat(fileName, "Images/A");
  463. strcat(fileName, numberStr);
  464. strcat(fileName, ".bmp");
  465. bool bSuccess = imwrite(fileName, frame);
  466. if (!bSuccess)
  467. {
  468. printf("Error writing the snapped image\n");
  469. }
  470. else
  471. imshow(WIN_DST, frame);
  472. }
  473. }
  474.  
  475. }
  476.  
  477. void MyCallBackFunc(int event, int x, int y, int flags, void* param)
  478. {
  479. //More examples: http://opencvexamples.blogspot.com/2014/01/detect-mouse-clicks-and-moves-on-image.html
  480. Mat* src = (Mat*)param;
  481. if (event == CV_EVENT_LBUTTONDOWN)
  482. {
  483. printf("Pos(x,y): %d,%d Color(RGB): %d,%d,%d\n",
  484. x, y,
  485. (int)(*src).at<Vec3b>(y, x)[2],
  486. (int)(*src).at<Vec3b>(y, x)[1],
  487. (int)(*src).at<Vec3b>(y, x)[0]);
  488. }
  489. }
  490.  
  491.  
  492. void testMouseClick()
  493. {
  494. Mat src;
  495. // Read image from file
  496. char fname[MAX_PATH];
  497. while (openFileDlg(fname))
  498. {
  499. src = imread(fname);
  500. //Create a window
  501. namedWindow("My Window", 1);
  502.  
  503. //set the callback function for any mouse event
  504. setMouseCallback("My Window", MyCallBackFunc, &src);
  505.  
  506. //show the image
  507. imshow("My Window", src);
  508.  
  509. // Wait until user press some key
  510. waitKey(0);
  511. }
  512. }
  513.  
  514. /* Histogram display function - display a histogram using bars (simlilar to L3 / PI)
  515. Input:
  516. name - destination (output) window name
  517. hist - pointer to the vector containing the histogram values
  518. hist_cols - no. of bins (elements) in the histogram = histogram image width
  519. hist_height - height of the histogram image
  520. Call example:
  521. showHistogram ("MyHist", hist_dir, 255, 200);
  522. */
  523.  
  524. void DrawCross(Mat& img, Point p, int size, Scalar color, int thickness){
  525.  
  526. line(img, Point(p.x - size / 2, p.y), Point(p.x + size / 2, p.y), color, thickness, 8);
  527. line(img, Point(p.x, p.y - size / 2), Point(p.x, p.y + size / 2), color, thickness, 8);
  528.  
  529. }
  530.  
  531. void findObj(int event, int x, int y, int flags, void* param){
  532.  
  533. Mat* src = (Mat*)param;
  534. if (event == CV_EVENT_LBUTTONDOWN)
  535. {
  536. Vec3b pixel = (*src).at<Vec3b>(y, x);
  537. int xc = 0;
  538. int yc = 0;
  539. int height = (*src).rows;
  540. int width = (*src).cols;
  541. Mat dst = Mat(height, width, CV_8UC3);
  542.  
  543. int *vertical = (int*)calloc(height, sizeof(int));
  544. int *horizontal = (int*)calloc(width, sizeof(int));
  545. int aria = 0;
  546. int perimeter = 0;
  547.  
  548. for (int i = 0; i < height; i++)
  549. for (int j = 0; j < width; j++)
  550. if (pixel == (*src).at<Vec3b>(i, j)){
  551. aria += 1, xc += j, yc += i, vertical[j]++, horizontal[i]++;
  552.  
  553. if (pixel != (*src).at<Vec3b>(i + 1, j) || pixel != (*src).at<Vec3b>(i + 1, j + 1) || pixel != (*src).at<Vec3b>(i + 1, j - 1) || pixel != (*src).at<Vec3b>(i, j + 1) || pixel != (*src).at<Vec3b>(i, j - 1) || pixel != (*src).at<Vec3b>(i - 1, j) || pixel != (*src).at<Vec3b>(i - 1, j - 1) || pixel != (*src).at<Vec3b>(i - 1, j - 1))
  554. dst.at<Vec3b>(i, j) = *(new Vec3b(0, 0, 0)), perimeter++;
  555. else
  556. dst.at<Vec3b>(i, j) = (*src).at<Vec3b>(i, j);
  557.  
  558. }
  559. else
  560. dst.at<Vec3b>(i, j) = (*src).at<Vec3b>(i, j);
  561.  
  562.  
  563.  
  564. xc /= aria;
  565. yc /= aria;
  566.  
  567. int numa = 0;
  568. int num1 = 0;
  569. int num2 = 0;
  570. for (int i = 0; i < height; i++)
  571. for (int j = 0; j < width; j++)
  572. {
  573. if (pixel == (*src).at<Vec3b>(i, j))
  574. numa += (i - yc)*(j - xc),
  575. num1 += (j - xc)*(j - xc),
  576. num2 += (i - yc)*(i - yc);
  577. }
  578.  
  579. float phi = atan2(2*(float)numa, (float)(num1 - num2)) / 2;
  580. int teta = phi*(180 / 3.14);
  581.  
  582. //Draw cross
  583. DrawCross(dst, Point(xc, yc), 20, Scalar(255, 255, 255), 2);
  584.  
  585. float thickness = 0;
  586. thickness = 4 * 3.14 *(aria / (perimeter*perimeter));
  587.  
  588. int delta = 30; // arbitrary value
  589. Point P1, P2;
  590. P1.x = xc - delta;
  591. P1.y = yc - (int)(delta*atan(teta)); // teta is the elongation angle in radians
  592. P2.x = xc + delta;
  593. P2.y = yc + (int)(delta*atan(teta));
  594. line(dst, P1, P2, Scalar(0, 0, 0), 1, 8);
  595.  
  596.  
  597. printf("Object Area %d\n", aria);
  598. printf("Center of mass %d %d\n", xc, yc);
  599. printf("Perimeter : %d\n", perimeter);
  600. printf("Teta %d\n", teta);
  601. printf("T= %.2f \n", thickness);
  602. imshow("Object", dst);
  603. waitKey(0);
  604. }
  605. }
  606. void findObjectArea()
  607. {
  608. Mat src;
  609. // Read image from file
  610. char fname[MAX_PATH];
  611. while (openFileDlg(fname))
  612. {
  613. src = imread(fname);
  614. //Create a window
  615. namedWindow("Window Area", 1);
  616.  
  617. //set the callback function for any mouse event
  618. setMouseCallback("Window Area", findObj, &src);
  619.  
  620. //show the image
  621. imshow("Window Area", src);
  622.  
  623. // Wait until user press some key
  624. waitKey(0);
  625. }
  626.  
  627. }
  628.  
  629. void genColor()
  630. {
  631. Mat src;
  632. // Read image from file
  633. char fname[MAX_PATH];
  634. while (openFileDlg(fname))
  635. {
  636. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  637. //Create a window
  638. namedWindow("My Window", 1);
  639.  
  640. int width = src.cols;
  641. int height = src.rows;
  642.  
  643. //generare paleta de culori
  644. Scalar colorLUT[1000] = { 0 };
  645. Scalar color;
  646. for (int i = 1; i < 1000; i++)
  647. {
  648. Scalar color(rand() & 255, rand() & 255, rand() & 255);
  649. colorLUT[i] = color;
  650.  
  651. }
  652. colorLUT[0] = Scalar(255, 255, 255); // fundalul va fi alb
  653.  
  654. Mat labels = Mat::zeros(src.size(), CV_16SC1); //matricea de etichete
  655. Mat dst = Mat::zeros(src.size(), CV_8UC3); //matricea destinatie pt. afisare
  656.  
  657. //printf("%d %d", labels.rows, labels.cols);
  658.  
  659. //Procesare (labeling) ….
  660. int label = 0;
  661. for (int i = 1; i < height - 1; i++)
  662. for (int j = 1; j < width - 1; j++)
  663. {
  664. if (src.at<uchar>(i, j) == 0 && labels.at<short>(i, j) == 0)
  665. {
  666. label++;
  667. short newLabel = label % 1000;
  668. labels.at<short>(i, j) = newLabel;
  669. queue<Point2i> Q;
  670. Q.push({ i, j });
  671. while (!Q.empty())
  672. {
  673. Point2i q = Q.front();
  674. Q.pop();
  675. for (int k = -1; k <= 1; k++)
  676. for (int m = -1; m <= 1; m++)
  677. {
  678. int x = q.x + k;
  679. int y = q.y + m;
  680. if (src.at<uchar>(x, y) == 0 && labels.at<short>(x, y) == 0)
  681. {
  682. labels.at<short>(x, y) = newLabel;
  683. Q.push({ x, y });
  684. }
  685. }
  686.  
  687. }
  688. }
  689. }
  690.  
  691. for (int i = 1; i < height - 1; i++)
  692. for (int j = 1; j < width - 1; j++)
  693. {
  694. Scalar color = colorLUT[labels.at<short>(i, j)]; // valabil pt. Met. 1 BFS
  695. dst.at<Vec3b>(i, j)[0] = (uchar)color[0];
  696. dst.at<Vec3b>(i, j)[1] = (uchar)color[1];
  697. dst.at<Vec3b>(i, j)[2] = (uchar)color[2];
  698. }
  699.  
  700. printf("%d", label);
  701. //show the image
  702. imshow("My Window", src);
  703. imshow("Label photo", dst);
  704.  
  705. // Wait until user press some key
  706. waitKey(0);
  707. }
  708.  
  709. }
  710.  
  711. void clickObject(){
  712.  
  713. char fname[MAX_PATH];
  714. while (openFileDlg(fname)){
  715.  
  716. Mat src = imread(fname);
  717. namedWindow("My Window", 1);
  718. setMouseCallback("My Window", findObj, &src);
  719. imshow("My Window", src);
  720. waitKey();
  721. }
  722. }
  723.  
  724. void convertRGBToGrayScale()
  725. {
  726. char fname[MAX_PATH];
  727. while (openFileDlg(fname))
  728. {
  729. Mat src;
  730. src = imread(fname);
  731.  
  732.  
  733. int height = src.rows;
  734. int width = src.cols;
  735.  
  736. Mat dst = Mat(height, width, CV_8UC1);
  737.  
  738. // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
  739. // Varianta ineficienta (lenta)
  740. for (int i = 0; i<height; i++)
  741. for (int j = 0; j<width; j++)
  742. {
  743. Vec3b v3 = src.at<Vec3b>(i, j);
  744. uchar b = v3[0];
  745. uchar g = v3[1];
  746. uchar r = v3[2];
  747. dst.at<uchar>(i, j) = (r + g + b) / 3;
  748. }
  749.  
  750. imshow("input image", src);
  751. imshow("converted image", dst);
  752. waitKey();
  753. }
  754. }
  755.  
  756. void convertGrayScaleToBlackAndWhite()
  757. {
  758. int n;
  759. printf("Please enter a number: \n");
  760. scanf("%d", &n);
  761. if (!(n <= 255 && n >= 0))
  762. {
  763. printf("Wrong number!");
  764. return;
  765. }
  766. char fname[MAX_PATH];
  767. while (openFileDlg(fname))
  768. {
  769. Mat src;
  770. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  771.  
  772.  
  773. int height = src.rows;
  774. int width = src.cols;
  775.  
  776. Mat dst = Mat(height, width, CV_8UC1);
  777.  
  778. // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
  779. // Varianta ineficienta (lenta)
  780. for (int i = 0; i<height; i++)
  781. for (int j = 0; j<width; j++)
  782. {
  783. uchar pixel = src.at<uchar>(i, j);
  784. if (pixel >= n)
  785. dst.at<uchar>(i, j) = 255;
  786. else
  787. dst.at<uchar>(i, j) = 0;
  788. }
  789.  
  790. imshow("input image", src);
  791. imshow("converted image", dst);
  792. waitKey();
  793. }
  794. }
  795.  
  796.  
  797. float my_max(float a, float b)
  798. {
  799. return (a > b ? a : b);
  800. }
  801.  
  802. float my_min(float a, float b)
  803. {
  804. return a < b ? a : b;
  805. }
  806.  
  807. void convertRGBToHSV()
  808. {
  809. char fname[MAX_PATH];
  810. while (openFileDlg(fname))
  811. {
  812. Mat src;
  813. src = imread(fname);
  814.  
  815.  
  816. int height = src.rows;
  817. int width = src.cols;
  818.  
  819. Mat dst = Mat(height, width, CV_8UC3);
  820. Mat h_mat = Mat(height, width, CV_8UC1);
  821. Mat s_mat = Mat(height, width, CV_8UC1);
  822. Mat v_mat = Mat(height, width, CV_8UC1);
  823.  
  824. // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
  825. // Varianta ineficienta (lenta)
  826. for (int i = 0; i<height; i++)
  827. for (int j = 0; j<width; j++)
  828. {
  829. Vec3b v3 = src.at<Vec3b>(i, j);
  830. float b = ((float)v3[0]) / 255;
  831. float g = ((float)v3[1]) / 255;
  832. float r = ((float)v3[2]) / 255;
  833.  
  834. float M = my_max(my_max(r, g), b);
  835. float m = my_min(my_min(r, g), b);
  836. float C = M - m;
  837.  
  838. //Value
  839. dst.at<Vec3b>(i, j)[2] = M * 255;
  840. v_mat.at<uchar>(i, j) = M * 255;
  841.  
  842. //Saturation
  843. if (M != 0)
  844. {
  845. dst.at<Vec3b>(i, j)[1] = (C / M) * 255;
  846. s_mat.at<uchar>(i, j) = (C / M) * 255;
  847. }
  848. else
  849. {
  850. dst.at<Vec3b>(i, j)[1] = 0;
  851. s_mat.at<uchar>(i, j) = 0;
  852. }
  853. //Hue
  854. float H;
  855. if (C != 0) {
  856. if (M == r) H = 60 * (g - b) / C;
  857. if (M == g) H = 120 + 60 * (b - r) / C;
  858. if (M == b) H = 240 + 60 * (r - g) / C;
  859. }
  860. else // grayscale
  861. H = 0;
  862. if (H < 0)
  863. H = H + 360;
  864. {
  865. dst.at<Vec3b>(i, j)[0] = H * 255 / 360;
  866. h_mat.at<uchar>(i, j) = H * 255 / 360;
  867. }
  868. }
  869.  
  870. imshow("input image", src);
  871. imshow("HSV image", dst);
  872. imshow("H image", h_mat);
  873. imshow("S image", s_mat);
  874. imshow("V image", v_mat);
  875. waitKey();
  876. }
  877. }
  878.  
  879. void showHistogram(const std::string& name, int* hist, const int hist_cols, const int hist_height)
  880. {
  881. Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); // constructs a white image
  882.  
  883. //computes histogram maximum
  884. int max_hist = 0;
  885. for (int i = 0; i<hist_cols; i++)
  886. if (hist[i] > max_hist)
  887. max_hist = hist[i];
  888. double scale = 1.0;
  889. scale = (double)hist_height / max_hist;
  890. int baseline = hist_height - 1;
  891.  
  892. for (int x = 0; x < hist_cols; x++) {
  893. Point p1 = Point(x, baseline);
  894. Point p2 = Point(x, baseline - cvRound(hist[x] * scale));
  895. line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
  896. }
  897.  
  898. imshow(name, imgHist);
  899. }
  900.  
  901. void HistogramCalc(){
  902.  
  903. int h[256] = { 0 };
  904.  
  905. char fname[MAX_PATH];
  906. while (openFileDlg(fname))
  907. {
  908. Mat src;
  909. src = imread(fname);
  910.  
  911. int height = src.rows;
  912. int width = src.cols;
  913.  
  914. for (int i = 0; i < height; i++)
  915. for (int j = 0; j < width; j++)
  916. {
  917. int g = (int) src.at<uchar>(i, j);
  918. h[g]++;
  919. }
  920.  
  921. imshow("input image", src);
  922. showHistogram("Histogram", h, 256, 150);
  923. waitKey();
  924. }
  925. }
  926.  
  927. void FDP(){
  928.  
  929. int h[256] = { 0 };
  930. float p[256] = { 0.0 };
  931. int hf[256] = { 0 };
  932. int vec[256] = { 0 };
  933.  
  934. char fname[MAX_PATH];
  935. while (openFileDlg(fname))
  936. {
  937. Mat src;
  938. src = imread(fname);
  939.  
  940. int height = src.rows;
  941. int width = src.cols;
  942. int M = height*width;
  943.  
  944.  
  945. for (int i = 0; i < height; i++)
  946. for (int j = 0; j < width; j++)
  947. {
  948. int g = (int)src.at<uchar>(i, j);
  949. h[g]++;
  950. }
  951.  
  952. int wh = 5;
  953. float th = 0.0003;
  954.  
  955. for (int i = wh; i < 255 - wh; i++){
  956. float v = 0.0;
  957. for (int j = -wh; j <= wh; j++)
  958. v += h[i + j];
  959. hf[i] = v / (2 * wh + 1);
  960. }
  961.  
  962. for (int i = wh; i < 255 - wh; i++){
  963. float v = 0.0;
  964. for (int j = -wh; j <= wh; j++)
  965. v += hf[i + j];
  966. h[i] = v / (2 * wh + 1);
  967. }
  968.  
  969. for (int i = 0; i < 255; i++)
  970. {
  971. p[i] = (float)h[i] / M;
  972. }
  973.  
  974. int nr = 0;
  975. for (int i = wh; i < 255 - wh; i++){
  976.  
  977. float v = 0.0;
  978. bool max = true;
  979. for (int j = i-wh; j < i+wh; j++){
  980. if (p[i]<p[j]){
  981. max = false;
  982. break;
  983. }
  984. v += p[i];
  985. }
  986.  
  987. v = v / (2 * wh + 1);
  988.  
  989. if (max && p[i] > (v + th)){
  990. vec[nr] = i;
  991. nr++;
  992. }
  993. }
  994.  
  995. imshow("input image", src);
  996. printf("Numar varfuri: %d ", nr);
  997. for (int i = 0; i < nr; i++){
  998. printf("%d ", vec[i]);
  999. }
  1000. printf("\n");
  1001. showHistogram("Histogram", h, 256, 200);
  1002. waitKey();
  1003. }
  1004. }
  1005.  
  1006. bool is_Inside(Mat img, int i, int j)
  1007. {
  1008. int height = img.rows;
  1009. int width = img.cols;
  1010.  
  1011. //Check rows
  1012. if (!(i >= 0 && i < height))
  1013. return FALSE;
  1014.  
  1015. //Check columns
  1016. if (!(j >= 0 && j < width))
  1017. return FALSE;
  1018.  
  1019. return TRUE;
  1020. }
  1021.  
  1022. int main()
  1023. {
  1024. int op;
  1025. do
  1026. {
  1027. //system("cls");
  1028. destroyAllWindows();
  1029. printf("Menu:\n");
  1030. printf(" 1 - Open image\n");
  1031. printf(" 2 - Open BMP images from folder\n");
  1032. printf(" 3 - Image negative - diblook style\n");
  1033. printf(" 4 - BGR->HSV\n");
  1034. printf(" 5 - Resize image\n");
  1035. printf(" 6 - Canny edge detection\n");
  1036. printf(" 7 - Edges in a video sequence\n");
  1037. printf(" 8 - Snap frame from live video\n");
  1038. printf(" 9 - Mouse callback demo\n");
  1039. printf(" 10 - Negative image\n");
  1040. printf(" 11 - Gray factor aditiv \n");
  1041. printf(" 12 - Gray factor multiplicativ \n");
  1042. printf(" 13 - Create 256x256 \n");
  1043. printf(" 14 - Inverse matrice 3x3 \n");
  1044. printf(" 15 - Convert RGB to GrayScale\n");
  1045. printf(" 16 - Convert GrayScale to Black and White\n");
  1046. printf(" 17 - Convert RGB to HSV\n");
  1047. printf(" 18 - Calcul Histograma\n");
  1048. printf(" 19 - FDP\n");
  1049. printf(" 20 - Object Click\n");
  1050. printf(" 21 - Color object\n");
  1051. printf(" 0 - Exit\n\n");
  1052. printf("Option: ");
  1053. scanf("%d", &op);
  1054. switch (op)
  1055. {
  1056. case 1:
  1057. testOpenImage();
  1058. break;
  1059. case 2:
  1060. testOpenImagesFld();
  1061. break;
  1062. case 3:
  1063. testParcurgereSimplaDiblookStyle(); //diblook style
  1064. break;
  1065. case 4:
  1066. //testColor2Gray();
  1067. testBGR2HSV();
  1068. break;
  1069. case 5:
  1070. testResize();
  1071. break;
  1072. case 6:
  1073. testCanny();
  1074. break;
  1075. case 7:
  1076. testVideoSequence();
  1077. break;
  1078. case 8:
  1079. testSnap();
  1080. break;
  1081. case 9:
  1082. testMouseClick();
  1083. break;
  1084. case 10:
  1085. testNegativeImage();
  1086. break;
  1087. case 11:
  1088. changeImageGray();
  1089. break;
  1090. case 12:
  1091. changeImageGrayMultiplication();
  1092. break;
  1093. case 13:
  1094. create256();
  1095. break;
  1096. case 14:
  1097. inverse();
  1098. break;
  1099. case 15:
  1100. convertRGBToGrayScale();
  1101. break;
  1102. case 16:
  1103. convertGrayScaleToBlackAndWhite();
  1104. break;
  1105. case 17:
  1106. convertRGBToHSV();
  1107. break;
  1108. case 18:
  1109. HistogramCalc();
  1110. break;
  1111. case 19:
  1112. FDP();
  1113. break;
  1114. case 20:
  1115. clickObject();
  1116. break;
  1117. case 21:
  1118. genColor();
  1119. break;
  1120. }
  1121. } while (op != 0);
  1122. return 0;
  1123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement