Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.33 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.  
  8. void testOpenImage()
  9. {
  10. char fname[MAX_PATH];
  11. while (openFileDlg(fname))
  12. {
  13. Mat src;
  14. src = imread(fname);
  15. imshow("image", src);
  16. waitKey();
  17. }
  18. }
  19.  
  20. void testOpenImagesFld()
  21. {
  22. char folderName[MAX_PATH];
  23. if (openFolderDlg(folderName) == 0)
  24. return;
  25. char fname[MAX_PATH];
  26. FileGetter fg(folderName, "bmp");
  27. while (fg.getNextAbsFile(fname))
  28. {
  29. Mat src;
  30. src = imread(fname);
  31. imshow(fg.getFoundFileName(), src);
  32. if (waitKey() == 27) //ESC pressed
  33. break;
  34. }
  35. }
  36.  
  37.  
  38. void testResize()
  39. {
  40. char fname[MAX_PATH];
  41. while (openFileDlg(fname))
  42. {
  43. Mat src;
  44. src = imread(fname);
  45. Mat dst1, dst2;
  46. //without interpolation
  47. resizeImg(src, dst1, 320, false);
  48. //with interpolation
  49. resizeImg(src, dst2, 320, true);
  50. imshow("input image", src);
  51. imshow("resized image (without interpolation)", dst1);
  52. imshow("resized image (with interpolation)", dst2);
  53. waitKey();
  54. }
  55. }
  56.  
  57.  
  58. void testVideoSequence()
  59. {
  60. VideoCapture cap("Videos/rubic.avi"); // off-line video from file
  61. //VideoCapture cap(0); // live video from web cam
  62. if (!cap.isOpened()) {
  63. printf("Cannot open video capture device.\n");
  64. waitKey(0);
  65. return;
  66. }
  67.  
  68. Mat edges;
  69. Mat frame;
  70. char c;
  71.  
  72. while (cap.read(frame))
  73. {
  74. Mat grayFrame;
  75. cvtColor(frame, grayFrame, CV_BGR2GRAY);
  76. imshow("source", frame);
  77. imshow("gray", grayFrame);
  78. c = cvWaitKey(0); // waits a key press to advance to the next frame
  79. if (c == 27) {
  80. // press ESC to exit
  81. printf("ESC pressed - capture finished\n");
  82. break; //ESC pressed
  83. };
  84. }
  85. }
  86.  
  87.  
  88. void testSnap()
  89. {
  90. VideoCapture cap(0); // open the deafult camera (i.e. the built in web cam)
  91. if (!cap.isOpened()) // openenig the video device failed
  92. {
  93. printf("Cannot open video capture device.\n");
  94. return;
  95. }
  96.  
  97. Mat frame;
  98. char numberStr[256];
  99. char fileName[256];
  100.  
  101. // video resolution
  102. Size capS = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
  103. (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
  104.  
  105. // Display window
  106. const char* WIN_SRC = "Src"; //window for the source frame
  107. namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
  108. cvMoveWindow(WIN_SRC, 0, 0);
  109.  
  110. const char* WIN_DST = "Snapped"; //window for showing the snapped frame
  111. namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
  112. cvMoveWindow(WIN_DST, capS.width + 10, 0);
  113.  
  114. char c;
  115. int frameNum = -1;
  116. int frameCount = 0;
  117.  
  118. for (;;)
  119. {
  120. cap >> frame; // get a new frame from camera
  121. if (frame.empty())
  122. {
  123. printf("End of the video file\n");
  124. break;
  125. }
  126.  
  127. ++frameNum;
  128.  
  129. imshow(WIN_SRC, frame);
  130. Mat H(frame.rows, frame.cols, CV_8UC1);
  131. Mat HSV;
  132. cvtColor(frame, HSV, CV_BGR2HSV);
  133. uchar h;
  134. for (int i = 0; i < HSV.rows; i++){
  135. for (int j = 0; j < HSV.cols; j++){
  136. h = HSV.at<Vec3b>(i, j)[0];
  137. if (h < 30 && h>4){
  138. h = 255;
  139. }
  140. else {
  141. h = 0;
  142. }
  143. H.at<uchar>(i, j) = h;
  144. }
  145. }
  146. imshow("CanalH", H);
  147.  
  148.  
  149. c = cvWaitKey(10); // waits a key press to advance to the next frame
  150. if (c == 27) {
  151. // press ESC to exit
  152. printf("ESC pressed - capture finished");
  153. break; //ESC pressed
  154. }
  155. if (c == 115){ //'s' pressed - snapp the image to a file
  156. frameCount++;
  157. fileName[0] = NULL;
  158. sprintf(numberStr, "%d", frameCount);
  159. strcat(fileName, "Images/A");
  160. strcat(fileName, numberStr);
  161. strcat(fileName, ".bmp");
  162. bool bSuccess = imwrite(fileName, frame);
  163. if (!bSuccess)
  164. {
  165. printf("Error writing the snapped image\n");
  166. }
  167. else
  168. imshow(WIN_DST, frame);
  169. }
  170. }
  171.  
  172. }
  173.  
  174. void MyCallBackFunc(int event, int x, int y, int flags, void* param)
  175. {
  176. //More examples: http://opencvexamples.blogspot.com/2014/01/detect-mouse-clicks-and-moves-on-image.html
  177. Mat* src = (Mat*)param;
  178. if (event == CV_EVENT_LBUTTONDOWN)
  179. {
  180. printf("Pos(x,y): %d,%d Color(RGB): %d,%d,%d\n",
  181. x, y,
  182. (int)(*src).at<Vec3b>(y, x)[2],
  183. (int)(*src).at<Vec3b>(y, x)[1],
  184. (int)(*src).at<Vec3b>(y, x)[0]);
  185. }
  186. }
  187.  
  188. void testMouseClick()
  189. {
  190. Mat src;
  191. // Read image from file
  192. char fname[MAX_PATH];
  193. while (openFileDlg(fname))
  194. {
  195. src = imread(fname);
  196. //Create a window
  197. namedWindow("My Window", 1);
  198.  
  199. //set the callback function for any mouse event
  200. setMouseCallback("My Window", MyCallBackFunc, &src);
  201.  
  202. //show the image
  203. imshow("My Window", src);
  204.  
  205. // Wait until user press some key
  206. waitKey(0);
  207. }
  208. }
  209.  
  210. void negative_image(){
  211. Mat img = imread("Images/cameraman.bmp",
  212. CV_LOAD_IMAGE_GRAYSCALE);
  213. for (int i = 0; i<img.rows; i++){
  214. for (int j = 0; j<img.cols; j++){
  215. img.at<uchar>(i, j) = 255 - img.at<uchar>(i, j);
  216. }
  217. }
  218. imshow("negative image", img);
  219. waitKey(0);
  220. }
  221.  
  222. void change_aditiv(){
  223. Mat img = imread("Images/cameraman.bmp",
  224. CV_LOAD_IMAGE_GRAYSCALE);
  225. for (int i = 0; i<img.rows; i++){
  226. for (int j = 0; j<img.cols; j++){
  227. if (img.at<uchar>(i, j) < 225){
  228. img.at<uchar>(i, j) = 30 + img.at<uchar>(i, j);
  229. }
  230. }
  231. }
  232. imshow("negative image", img);
  233. waitKey(0);
  234. }
  235.  
  236. void change_multiplicativ(){
  237. Mat img = imread("Images/cameraman.bmp",
  238. CV_LOAD_IMAGE_GRAYSCALE);
  239. for (int i = 0; i < img.rows; i++){
  240. for (int j = 0; j < img.cols; j++){
  241. img.at<uchar>(i, j) = 4 * img.at<uchar>(i, j);
  242. if (img.at<uchar>(i, j) > 255) {
  243. img.at<uchar>(i, j) = 255;
  244. }
  245. }
  246. }
  247. imshow("negative image", img);
  248. imwrite("D:\\Student\\IliesAlinaDenisa30233\\OpenCVApplication-VS2013_2413_basic\\Images\\img1.bmp", img);
  249. waitKey(0);
  250. }
  251.  
  252. void creare_imagine(){
  253. Mat img(256, 256, CV_8UC3);
  254. for (int i = 0; i < img.rows / 2; i++){
  255. for (int j = 0; j < img.cols / 2; j++){
  256. img.at<Vec3b>(i, j) = { 255, 255, 255 };
  257. }
  258. }
  259. for (int i = 0; i < img.rows / 2; i++){
  260. for (int j = img.cols / 2; j < img.rows; j++){
  261. img.at<Vec3b>(i, j) = { 0, 0, 255 };
  262. }
  263. }
  264. for (int i = img.rows / 2; i < img.rows; i++){
  265. for (int j = 0; j < img.cols / 2; j++){
  266. img.at<Vec3b>(i, j) = { 0, 255, 0 };
  267. }
  268. }
  269. for (int i = img.rows / 2; i<img.rows; i++){
  270. for (int j = img.cols / 2; j < img.cols; j++){
  271. img.at<Vec3b>(i, j) = { 0, 255, 255 };
  272. }
  273. }
  274. imshow("firstImg", img);
  275. waitKey(0);
  276. }
  277.  
  278. void matrice(){
  279. float vals[9] = { -1, 2, 3, 4, -1, 6, 7, 8, -1 };
  280. Mat M(3, 3, CV_32FC1, vals);
  281. Mat Minversata = M.inv();
  282. std::cout << Minversata << std::endl;
  283. waitKey(0);
  284.  
  285. }
  286.  
  287. void copiere_canale(){
  288. Mat img = imread("Images/Lena_24bits.bmp", 1);
  289. Mat mat1(img.rows, img.cols, CV_8UC1);
  290. Mat mat2(img.rows, img.cols, CV_8UC1);
  291. Mat mat3(img.rows, img.cols, CV_8UC1);
  292. for (int i = 0; i < img.rows; i++){
  293. for (int j = 0; j < img.cols; j++){
  294. Vec3b pixel = img.at<Vec3b>(i, j);
  295. unsigned char B = pixel[0];
  296. unsigned char G = pixel[1];
  297. unsigned char R = pixel[2];
  298. mat1.at<uchar>(i, j) = R;
  299. mat2.at<uchar>(i, j) = G;
  300. mat3.at<uchar>(i, j) = B;
  301. }
  302. }
  303. imshow("Red", mat1);
  304. imshow("Green", mat2);
  305. imshow("Blue", mat3);
  306. waitKey(0);
  307. }
  308.  
  309. void conversie() {
  310. Mat img = imread("Images/Lena_24bits.bmp", 1);
  311. Mat img1(img.rows, img.cols, CV_8UC1);
  312. for (int i = 0; i < img.rows; i++){
  313. for (int j = 0; j < img.cols; j++){
  314. Vec3b pixel = img.at<Vec3b>(i, j);
  315. unsigned char B = pixel[0];
  316. unsigned char G = pixel[1];
  317. unsigned char R = pixel[2];
  318. img1.at<uchar>(i, j) = (B + G + R) / 3;
  319. }
  320. }
  321. imshow("ImagineSursa", img);
  322. imshow("ImagineDestinatie", img1);
  323. waitKey(0);
  324. }
  325.  
  326. void conversiegray(){
  327. int prag;
  328. Mat img = imread("Images/eight.bmp", CV_LOAD_IMAGE_GRAYSCALE);
  329. Mat img1(img.rows, img.cols, CV_8UC1);
  330. printf("Dati pragul : ");
  331. scanf("%d", &prag);
  332. for (int i = 0; i < img.rows; i++){
  333. for (int j = 0; j < img.cols; j++){
  334. uchar pixel = img.at<uchar>(i, j);
  335. if (pixel < prag) {
  336. img1.at<uchar>(i, j) = 0;
  337. }
  338. else if (pixel >= prag)
  339. {
  340. img1.at<uchar>(i, j) = 255;
  341. }
  342. }
  343. }
  344. imshow("ImagineSursa", img);
  345. imshow("ImagineDest", img1);
  346. waitKey(0);
  347. }
  348. float max1(float a, float b, float c){
  349. float z = max(a, b);
  350. float maxim = max(z, c);
  351. return maxim;
  352. }
  353. float min1(float a, float b, float c){
  354. float z = min(a, b);
  355. float minim = min(z, c);
  356. return minim;
  357. }
  358. void convertireCanale(){
  359. Mat img = imread("Images/Lena_24bits.bmp", 1);
  360. Mat HN(img.rows, img.cols, CV_8UC1);
  361. Mat SN(img.rows, img.cols, CV_8UC1);
  362. Mat VN(img.rows, img.cols, CV_8UC1);
  363. float H = 0, S, V;
  364. for (int i = 0; i < img.rows; i++){
  365. for (int j = 0; j < img.cols; j++){
  366. Vec3b pixel = img.at<Vec3b>(i, j);
  367. uchar B = pixel[0];
  368. uchar G = pixel[1];
  369. uchar R = pixel[2];
  370. float b = (float)B / 255;
  371. float g = (float)G / 255;
  372. float r = (float)R / 255;
  373.  
  374. float M = max1(b, g, r);
  375. float m = min1(b, g, r);
  376. float C = M - m;
  377. float V = M;
  378.  
  379. //Saturation
  380. if (C) {
  381. S = C / V;
  382. }
  383. else {
  384. S = 0;
  385. }
  386. //Hue
  387. if (C){
  388. if (M == r) H = 60 * (g - b) / C;
  389. if (M == g) H = 120 + 60 * (b - r) / C;
  390. if (M == b) H = 240 + 60 * (r - g) / C;
  391. }
  392. else {
  393. H = 0;
  394. }
  395. if (H < 0){
  396. H = H + 360;
  397. }
  398. HN.at<uchar>(i, j) = H * 255 / 360;
  399. SN.at<uchar>(i, j) = S * 255;
  400. VN.at<uchar>(i, j) = V * 255;
  401. }
  402. }
  403. imshow("ImagineSursa", img);
  404. imshow("H", HN);
  405. imshow("S", SN);
  406. imshow("v", VN);
  407. waitKey(0);
  408.  
  409. }
  410.  
  411.  
  412. /* Histogram display function - display a histogram using bars (simlilar to L3 / PI)
  413. Input:
  414. name - destination (output) window name
  415. hist - pointer to the vector containing the histogram values
  416. hist_cols - no. of bins (elements) in the histogram = histogram image width
  417. hist_height - height of the histogram image
  418. Call example:
  419. showHistogram ("MyHist", hist_dir, 255, 200);
  420. */
  421. void showHistogram(const string& name, int* hist, const int hist_cols, const int hist_height)
  422. {
  423. Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); // constructs a white image
  424.  
  425. //computes histogram maximum
  426. int max_hist = 0;
  427. for (int i = 0; i<hist_cols; i++)
  428. if (hist[i] > max_hist)
  429. max_hist = hist[i];
  430. double scale = 1.0;
  431. scale = (double)hist_height / max_hist;
  432. int baseline = hist_height - 1;
  433.  
  434. for (int x = 0; x < hist_cols; x++) {
  435. Point p1 = Point(x, baseline);
  436. Point p2 = Point(x, baseline - cvRound(hist[x] * scale));
  437. line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
  438. }
  439.  
  440. imshow(name, imgHist);
  441. }
  442.  
  443. void computeHist(Mat img, int* &hist, float* &pdf){
  444. hist = new int[256]{};
  445. for (int i = 0; i < img.rows; i++){
  446. for (int j = 0; j < img.cols; j++){
  447. hist[img.at<uchar>(i, j)]++;
  448. }
  449. }
  450. pdf = new float[256]{};
  451. for (int i = 0; i < 256; i++){
  452. pdf[i] = hist[i] * 1.0 / (img.rows*img.cols);
  453. }
  454. }
  455.  
  456. void computeHist1(Mat img, int* &hist, float* pdf, int m){
  457. hist = new int[m]{};
  458. for (int i = 0; i < img.rows; i++){
  459. for (int j = 0; j < img.cols; j++){
  460. hist[(int)(img.at<uchar>(i, j) / (256.0 / m))]++;
  461.  
  462. }
  463. }
  464. pdf = new float[m]{};
  465. for (int i = 0; i < m; i++){
  466. pdf[i] = hist[i] * 1.0 / (img.rows*img.cols);
  467. }
  468. }
  469.  
  470.  
  471. Mat multilevel(Mat img)
  472. {
  473. img = imread("Images/cameraman.bmp", CV_LOAD_IMAGE_GRAYSCALE);
  474. int WH = 5;
  475. int *hist;
  476. float *pdf = new float[256];
  477. int s = 0;
  478. int x = 0;
  479. float TH = 0.0003;
  480. float avg;
  481. computeHist(img, hist, pdf);
  482. std::vector <int> maxime;
  483. maxime.push_back(0);
  484. for (int k = 0 + WH; k < 255 - WH; k++)
  485. {
  486. int ok = 1;
  487. s = 0;
  488. for (int l = k - WH; l < k + WH; l++)
  489. {
  490. s = s + pdf[l];
  491. x++;
  492. if (pdf[k] < pdf[l]) ok = 0;
  493.  
  494. }
  495. avg = s / x;
  496. if (pdf[k] > avg + TH && ok == 1)
  497. maxime.push_back(k);
  498. }
  499. printf("Nr maxime \n");
  500. std::cout << maxime.size() << "\n";
  501. int histMax[256]{};
  502. for (int i = 0; i < maxime.size(); i++)
  503. histMax[maxime[i]] = hist[maxime[i]];
  504. showHistogram("Histograma", hist, 256, 256);
  505. showHistogram("Histograma1", histMax, 256, 256);
  506. waitKey(0);
  507.  
  508. for (int i = 0; i < img.rows; i++){
  509. for (int j = 0; j < img.cols; j++){
  510. int min = 1000000;
  511. int val = 0;
  512. for (int k = 0; k < maxime.size(); k++)
  513. {
  514. if ((abs(maxime[k] - img.at<uchar>(i, j))) < min) {
  515. min = abs(maxime[k] - img.at<uchar>(i, j));
  516. val = maxime[k];
  517. }
  518. }
  519.  
  520. img.at<uchar>(i, j) = val;
  521. }
  522. }
  523. return img;
  524. }
  525.  
  526. Mat calcInd(Mat img){
  527. Mat binPict(img.rows, img.cols, CV_8UC1);
  528.  
  529. for (int i = 0; i < img.rows; i++)
  530. {
  531. for (int j = 0; j < img.cols; j++)
  532. {
  533. if (img.at<unsigned char>(i, j) == 255)
  534. {
  535. binPict.at<unsigned char>(i, j) = 0;
  536. }
  537. else
  538. {
  539. binPict.at<unsigned char>(i, j) = 1;
  540. }
  541. }
  542. }
  543.  
  544. return binPict;
  545. }
  546.  
  547. void probleme(){
  548. int arie = 0;
  549. Mat img = imread("Images/oval_obl.bmp", CV_LOAD_IMAGE_GRAYSCALE);
  550. Mat src = calcInd(img);
  551. int aria = 0;
  552. for (int i = 0; i < src.rows; i++)
  553. {
  554. for (int j = 0; j < src.cols; j++)
  555. {
  556. if (src.at<unsigned char>(i, j) == 1)
  557. {
  558. aria++;
  559. }
  560. }
  561. }
  562. printf("Aria = %d \n", aria);
  563.  
  564.  
  565. ////////////////////////////////////////////////////////////////
  566. int xGr = 0, yGr = 0;
  567. for (int i = 0; i < src.rows; i++)
  568. {
  569. for (int j = 0; j < src.cols; j++)
  570. {
  571. xGr += i*src.at<unsigned char>(i, j);
  572. yGr += j*src.at<unsigned char>(i, j);
  573. }
  574. }
  575. xGr = xGr / aria;
  576. yGr = yGr / aria;
  577. printf("Centru de greutate X=%d Y=%d \n", xGr, yGr);
  578. img.at<uchar>(xGr, yGr) = 255;
  579. img.at<uchar>(xGr - 1, yGr) = 255;
  580. img.at<uchar>(xGr + 1, yGr) = 255;
  581. img.at<uchar>(xGr, yGr - 1) = 255;
  582. img.at<uchar>(xGr, yGr + 1) = 255;
  583. img.at<uchar>(xGr - 2, yGr) = 255;
  584. img.at<uchar>(xGr + 2, yGr) = 255;
  585. img.at<uchar>(xGr, yGr - 2) = 255;
  586. img.at<uchar>(xGr, yGr + 2) = 255;
  587. imshow("img", img);
  588. waitKey(0);
  589. /////////////////////////////////////////////////////////////////////
  590. double numarator = 0.0;
  591. double numitor1 = 0.0;
  592. double numitor2 = 0.0;
  593. double numitor = 0.0;
  594. float fi = 0.0;
  595.  
  596. for (int i = 0; i < src.rows; i++){
  597. for (int j = 0; j < src.cols; j++){
  598. numarator += ((i - xGr)*(j - yGr)*src.at<unsigned char>(i, j));
  599. }
  600. }
  601. numarator *= 2.0;
  602. for (int i = 0; i < src.rows; i++){
  603. for (int j = 0; j < src.cols; j++){
  604. numitor1 += ((j - yGr)*(j - yGr)*src.at<unsigned char>(i, j));
  605. }
  606. }
  607. for (int i = 0; i < src.rows; i++){
  608. for (int j = 0; j < src.cols; j++){
  609. numitor2 += ((i - xGr)*(i - xGr)*src.at<unsigned char>(i, j));
  610. }
  611. }
  612. numitor = numitor1 - numitor2;
  613. float a = atan2(numarator, numitor);
  614. printf("Axa alungire= %f \n", a);
  615. fi = 0.5*a;
  616. Point p2(yGr+ 100 * cos(fi),xGr + 100 * sin(fi));
  617. Point p1(yGr - 100 * cos(fi), xGr - 100 * sin(fi));
  618. line(img, p1, p2, 1, 1);
  619. imshow("img1", img);
  620. waitKey(0);
  621. /////////////////////////////////////////////////////////////////////////
  622. int perimetru = 0;
  623. for (int i = 0; i < src.rows; i++)
  624. {
  625. for (int j = 0; j < src.cols; j++)
  626. {
  627. if (src.at<unsigned char>(i, j) == 1)
  628. {
  629. if (src.at<unsigned char>(i - 1, j) == 0 ||
  630. src.at<unsigned char>(i + 1, j) == 0 ||
  631. src.at<unsigned char>(i, j - 1) == 0 ||
  632. src.at<unsigned char>(i, j + 1) == 0)
  633. {
  634. perimetru = perimetru + 1;
  635. img.at<uchar>(i, j) = 150;
  636. }
  637. }
  638. }
  639. }
  640. printf("Perimetrul este = %d \n", perimetru);
  641. imshow("Contur perimetru", img);
  642. waitKey(0);
  643. /////////////////////////////////////////////////////////////
  644. float factorSubtiere = 0.0;
  645. factorSubtiere = 4 * 3.14*aria / (perimetru*perimetru);
  646. printf("Factorul de subtiere= %f \n", factorSubtiere);
  647. waitKey(0);
  648. /////////////////////////////////////////////////////////////
  649. int cMin = src.cols;
  650. int cMax = 0;
  651. int rMin = src.rows;
  652. int rMax = 0;
  653.  
  654. float elongantia = 0.0;
  655. for (int i = 0; i < src.rows; i++){
  656. for (int j = 0; j < src.cols; j++){
  657. if (src.at<unsigned char>(i, j) == 1){
  658. if (src.at<unsigned char>(i - 1, j) == 0 ||
  659. src.at<unsigned char>(i + 1, j) == 0 ||
  660. src.at<unsigned char>(i, j - 1) == 0 ||
  661. src.at<unsigned char>(i, j + 1) == 0)
  662. {
  663. if (i > rMax){ rMax = i;}
  664. if (i < rMin){ rMin = i;}
  665. if (j > cMax){ cMax = j;}
  666. if (j < cMin){ cMin = j;}
  667. }
  668. }
  669. }
  670. }
  671. elongantia = ((float)(cMax - cMin + 1) / (rMax - rMin + 1));
  672. printf("Elongatia este = %f \n", elongantia);
  673. waitKey(0);
  674.  
  675. ////////////////////////////////////////////////////////
  676. Mat img1 = Mat::zeros(src.rows, src.cols, CV_8UC1);
  677. //rows
  678. int sum = 0;
  679. for (int i = 0; i < src.rows; i++){
  680. sum = 0;
  681. for (int j = 0; j < src.cols; j++){
  682. if (src.at<unsigned char>(i, j) != 0){
  683. sum += 1;
  684. }
  685. }
  686. for (int j = 0; j < sum; j++){
  687. img1.at<unsigned char>(i, j) = 255;
  688. }
  689. }
  690. Mat img2 = Mat::zeros(src.rows, src.cols, CV_8UC1);
  691. //cols
  692. sum = 0;
  693. for (int i = 0; i < src.cols; i++){
  694. sum = 0;
  695. for (int j = 0; j < src.rows; j++){
  696. if (src.at<unsigned char>(j, i) != 0){
  697. sum += 1;
  698. }
  699. }
  700. for (int j = 0; j < sum; j++){
  701. img2.at<unsigned char>(j, i) = 255;
  702. }
  703. }
  704.  
  705. imshow("Randuri", img1);
  706. waitKey(0);
  707. imshow("Coloane", img2);
  708. waitKey(0);
  709. Point rMinim(cMin,rMin);
  710. Point rMaxim(cMin,rMax);
  711. Point cMaxim1(cMax,rMax );
  712. Point cMaxim2(cMax,rMin );
  713. line(img, rMinim, rMaxim, 1, 1);
  714. line(img, rMaxim, cMaxim1, 1, 1);
  715. line(img,cMaxim1 ,cMaxim2 , 1, 1);
  716. line(img, cMaxim2,rMinim , 1, 1);
  717. imshow("dreptunghi", img);
  718. waitKey(0);
  719. }
  720.  
  721.  
  722. int main()
  723. {
  724.  
  725. Mat img = imread("Images/Cameraman.bmp", CV_LOAD_IMAGE_GRAYSCALE);
  726. Mat res(img.rows, img.cols, CV_8UC1);
  727. float* pdf = new float[256];
  728. int* hist;
  729. int op;
  730. int m = 256 / 127;
  731. do
  732. {
  733. // system("cls");
  734. destroyAllWindows();
  735. printf("Menu:\n");
  736. printf(" 1 - Open image\n");
  737. printf(" 2 - Open BMP images from folder\n");
  738. printf(" 3 - Resize image\n");
  739. printf(" 4 - Process video\n");
  740. printf(" 5 - Snap frame from live video\n");
  741. printf(" 6 - Mouse callback demo\n");
  742. printf(" 7 - Negative image\n");
  743. printf(" 8 - Change-aditiv\n");
  744. printf(" 9 - Change-multiplicativ\n");
  745. printf(" 10 - Creare imagine \n");
  746. printf(" 11 - Matrice \n");
  747. printf("12 - Copiere canale \n");
  748. printf("13 - Conversie \n");
  749. printf("14 - ConversieGrey \n");
  750. printf("15 -ConvertireCanale \n");
  751. printf("16 -Histrograma \n");
  752. printf("17 -Acumulatoare \n");
  753. printf("18 -Multilevel \n");
  754.  
  755. printf("19 -Probleme \n");
  756.  
  757. printf(" 0 - Exit\n\n");
  758. printf("Option: ");
  759. scanf("%d", &op);
  760. switch (op)
  761. {
  762. case 1:
  763. testOpenImage();
  764. break;
  765. case 2:
  766. testOpenImagesFld();
  767. break;
  768. case 3:
  769. testResize();
  770. break;
  771. case 4:
  772. testVideoSequence();
  773. break;
  774. case 5:
  775. testSnap();
  776. break;
  777. case 6:
  778. testMouseClick();
  779. break;
  780. case 7:
  781. negative_image();
  782. break;
  783. case 8:
  784. change_aditiv();
  785. break;
  786. case 9:
  787. change_multiplicativ();
  788. break;
  789. case 10:
  790. creare_imagine();
  791. break;
  792. case 11:
  793. matrice();
  794. break;
  795. case 12:
  796. copiere_canale();
  797. break;
  798. case 13:
  799. conversie();
  800. break;
  801. case 14:
  802. conversiegray();
  803. break;
  804. case 15:
  805. convertireCanale();
  806. break;
  807. case 16:
  808. computeHist(img, hist, pdf);
  809. imshow("imagine", img);
  810. showHistogram("histogram", hist, img.rows, img.cols);
  811. waitKey(0);
  812. break;
  813. case 17:
  814. computeHist1(img, hist, pdf, 128);
  815. imshow("imagine", img);
  816. showHistogram("histogram", hist, 256, 256);
  817. waitKey(0);
  818. break;
  819. case 18:
  820. //res = multilevel(img);
  821. //imshow("Imagine", res);
  822. //imshow("Imagine1", img);
  823. waitKey(0);
  824. break;
  825.  
  826.  
  827. case 19:
  828. probleme();
  829.  
  830. break;
  831.  
  832. }
  833.  
  834. } while (op != 0);
  835. return 0;
  836. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement