Advertisement
Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.00 KB | None | 0 0
  1. // OpenCVApplication.cpp : Defines the entry point for the console application.
  2. // Laborator PI, Nagy Imola, gr 30234
  3.  
  4. #include "stdafx.h"
  5. #include "common.h"
  6. #include <queue>
  7. #include <random>
  8. #include <vector>
  9.  
  10.  
  11. void testOpenImage()
  12. {
  13. char fname[MAX_PATH];
  14. while(openFileDlg(fname))
  15. {
  16. Mat src;
  17. src = imread(fname);
  18. imshow("image",src);
  19. waitKey();
  20. }
  21. }
  22.  
  23. void testOpenImagesFld()
  24. {
  25. char folderName[MAX_PATH];
  26. if (openFolderDlg(folderName)==0)
  27. return;
  28. char fname[MAX_PATH];
  29. FileGetter fg(folderName,"bmp");
  30. while(fg.getNextAbsFile(fname))
  31. {
  32. Mat src;
  33. src = imread(fname);
  34. imshow(fg.getFoundFileName(),src);
  35. if (waitKey()==27) //ESC pressed
  36. break;
  37. }
  38. }
  39.  
  40.  
  41. void testResize()
  42. {
  43. char fname[MAX_PATH];
  44. while(openFileDlg(fname))
  45. {
  46. Mat src;
  47. src = imread(fname);
  48. Mat dst1,dst2;
  49. //without interpolation
  50. resizeImg(src,dst1,320,false);
  51. //with interpolation
  52. resizeImg(src,dst2,320,true);
  53. imshow("input image",src);
  54. imshow("resized image (without interpolation)",dst1);
  55. imshow("resized image (with interpolation)",dst2);
  56. waitKey();
  57. }
  58. }
  59.  
  60.  
  61. void testVideoSequence()
  62. {
  63. VideoCapture cap("Videos/rubic.avi"); // off-line video from file
  64. //VideoCapture cap(0); // live video from web cam
  65. if (!cap.isOpened()) {
  66. printf("Cannot open video capture device.\n");
  67. waitKey(0);
  68. return;
  69. }
  70.  
  71. Mat edges;
  72. Mat frame;
  73. char c;
  74.  
  75. while (cap.read(frame))
  76. {
  77. Mat grayFrame;
  78. cvtColor(frame, grayFrame, CV_BGR2GRAY);
  79. imshow("source", frame);
  80. imshow("gray", grayFrame);
  81. c = cvWaitKey(0); // waits a key press to advance to the next frame
  82. if (c == 27) {
  83. // press ESC to exit
  84. printf("ESC pressed - capture finished\n");
  85. break; //ESC pressed
  86. };
  87. }
  88. }
  89.  
  90.  
  91. void testSnap()
  92. {
  93. VideoCapture cap(0); // open the deafult camera (i.e. the built in web cam)
  94. if (!cap.isOpened()) // openenig the video device failed
  95. {
  96. printf("Cannot open video capture device.\n");
  97. return;
  98. }
  99.  
  100. Mat frame;
  101. char numberStr[256];
  102. char fileName[256];
  103.  
  104. // video resolution
  105. Size capS = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
  106. (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
  107.  
  108. // Display window
  109. const char* WIN_SRC = "Src"; //window for the source frame
  110. namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
  111. cvMoveWindow(WIN_SRC, 0, 0);
  112.  
  113. const char* WIN_DST = "Snapped"; //window for showing the snapped frame
  114. namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
  115. cvMoveWindow(WIN_DST, capS.width + 10, 0);
  116.  
  117. char c;
  118. int frameNum = -1;
  119. int frameCount = 0;
  120.  
  121. for (;;)
  122. {
  123. cap >> frame; // get a new frame from camera
  124. if (frame.empty())
  125. {
  126. printf("End of the video file\n");
  127. break;
  128. }
  129.  
  130. ++frameNum;
  131.  
  132. imshow(WIN_SRC, frame);
  133.  
  134. c = cvWaitKey(10); // waits a key press to advance to the next frame
  135. if (c == 27) {
  136. // press ESC to exit
  137. printf("ESC pressed - capture finished");
  138. break; //ESC pressed
  139. }
  140. if (c == 115){ //'s' pressed - snapp the image to a file
  141. frameCount++;
  142. fileName[0] = NULL;
  143. sprintf(numberStr, "%d", frameCount);
  144. strcat(fileName, "Images/A");
  145. strcat(fileName, numberStr);
  146. strcat(fileName, ".bmp");
  147. bool bSuccess = imwrite(fileName, frame);
  148. if (!bSuccess)
  149. {
  150. printf("Error writing the snapped image\n");
  151. }
  152. else
  153. imshow(WIN_DST, frame);
  154. }
  155. }
  156.  
  157. }
  158.  
  159. void MyCallBackFunc(int event, int x, int y, int flags, void* param)
  160. {
  161. //More examples: http://opencvexamples.blogspot.com/2014/01/detect-mouse-clicks-and-moves-on-image.html
  162. Mat* src = (Mat*)param;
  163. if (event == CV_EVENT_LBUTTONDOWN)
  164. {
  165. printf("Pos(x,y): %d,%d Color(RGB): %d,%d,%d\n",
  166. x, y,
  167. (int)(*src).at<Vec3b>(y, x)[2],
  168. (int)(*src).at<Vec3b>(y, x)[1],
  169. (int)(*src).at<Vec3b>(y, x)[0]);
  170. }
  171. }
  172.  
  173. void testMouseClick()
  174. {
  175. Mat src;
  176. // Read image from file
  177. char fname[MAX_PATH];
  178. while (openFileDlg(fname))
  179. {
  180. src = imread(fname);
  181. //Create a window
  182. namedWindow("My Window", 1);
  183.  
  184. //set the callback function for any mouse event
  185. setMouseCallback("My Window", MyCallBackFunc, &src);
  186.  
  187. //show the image
  188. imshow("My Window", src);
  189.  
  190. // Wait until user press some key
  191. waitKey(0);
  192. }
  193. }
  194.  
  195. /* Histogram display function - display a histogram using bars (simlilar to L3 / PI)
  196. Input:
  197. name - destination (output) window name
  198. hist - pointer to the vector containing the histogram values
  199. hist_cols - no. of bins (elements) in the histogram = histogram image width
  200. hist_height - height of the histogram image
  201. Call example:
  202. showHistogram ("MyHist", hist_dir, 255, 200);
  203. */
  204. void showHistogram(const string& name, int* hist, const int hist_cols, const int hist_height)
  205. {
  206. Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); // constructs a white image
  207.  
  208. //computes histogram maximum
  209. int max_hist = 0;
  210. for (int i = 0; i<hist_cols; i++)
  211. if (hist[i] > max_hist)
  212. max_hist = hist[i];
  213. double scale = 1.0;
  214. scale = (double)hist_height / max_hist;
  215. int baseline = hist_height - 1;
  216.  
  217. for (int x = 0; x < hist_cols; x++) {
  218. Point p1 = Point(x, baseline);
  219. Point p2 = Point(x, baseline - cvRound(hist[x] * scale));
  220. line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
  221. }
  222.  
  223. imshow(name, imgHist);
  224. }
  225.  
  226.  
  227. /*Laborator 1*/
  228.  
  229. void negative_image(){
  230. char fname[MAX_PATH];
  231. while (openFileDlg(fname)) {
  232. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  233. for (int i = 0; i < img.rows; i++){
  234. for (int j = 0; j < img.cols; j++){
  235. img.at<uchar>(i, j) = 255 - img.at<uchar>(i, j);
  236. }
  237. }
  238. imshow("negative image", img);
  239. waitKey(0);
  240. }
  241.  
  242. }
  243.  
  244. void gray_level_add_factor() {
  245. int factor = 35;
  246. char fname[MAX_PATH];
  247. while (openFileDlg(fname)) {
  248. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  249. int height = src.rows;
  250. int width = src.cols;
  251. Mat dst = Mat(height, width, CV_8UC1);
  252. for (int i = 0; i < height; i++)
  253. for (int j = 0; j < width; j++)
  254. {
  255. int temp = src.at<uchar>(i, j) + factor;
  256. if (temp > 255)
  257. {
  258. dst.at<uchar>(i, j) = 255;
  259. }
  260. else
  261. {
  262. if (temp < 0)
  263. {
  264. dst.at<uchar>(i, j) = 0;
  265. }
  266. else
  267. {
  268. dst.at<uchar>(i, j) = temp;
  269. }
  270. }
  271. }
  272. imshow("input image", src);
  273. imshow("output image", dst);
  274. imwrite("Images/negative_image.bmp", dst);
  275. waitKey();
  276. }
  277. }
  278.  
  279. void gray_level_mul_factor() {
  280. float factor = 3.5;
  281. char fname[MAX_PATH];
  282. while (openFileDlg(fname)) {
  283. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  284. int height = src.rows;
  285. int width = src.cols;
  286. Mat dst = Mat(height, width, CV_8UC1);
  287. for (int i = 0; i < height; i++)
  288. for (int j = 0; j < width; j++)
  289. {
  290. float temp = src.at<uchar>(i, j) * factor;
  291. if (temp > 255)
  292. {
  293. dst.at<uchar>(i, j) = 255;
  294. }
  295. else
  296. {
  297. if (temp < 0)
  298. {
  299. dst.at<uchar>(i, j) = 0;
  300. }
  301. else
  302. {
  303. dst.at<uchar>(i, j) = temp;
  304. }
  305. }
  306. }
  307. imshow("input image", src);
  308. imshow("output image", dst);
  309. imwrite("Images/negative.bmp", dst);
  310. waitKey();
  311. }
  312. }
  313.  
  314. void createColorImage() {
  315. int len = 256;
  316. Mat img(len, len, CV_8UC3);
  317. // up left
  318. for (int i = 0; i < len/2; i++){
  319. for (int j = 0; j < len/2; j++)
  320. {
  321. Vec3b pixel = img.at< Vec3b>(i, j);
  322. pixel[0] = 255;
  323. pixel[1] = 255;
  324. pixel[2] = 255;
  325. img.at<Vec3b>(i, j) = pixel;
  326. }
  327.  
  328. }
  329. // up right
  330. for (int i = 0; i < len/2; i++)
  331. for (int j = len/2; j < len; j++)
  332. {
  333. Vec3b pixel = img.at< Vec3b>(i, j);
  334. pixel[0] = 0;
  335. pixel[1] = 0;
  336. pixel[2] = 255;
  337.  
  338. img.at<Vec3b>(i, j) = pixel;
  339. }
  340.  
  341. // down left
  342. for (int i = len/2; i < len; i++)
  343. for (int j = 0; j < 128; j++)
  344. {
  345. Vec3b pixel = img.at< Vec3b>(i, j);
  346. pixel[0] = 0;
  347. pixel[1] = 255;
  348. pixel[2] = 0;
  349. img.at<Vec3b>(i, j) = pixel;
  350. }
  351.  
  352. // down right
  353. for (int i = len/2; i < len; i++)
  354. for (int j = len/2; j < len; j++)
  355. {
  356. Vec3b pixel = img.at< Vec3b>(i, j);
  357. pixel[0] = 0;
  358. pixel[1] = 255;
  359. pixel[2] = 255;
  360. img.at<Vec3b>(i, j) = pixel;
  361. }
  362. imshow("input image", img);
  363. waitKey();
  364. }
  365.  
  366. void inverseMatrix()
  367. {
  368. double vals[9] = { 1.3, 2.5 , 1, 0, 5.4, -2.4, 4.5, 2, 1 };
  369. //float vals[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  370. Mat M(3, 3, CV_32FC1, vals);
  371. std::cout << M << std::endl;
  372. Mat inverse;
  373. invert(M, inverse);
  374.  
  375. std::cout << inverse << std::endl;
  376. Mat prod = M*inverse;
  377. std::cout << prod << std::endl;
  378. fflush(stdin);
  379. getchar();
  380.  
  381.  
  382. }
  383.  
  384. //lab2
  385. void separateRGB() {
  386. Mat img = imread("Images/flowers_24bits.bmp", CV_LOAD_IMAGE_UNCHANGED);
  387. Mat red(img.rows, img.cols, CV_8UC3);
  388. Mat green(img.rows, img.cols, CV_8UC3);
  389. Mat blue(img.rows, img.cols, CV_8UC3);
  390.  
  391. Vec3b color; //color of the original pixel at index (i,j)
  392. for (int i = 0; i < img.rows; i++) {
  393. for (int j = 0; j < img.cols; j++) {
  394. color = img.at<Vec3b>(i, j);
  395.  
  396. red.at<uchar>(i, j) = color[2];
  397. green.at<uchar>(i, j) = color[1];
  398. blue.at<uchar>(i, j) = color[0];
  399. }
  400. }
  401. imshow("red", red);
  402. imshow("green", green);
  403. imshow("blue", blue);
  404. waitKey(0);
  405. }
  406.  
  407. void rgbToGrayscale() {
  408. Mat img = imread("Images/flowers_24bits.bmp", CV_LOAD_IMAGE_UNCHANGED);
  409. Mat grayImg(img.rows, img.cols, CV_8UC1);
  410. Vec3b color; //color of the original pixel at index (i,j)
  411. for (int i = 0; i < img.rows; i++) {
  412. for (int j = 0; j < img.cols; j++) {
  413. color = img.at<Vec3b>(i, j);
  414. grayImg.at<uchar>(i, j) = (color[0] + color[1] + color[2]) / 3;
  415. }
  416. }
  417.  
  418. imshow("Grayscale image", grayImg);
  419. waitKey(0);
  420. }
  421.  
  422. void grayscaleToBinary() {
  423. Mat img = imread("Images/bacteria.bmp", CV_LOAD_IMAGE_UNCHANGED);
  424. Mat binary(img.rows, img.cols, CV_8UC1);
  425. int treshold;
  426. printf("Enter the treshold:");
  427. scanf("%d", &treshold);
  428.  
  429. imshow("Original image", img);
  430.  
  431. for (int i = 0; i < img.rows; i++) {
  432. for (int j = 0; j < img.cols; j++) {
  433. if (img.at<uchar>(i, j) < treshold) {
  434. binary.at<uchar>(i, j) = 0;
  435. }
  436. else
  437. {
  438. binary.at<uchar>(i, j) = 255;
  439. }
  440. }
  441. }
  442.  
  443. imshow("Binary image", binary);
  444. waitKey(0);
  445. }
  446.  
  447. Mat binerise(Mat img, float threshold) {
  448. Mat binary(img.rows, img.cols, CV_8UC1);
  449. for (int i = 0; i < img.rows; i++) {
  450. for (int j = 0; j < img.cols; j++) {
  451. if (img.at<uchar>(i, j) < threshold) {
  452. binary.at<uchar>(i, j) = 0;
  453. }
  454. else
  455. {
  456. binary.at<uchar>(i, j) = 255;
  457. }
  458. }
  459. }
  460. return binary;
  461. }
  462.  
  463. void rgbToHSV() {
  464. Mat img = imread("Images/flowers_24bits.bmp", CV_LOAD_IMAGE_UNCHANGED);
  465.  
  466. Mat H(img.rows, img.cols, CV_8UC1);
  467. Mat S(img.rows, img.cols, CV_8UC1);
  468. Mat V(img.rows, img.cols, CV_8UC1);
  469.  
  470. Vec3b color;
  471.  
  472. float r, g, b; // normalized components
  473. float M, m; //max and min
  474. float C;
  475.  
  476. float h1, s1, v1;
  477.  
  478.  
  479. imshow("original", img);
  480.  
  481. for (int i = 0; i < img.rows; i++){
  482. for (int j = 0; j < img.cols; j++){
  483. color = img.at<Vec3b>(i, j);
  484.  
  485. //ordering is blue , green, red
  486. b = ((float) color[0]) / 255;
  487. g = ((float) color[1]) / 255;
  488. r = ((float) color[2]) / 255;
  489.  
  490. M = max(r, g, b);
  491. m = min(r, g, b);
  492. C = M - m;
  493.  
  494. // Value
  495. v1 = M;
  496.  
  497. // Saturation
  498. if (v1 != 0)
  499. s1 = C / v1;
  500. else // Grayscale
  501. s1 = 0;
  502.  
  503. // Hue
  504. if (C != 0){
  505. if (M == r) h1 = 60 * ((g - b) / C);
  506. if (M == g) h1 = 120 + 60 * ((b - r) / C);
  507. if (M == b) h1 = 240 + 60 * ((r - g) / C);
  508. }
  509. else h1 = 0;
  510.  
  511. if (h1 < 0){
  512. h1 = h1 + 360;
  513. }
  514.  
  515. //normalize values
  516.  
  517. h1 = h1 * 255 / 360;
  518. s1 = s1 * 255;
  519. v1 = v1 * 255;
  520.  
  521. //fill matrices
  522. H.at<uchar>(i, j) = h1;
  523. S.at<uchar>(i, j) = s1;
  524. V.at<uchar>(i, j) = v1;
  525. }
  526. }
  527.  
  528. imshow("H", H);
  529. imshow("S", S);
  530. imshow("V", V);
  531. waitKey(0);
  532. }
  533.  
  534. bool isInside(int i, int j, Mat img) {
  535.  
  536. if (i > 0 && i < img.rows && j > 0 && j < img.cols) {
  537. return true;
  538. }
  539. else {
  540. return false;
  541. }
  542.  
  543. }
  544.  
  545. void isInsideImage() {
  546. Mat img = imread("Images/kids.bmp", CV_LOAD_IMAGE_UNCHANGED);
  547. int i, j;
  548. printf("i = ");
  549. scanf("%d", &i);
  550. printf("\nj = ");
  551. scanf("%d", &j);
  552.  
  553. if (isInside(i, j, img)) {
  554. printf("It is inside.\n");
  555. }
  556. else {
  557. printf("It is outside.\n");
  558. }
  559.  
  560. imshow("Image", img);
  561. waitKey(0);
  562. }
  563.  
  564. //lab3
  565.  
  566. //function to check whether the clicked color is equal to another color of a pixel in the image
  567. bool equalColor(Vec3b col1, Vec3b col2) {
  568. if (col1[0] == col2[2] && col1[1] == col2[1] && col1[2] == col2[0])
  569. return true;
  570. return false;
  571. }
  572.  
  573. //calculate area of clicked object
  574. int calculateArea(Vec3b color, Mat img) {
  575. int area = 0;
  576. Vec3b imgColor;
  577. for (int i = 0; i < img.rows; i++) {
  578. for (int j = 0; j < img.cols; j++) {
  579. imgColor = img.at<Vec3b>(i, j);
  580. if (equalColor(imgColor, color))
  581. area++;
  582. }
  583. }
  584. return area;
  585. }
  586.  
  587. int* centruDeMasa(Vec3b color, Mat img) {
  588. int area = calculateArea(color, img);
  589. int r = 0, c = 0;
  590. Vec3b imgColor;
  591.  
  592. for (int i = 0; i < img.rows; i++) {
  593. for (int j = 0; j < img.cols; j++) {
  594. imgColor = img.at<Vec3b>(i, j);
  595. if (equalColor(imgColor, color)) {
  596. r = r + i;
  597. c = c + j;
  598. }
  599. }
  600. }
  601. r = r / area;
  602. c = c / area;
  603. int centre[2] = { r, c };
  604. return centre;
  605. }
  606.  
  607. double elongationAxis(Vec3b color, Mat img) {
  608. int numarator = 0;
  609. int numitor1 = 0;
  610. int numitor2 = 0;
  611. int numitor;
  612. //centre[0] = rows, centre[1] = cols
  613. int* centre = centruDeMasa(color, img);
  614. Vec3b imgColor;
  615.  
  616. for (int i = 0; i < img.rows; i++) {
  617. for (int j = 0; j < img.cols; j++) {
  618. imgColor = img.at<Vec3b>(i, j);
  619. if (equalColor(imgColor, color)) {
  620. numarator = numarator + (i - centre[0])*(j - centre[1]);
  621. numitor1 = numitor1 + (j - centre[1])*(j - centre[1]);
  622. numitor2 = numitor2 + (i - centre[0])*(i - centre[0]);
  623. }
  624. }
  625. }
  626.  
  627. numarator = numarator * 2;
  628. numitor = numitor1 - numitor2;
  629.  
  630. double axis = atan2(numarator, numitor);
  631.  
  632. Point p(centre[0], centre[1]);
  633.  
  634. return axis;
  635.  
  636. }
  637.  
  638. int calculatePerimeter(Vec3b color, Mat img) {
  639. //white : RGB (255,255,255)
  640. int perimeter = 0;
  641. Vec3b imgColor;
  642. Vec3b white(255, 255, 255);
  643.  
  644. for (int i = 0; i < img.rows; i++) {
  645. for (int j = 0; j < img.cols; j++) {
  646. imgColor = img.at<Vec3b>(i, j);
  647. if (equalColor(imgColor, color) && !equalColor(imgColor, white)) {
  648. if (equalColor(white, img.at<Vec3b>(i + 1, j)) ||
  649. equalColor(white, img.at<Vec3b>(i - 1, j)) ||
  650. equalColor(white, img.at<Vec3b>(i, j + 1)) ||
  651. equalColor(white, img.at<Vec3b>(i, j - 1))) {
  652. perimeter++;
  653. }
  654. else {
  655. }
  656. }
  657. }
  658. }
  659. if (perimeter == 0) {
  660. printf("Click on an object! You clicked the background!\n");
  661. }
  662. return perimeter;
  663. }
  664.  
  665. double thinnessRatio(Vec3b color, Mat img) {
  666. int perimeter = calculatePerimeter(color, img);
  667. int area = calculateArea(color, img);
  668. double thin = 4 * PI * area / (perimeter*perimeter);
  669. return thin;
  670. }
  671.  
  672. float aspectRatio(Vec3b color, Mat img) {
  673. int cmin = img.cols;
  674. int rmin = img.rows;
  675. int cmax = 0, rmax = 0;
  676. Vec3b imgColor;
  677.  
  678. for (int i = 0; i < img.rows; i++) {
  679. for (int j = 0; j < img.cols; j++) {
  680. imgColor = img.at<Vec3b>(i, j);
  681. if (equalColor(imgColor, color)) {
  682. if (i < rmin) rmin = i;
  683. if (i > rmax) rmax = i;
  684. if (j < cmin) cmin = j;
  685. if (j > cmax) cmax = j;
  686. }
  687. }
  688. }
  689.  
  690. printf("rmin = %d, rmax = %d, cmin = %d, cmax = %d\n", rmin, rmax, cmin, cmax);
  691. float ratio = (cmax - cmin + 1) / (rmax - rmin + 1);
  692. return ratio;
  693. }
  694.  
  695. void projections(Vec3b color, int row, int col, Mat img) {
  696. int horizontal = 0;
  697. int vertical = 0;
  698. Vec3b imgColor;
  699.  
  700. for (int i = 0; i < img.rows; i++) {
  701. imgColor = img.at<Vec3b>(i, col);
  702. if (equalColor(color, imgColor)) {
  703. vertical++;
  704. }
  705. }
  706.  
  707. for (int j = 0; j < img.rows; j++) {
  708. imgColor = img.at<Vec3b>(row, j);
  709. if (equalColor(color, imgColor)) {
  710. horizontal++;
  711. }
  712. }
  713.  
  714. printf("Sum of pixels on row %d = %d\nSum of pixels on column %d = %d\n", row, horizontal, col, vertical);
  715. }
  716.  
  717. void drawObject(Mat img, Vec3b color) {
  718. Vec3b imgColor;
  719. for (int i = 0; i < img.rows; i++) {
  720. for (int j = 0; j < img.cols; j++) {
  721. imgColor = img.at<Vec3b>(i, j);
  722. if (equalColor(imgColor, color)) {
  723. img.at<Vec3b>(i, j) = imgColor;
  724. }
  725. else {
  726. img.at<Vec3b>(i, j) = Vec3b(255, 255, 255);
  727. }
  728. }
  729. }
  730. }
  731.  
  732. void newFunction(Mat img) {
  733. int areaTH, phi;
  734. printf("Enter treshold for area:");
  735. scanf("%d", &areaTH);
  736. printf("Enter phi:");
  737. scanf("%d", &phi);
  738.  
  739. Vec3b imgColor;
  740.  
  741. for (int i = 0; i < img.rows; i++) {
  742. for (int j = 0; j < img.cols; j++) {
  743. imgColor = img.at<Vec3b>(i, j);
  744. if (!equalColor(imgColor, Vec3b(255, 255, 255))) {
  745. if (calculateArea(imgColor, img) < areaTH) {
  746. drawObject(img, imgColor);
  747. }
  748.  
  749. }
  750. }
  751. }
  752. }
  753.  
  754. void showProperties(Vec3b color, Mat img) {
  755. printf("Area of selected object = %d\n", calculateArea(color, img));
  756. int* centre = centruDeMasa(color, img);
  757. printf("Centrul de masa = (%d,%d)\n", centre[0], centre[1]);
  758. printf("Elongation axis = %lf\n", elongationAxis(color, img));
  759. printf("Perimeter = %d\n", calculatePerimeter(color, img));
  760. printf("Thinnes ratio = %lf\n", thinnessRatio(color, img));
  761. printf("Aspect ratio = %f\n", aspectRatio(color, img));
  762. printf("\n --- Select another object --- \n");
  763.  
  764. waitKey(0);
  765.  
  766. }
  767.  
  768. void onMouse(int event, int x, int y, int flags, void* param)
  769. {
  770. Mat* src = (Mat*)param;
  771. Vec3b color;
  772. if (event == CV_EVENT_LBUTTONDOWN)
  773. {
  774. color[0] = (int)(*src).at<Vec3b>(y, x)[2],
  775. color[1] = (int)(*src).at<Vec3b>(y, x)[1],
  776. color[2] = (int)(*src).at<Vec3b>(y, x)[0];
  777. //printf("R= %d , G= %d, B= %d\n", color[0], color[1], color[2]);
  778. projections(color, x, y, *src);
  779. showProperties(color, *src);
  780. }
  781. }
  782.  
  783. //lab4-1
  784. void calcProperties() {
  785. char fname[MAX_PATH];
  786. while (openFileDlg)
  787. {
  788. Mat img = imread(fname, CV_8UC3);
  789. namedWindow("Click on an object", 1);
  790. setMouseCallback("Click on an object", onMouse, &img);
  791. imshow("Click on an object", img);
  792. waitKey(0);
  793. }
  794. }
  795.  
  796. // Lab 4
  797. int di[8] = { -1, 0, 1, 0, -1, 1, -1, 1 };
  798. int dj[8] = { 0, -1, 0, 1, -1, -1, 1, 1 };
  799.  
  800. uchar neighbors[4];
  801.  
  802. void bfs() {
  803. Mat img = imread("Images/PI-L5/letters.bmp", CV_LOAD_IMAGE_GRAYSCALE);
  804.  
  805. Mat labels = Mat::zeros(img.rows, img.cols, CV_8UC1);
  806. std::queue<Point2i> Q;
  807. Point2i p;
  808. int label = 0;
  809.  
  810. for (int i = 0; i < labels.rows; i++) {
  811. for (int j = 0; j < labels.cols; j++)
  812. {
  813. labels.at<uchar>(i, j) = 0;
  814. }
  815. }
  816.  
  817. for (int i = 1; i < img.rows - 1; i++)
  818. {
  819. for (int j = 1; j < img.cols - 1; j++)
  820. {
  821. if ((img.at<uchar>(i, j) == 0) && (labels.at<uchar>(i, j) == 0)) {
  822. label++;
  823. labels.at<uchar>(i, j) = label;
  824. Q.push({ i,j });
  825. while (!Q.empty()) {
  826. p = Q.front();
  827. Q.pop();
  828.  
  829. for (int k = 0; k < 8; k++) {
  830. int ni = p.x + di[k];
  831. int nj = p.y + dj[k];
  832. if (ni >= 0 && ni < img.rows && nj >= 0 && nj < img.cols && img.at<uchar>(ni, nj) == 0 && labels.at<uchar>(ni, nj) == 0) {
  833. labels.at<uchar>(ni, nj) = label;
  834. Q.push({ ni, nj });
  835. }
  836. }
  837. }
  838. }
  839. }
  840. }
  841.  
  842. //generare paleta de culori
  843. Scalar colorLUT[1000] = { 0 };
  844. Scalar color;
  845. for (int i = 1; i < 1000; i++)
  846. {
  847. Scalar color(rand() & 255, rand() & 255, rand() & 255);
  848. colorLUT[i] = color;
  849. }
  850. colorLUT[0] = Scalar(255, 255, 255); // fundalul va fi alb
  851.  
  852. Mat dst = Mat::zeros(img.rows, img.cols, CV_8UC3); //matricea destinatie pt. afisare
  853.  
  854. for (int i = 1; i < img.rows - 1; i++)
  855. for (int j = 1; j < img.cols - 1; j++)
  856. {
  857. Scalar color = colorLUT[labels.at<uchar>(i, j)]; // valabil pt. Met. 1 BFS
  858. dst.at<Vec3b>(i, j)[0] = color[0];
  859. dst.at<Vec3b>(i, j)[1] = color[1];
  860. dst.at<Vec3b>(i, j)[2] = color[2];
  861. }
  862.  
  863. imshow("original", img);
  864. imshow("colorized", dst);
  865. waitKey(0);
  866. }
  867.  
  868. void generateColorImg() {
  869. Mat colors(255, 255, CV_8UC1);
  870. Mat img(255, 255, CV_8UC3);
  871.  
  872. std::default_random_engine gen;
  873. std::uniform_int_distribution<int> d(0, 255);
  874. uchar r, g, b;
  875. int nrOfColors = 5;
  876. std::vector<Vec3b> rand_colors = std::vector<Vec3b>(nrOfColors);
  877.  
  878. for (int i = 0; i < 5; i++) {
  879. r = d(gen);
  880. g = d(gen);
  881. b = d(gen);
  882. rand_colors.at(i) = Vec3b(r, g, b);
  883. }
  884. rand_colors.at(0) = Vec3b(255, 255, 255);
  885. int index;
  886. for (int i = 0; i < 255; i++) {
  887. for (int j = 0; j < 255; j++) {
  888. index = rand() % nrOfColors;
  889. img.at<Vec3b>(i, j) = rand_colors[index];
  890. }
  891. }
  892.  
  893. imshow("img", img);
  894. waitKey(0);
  895. }
  896.  
  897. // lab5
  898.  
  899. typedef struct {
  900. int i, j;
  901. byte c;
  902. } my_point;
  903.  
  904. void follow_border() {
  905. char fname[MAX_PATH];
  906. while (openFileDlg(fname))
  907. {
  908. Mat src;
  909. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  910.  
  911. Mat dst = Mat(src.size(), CV_8UC1);
  912.  
  913. std::vector<my_point> contur;
  914.  
  915. bool found = false;
  916. int i0, j0;
  917.  
  918. for (int i = 0; i < src.rows && !found; i++) {
  919. for (int j = 0; j < src.cols && !found; j++) {
  920. if (src.at<uchar>(i, j) == 0) {
  921. i0 = i;
  922. j0 = j;
  923. found = true;
  924. }
  925. }
  926. }
  927.  
  928. int di[] = { 0, -1, -1, -1, 0, 1, 1, 1 };
  929. int dj[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  930.  
  931. contur.push_back(my_point{ i0, j0, 7 });
  932.  
  933. bool finished = false;
  934.  
  935. int x = i0;
  936. int y = j0;
  937. int dir = 7;
  938.  
  939. while (!finished) {
  940. if (dir % 2 == 0) {
  941. dir = (dir + 7) % 8;
  942. }
  943. else {
  944. dir = (dir + 6) % 8;
  945. }
  946.  
  947. while (src.at<uchar>(x + di[dir], y + dj[dir]) != 0) {
  948. dir = dir + 1;
  949. if (dir == 8) {
  950. dir = 0;
  951. }
  952. }
  953.  
  954. contur.push_back(my_point{ x + di[dir], y + dj[dir], (byte)dir });
  955.  
  956. x += di[dir];
  957. y += dj[dir];
  958.  
  959. int n = contur.size();
  960. if (n > 2 && contur.at(0).i == contur.at(n - 2).i && contur.at(0).j == contur.at(n - 2).j && contur.at(1).i == contur.at(n - 1).i && contur.at(1).j == contur.at(n - 1).j) {
  961. finished = true;
  962.  
  963. }
  964. }
  965.  
  966.  
  967. for (int i = 0; i < contur.size(); i++) {
  968. dst.at<uchar>(contur.at(i).i, contur.at(i).j) = 0;
  969. int v = 7;
  970. if (i > 0) {
  971. v = contur.at(i).c - contur.at(i - 1).c;
  972. }
  973. if (v < 0)
  974. v += 8;
  975. printf("%i ( %i , %i ) %i , %i \n", i, contur.at(i).i, contur.at(i).j, contur.at(i).c, v);
  976. }
  977.  
  978. imshow("original", src);
  979. imshow("contur", dst);
  980. waitKey();
  981. }
  982. }
  983.  
  984. // lab6
  985.  
  986. Mat dilatare_param(Mat src) {
  987. Mat dst = Mat(src.size(), CV_8UC1);
  988. dst = src.clone();
  989.  
  990. int di[] = { 0, -1, -1, -1, 0, 1, 1, 1 };
  991. int dj[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  992.  
  993. for (int i = 0; i < src.rows; i++)
  994. {
  995. for (int j = 0; j < src.cols; j++)
  996. {
  997. if (src.at<uchar>(i, j) == 0) { //daca e obiect
  998. for (int k = 0; k < 8; k++) {
  999. dst.at<uchar>(i + di[k], j + dj[k]) = 0;
  1000. }
  1001. }
  1002. }
  1003. }
  1004.  
  1005. return dst;
  1006. }
  1007.  
  1008. void dilatare() {
  1009. char fname[MAX_PATH];
  1010. while (openFileDlg(fname))
  1011. {
  1012. Mat src;
  1013. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1014.  
  1015. Mat dst = Mat(src.size(), CV_8UC1);
  1016.  
  1017. dst = dilatare_param(src);
  1018.  
  1019. imshow("original", src);
  1020. imshow("dst", dst);
  1021. waitKey();
  1022. }
  1023. }
  1024.  
  1025. Mat eroziune_param(Mat src) {
  1026. Mat dst = Mat(src.size(), CV_8UC1);
  1027. dst = src.clone();
  1028.  
  1029. int di[] = { 0, -1, -1, -1, 0, 1, 1, 1 };
  1030. int dj[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  1031.  
  1032. for (int i = 0; i < src.rows; i++)
  1033. {
  1034. for (int j = 0; j < src.cols; j++)
  1035. {
  1036. if (src.at<uchar>(i, j) == 0) { //daca e obiect
  1037. bool ok = true;
  1038. for (int k = 0; (k < 8) && ok; k++) {
  1039. if (src.at<uchar>(i + di[k], j + dj[k]) == 255) {
  1040. dst.at<uchar>(i, j) = 255;
  1041. }
  1042.  
  1043. }
  1044. }
  1045. }
  1046. }
  1047.  
  1048. return dst;
  1049. }
  1050.  
  1051. void eroziune() {
  1052. char fname[MAX_PATH];
  1053. while (openFileDlg(fname))
  1054. {
  1055. Mat src;
  1056. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1057. Mat dst = Mat(src.size(), CV_8UC1);
  1058.  
  1059. dst = eroziune_param(src);
  1060.  
  1061. imshow("original", src);
  1062. imshow("dst", dst);
  1063. waitKey();
  1064. }
  1065. }
  1066.  
  1067. void incidere() {
  1068. char fname[MAX_PATH];
  1069. while (openFileDlg(fname))
  1070. {
  1071. Mat src;
  1072. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1073. Mat temp = Mat(src.size(), CV_8UC1);
  1074.  
  1075. temp = dilatare_param(src);
  1076.  
  1077. Mat dst = Mat(src.size(), CV_8UC1);
  1078.  
  1079. dst = eroziune_param(temp);
  1080.  
  1081.  
  1082. imshow("original", src);
  1083. imshow("dst", dst);
  1084. waitKey();
  1085. }
  1086. }
  1087.  
  1088. void deschidere() {
  1089. char fname[MAX_PATH];
  1090. while (openFileDlg(fname))
  1091. {
  1092. Mat src;
  1093. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1094. Mat temp = Mat(src.size(), CV_8UC1);
  1095.  
  1096. temp = eroziune_param(src);
  1097.  
  1098. Mat dst = Mat(src.size(), CV_8UC1);
  1099.  
  1100. dst = dilatare_param(temp);
  1101.  
  1102.  
  1103. imshow("original", src);
  1104. imshow("dst", dst);
  1105. waitKey();
  1106. }
  1107. }
  1108.  
  1109. void contur() {
  1110. char fname[MAX_PATH];
  1111. while (openFileDlg(fname))
  1112. {
  1113. Mat src;
  1114. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1115. Mat temp = Mat(src.size(), CV_8UC1);
  1116.  
  1117. temp = eroziune_param(src);
  1118.  
  1119. Mat dst = Mat(src.size(), CV_8UC1);
  1120.  
  1121. for (int i = 0; i < src.rows; i++)
  1122. {
  1123. for (int j = 0; j < src.cols; j++)
  1124. {
  1125. if (src.at<uchar>(i, j) != temp.at<uchar>(i, j)) {
  1126. dst.at<uchar>(i, j) = 0;
  1127. }
  1128. else {
  1129. dst.at<uchar>(i, j) = 255;
  1130. }
  1131. }
  1132. }
  1133.  
  1134. imshow("original", src);
  1135. imshow("dst", dst);
  1136. waitKey();
  1137. }
  1138. }
  1139.  
  1140. void histogram() {
  1141. char fname[MAX_PATH];
  1142. while (openFileDlg(fname))
  1143. {
  1144. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1145.  
  1146. int h[256];
  1147. int M = src.rows * src.cols;
  1148. float fdp[256];
  1149. float medie = 0;
  1150. float deviatie = 0;
  1151.  
  1152. // initializare vectori
  1153. for (int i = 0; i < 256; i++) {
  1154. h[i] = 0;
  1155. fdp[i] = 0;
  1156. }
  1157.  
  1158. //histograma
  1159. for (int i = 0; i < src.rows; i++) {
  1160. for (int j = 0; j < src.cols; j++)
  1161. {
  1162. h[src.at<uchar>(i, j)] ++;
  1163. }
  1164. }
  1165.  
  1166. showHistogram("Histograma", h, src.cols, src.rows);
  1167.  
  1168. //fdp si medie
  1169. for (int i = 0; i < 256; i++) {
  1170. fdp[i] = h[i] / (float)M;
  1171. medie += fdp[i];
  1172. }
  1173.  
  1174. printf("Medie: %f\n", medie);
  1175.  
  1176. //deviatie
  1177. for (int i = 0; i < src.rows; i++)
  1178. {
  1179. for (int j = 0; j < src.cols; j++)
  1180. {
  1181. deviatie += ((src.at<uchar>(i, j) - medie) * (src.at<uchar>(i, j) - medie));
  1182. }
  1183. }
  1184. deviatie /= M;
  1185. deviatie = sqrt(deviatie);
  1186. printf("Deviatie: %f\n",deviatie);
  1187.  
  1188. imshow("original", src);
  1189. waitKey();
  1190. }
  1191. }
  1192.  
  1193. void automated_binarization() {
  1194. char fname[MAX_PATH];
  1195. while (openFileDlg(fname))
  1196. {
  1197. Mat src;
  1198. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1199. int h[256];
  1200. int imax = 0;
  1201. int imin = 256;
  1202. float eroare = 0.1;
  1203. float tk = 0;
  1204. float tk_1 = 0;
  1205.  
  1206. // initializare vectori
  1207. for (int i = 0; i < 256; i++) {
  1208. h[i] = 0;
  1209. }
  1210.  
  1211. //histograma, Imin, Imax
  1212. for (int i = 0; i < src.rows; i++) {
  1213. for (int j = 0; j < src.cols; j++)
  1214. {
  1215. int intenstity = src.at<uchar>(i, j);
  1216. h[intenstity] ++;
  1217. if (intenstity < imin) {
  1218. imin = intenstity;
  1219. }
  1220. else if (intenstity > imax){
  1221. imax = intenstity;
  1222. }
  1223. }
  1224. }
  1225. //initializare t
  1226. tk = (imin + imax) / 2;
  1227.  
  1228. while (tk - tk_1) {
  1229. // calculare cele doua medii
  1230. float medie1 = 0;
  1231. float medie2 = 0;
  1232.  
  1233. int contor = 0;
  1234. for (int i = 0; i <= tk; i++) {
  1235. medie1 += i * h[i];
  1236. contor += h[i];
  1237. }
  1238.  
  1239. medie1 /= contor;
  1240.  
  1241. contor = 0;
  1242. for (int i = tk + 1; i < 256; i++) {
  1243. medie2 += i * h[i];
  1244. contor += h[i];
  1245. }
  1246. medie2 /= contor;
  1247.  
  1248. // calculare t
  1249. tk = (medie1 + medie2) / 2;
  1250. tk_1 = tk;
  1251. }
  1252.  
  1253. Mat dst = binerise(src, tk);
  1254.  
  1255. imshow("original", src);
  1256. imshow("binary", dst);
  1257. waitKey();
  1258. }
  1259. }
  1260.  
  1261. void negativ() {
  1262. char fname[MAX_PATH];
  1263. while (openFileDlg(fname))
  1264. {
  1265. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1266. Mat dst = Mat(src.size(), CV_8UC1);
  1267.  
  1268. for (int i = 0; i < src.rows; i++)
  1269. {
  1270. for (int j = 0; j < src.cols; j++)
  1271. {
  1272. dst.at<uchar>(i, j) = 255 - src.at<uchar>(i, j);
  1273. }
  1274. }
  1275.  
  1276. imshow("original", src);
  1277. imshow("dst", dst);
  1278. waitKey();
  1279. }
  1280. }
  1281.  
  1282. void contrast_modif() {
  1283. char fname[MAX_PATH];
  1284. while (openFileDlg(fname))
  1285. {
  1286. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1287. Mat dst = Mat(src.size(), CV_8UC1);
  1288.  
  1289. int imax = 0;
  1290. int imin = 256;
  1291.  
  1292. //histograma, Imin, Imax
  1293. for (int i = 0; i < src.rows; i++) {
  1294. for (int j = 0; j < src.cols; j++)
  1295. {
  1296. int intenstity = src.at<uchar>(i, j);
  1297. if (intenstity < imin) {
  1298. imin = intenstity;
  1299. }
  1300. else if (intenstity > imax) {
  1301. imax = intenstity;
  1302. }
  1303. }
  1304. }
  1305.  
  1306. int nImax = 0;
  1307. int nImin = 256;
  1308.  
  1309.  
  1310. // read new intensity limits
  1311. printf("Minimum intensity: %d, maximum intensity: %d\n", imin, imax);
  1312. printf("New minimum intensity: \n");
  1313. scanf("%d", &nImin);
  1314. printf("New maximum intensity: \n");
  1315. scanf("%d", &nImax);
  1316.  
  1317. // modify image
  1318. for (int i = 0; i < src.rows; i++) {
  1319. for (int j = 0; j < src.cols; j++)
  1320. {
  1321. dst.at<uchar>(i, j) = nImin + (src.at<uchar>(i, j) - imin) * (nImax - nImin) / (imax - imin);
  1322. }
  1323. }
  1324.  
  1325. imshow("original", src);
  1326. imshow("dst", dst);
  1327. waitKey();
  1328. }
  1329. }
  1330.  
  1331. void gamma_correction() {
  1332. char fname[MAX_PATH];
  1333. while (openFileDlg(fname))
  1334. {
  1335. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1336. Mat dst = Mat(src.size(), CV_8UC1);
  1337.  
  1338. double gamma = 0.4;
  1339.  
  1340. // modify image
  1341. for (int i = 0; i < src.rows; i++) {
  1342. for (int j = 0; j < src.cols; j++)
  1343. {
  1344. double corrected = 255 * pow((src.at<uchar>(i, j) / (double) 255), gamma);
  1345. corrected = max(0, corrected);
  1346. corrected = min(255, corrected);
  1347. dst.at<uchar>(i, j) = (uchar)corrected;
  1348. }
  1349. }
  1350.  
  1351. imshow("original", src);
  1352. imshow("dst", dst);
  1353. waitKey();
  1354. }
  1355. }
  1356.  
  1357. void light_modif() {
  1358. char fname[MAX_PATH];
  1359. while (openFileDlg(fname))
  1360. {
  1361. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1362. Mat dst = Mat(src.size(), CV_8UC1);
  1363.  
  1364. int offset = 10;
  1365.  
  1366. // modify image
  1367. for (int i = 0; i < src.rows; i++) {
  1368. for (int j = 0; j < src.cols; j++)
  1369. {
  1370. int corrected = src.at<uchar>(i, j) + offset;
  1371. corrected = max(0, corrected);
  1372. corrected = min(255, corrected);
  1373. dst.at<uchar>(i, j) = (uchar) corrected;
  1374. }
  1375. }
  1376.  
  1377. imshow("original", src);
  1378. imshow("dst", dst);
  1379. waitKey();
  1380. }
  1381. }
  1382.  
  1383. void filtru_trece_jos() {
  1384. char fname[MAX_PATH];
  1385. while (openFileDlg(fname))
  1386. {
  1387. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1388. Mat dst = Mat(img.size(), CV_8UC1);
  1389.  
  1390. int H[3][3] =
  1391. { { 1, 1, 1},
  1392. { 1, 1, 1 },
  1393. { 1, 1, 1} };
  1394.  
  1395. int sum = 0;
  1396.  
  1397. for (int i = 0; i < 3; i++) {
  1398. for (int j = 0; j < 3; j++)
  1399. {
  1400. sum += H[i][j];
  1401. }
  1402. }
  1403.  
  1404. for (int i = 1; i < img.rows - 1; i++)
  1405. {
  1406. for (int j = 1; j < img.cols - 1; j++)
  1407. {
  1408. int pixel = 0;
  1409. for (int k = 0; k < 3; k++)
  1410. {
  1411. for (int z = 0; z < 3; z++)
  1412. {
  1413. pixel += (H[k][z] * img.at<uchar>(i + k - 1, z + j - 1));
  1414. }
  1415. }
  1416.  
  1417.  
  1418. if (sum != 0)
  1419. {
  1420. pixel /= sum;
  1421. }
  1422. if (pixel >= 255)
  1423. {
  1424. pixel = 255;
  1425. }
  1426. if (pixel <= 0)
  1427. {
  1428. pixel = 0;
  1429. }
  1430. dst.at<uchar>(i, j) = pixel;
  1431. }
  1432. }
  1433.  
  1434. imshow("original", img);
  1435. imshow("dst", dst);
  1436. waitKey();
  1437. }
  1438. }
  1439.  
  1440. void filtru_trece_sus() {
  1441. char fname[MAX_PATH];
  1442. while (openFileDlg(fname))
  1443. {
  1444. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1445. Mat dst = Mat(img.size(), CV_8UC1);
  1446.  
  1447. int H[3][3] =
  1448. { { 0, -1, 0 },
  1449. { -1, 4, -1 },
  1450. { 0, -1, 0 } };
  1451.  
  1452. int sum_poz = 0;
  1453. int sum_neg = 0;
  1454. int sum;
  1455.  
  1456. for (int i = 0; i < 7; i++) {
  1457. for (int j = 0; j < 7; j++)
  1458. {
  1459. if (H[i][j] < 0)
  1460. {
  1461. sum_neg += H[i][j] * (-1);
  1462. }
  1463. else
  1464. {
  1465. sum_poz += H[i][j];
  1466. }
  1467. }
  1468. }
  1469. sum = max(sum_poz, sum_neg);
  1470.  
  1471. for (int i = 0; i < img.rows - 1; i++)
  1472. {
  1473. for (int j = 0; j < img.cols - 1; j++)
  1474. {
  1475. int pixel = 0;
  1476. for (int k = 0; k < 3; k++)
  1477. {
  1478. for (int z = 0; z < 3; z++)
  1479. {
  1480. pixel += H[k][z] * img.at<uchar>(k + i - 1, z + j - 1);
  1481. }
  1482. }
  1483.  
  1484.  
  1485. if (sum != 0)
  1486. {
  1487. pixel /= sum;
  1488. }
  1489. pixel += 128;
  1490.  
  1491. if (pixel >= 255)
  1492. {
  1493. pixel = 255;
  1494. }
  1495. else if (pixel <= 0)
  1496. {
  1497. pixel = 0;
  1498. }
  1499. dst.at<uchar>(i, j) = pixel;
  1500. }
  1501. }
  1502.  
  1503. imshow("original", img);
  1504. imshow("dst", dst);
  1505. waitKey();
  1506. }
  1507. }
  1508.  
  1509. void interschimba(int &a, int &b) {
  1510. int c = a;
  1511. a = b;
  1512. b = c;
  1513. }
  1514.  
  1515. void bubble_sort(int(&v)[9], int n) {
  1516. bool isSorted = false;
  1517. int i = 0;
  1518. while (!isSorted && i < n - 1) {
  1519. isSorted = true;
  1520. for (int j = 1; j < n - i - 1; j++) {
  1521. if (v[j] > v[j + 1]) {
  1522. interschimba(v[j], v[j + 1]);
  1523. isSorted = false;
  1524. }
  1525. }
  1526. }
  1527. }
  1528.  
  1529. void saltPepperFilter()
  1530. {
  1531. char fname[MAX_PATH];
  1532. while (openFileDlg(fname))
  1533. {
  1534. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1535. Mat dst = Mat(img.size(), CV_8UC1);
  1536.  
  1537. int filterSize = 8;
  1538. int di[] = { 0, -1, -1, -1, 0, 1, 1, 1 };
  1539. int dj[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  1540.  
  1541. int filter[9];
  1542.  
  1543. for (int i = 1; i < img.rows - 1; i++)
  1544. {
  1545. for (int j = 1; j < img.cols - 1; j++)
  1546. {
  1547. for (int k = 0; k < 8; k++) {
  1548. filter[k] = img.at<uchar>(i + di[k], j + dj[k]);
  1549. bubble_sort(filter, 8);
  1550. dst.at<uchar>(i, j) = filter[4];
  1551. }
  1552. }
  1553. }
  1554.  
  1555. imshow("original", img);
  1556. imshow("dst", dst);
  1557. waitKey();
  1558. }
  1559. }
  1560.  
  1561. Mat gaussian_filter(Mat src, int w) {
  1562. Mat dst = Mat(src.size(), CV_8UC1);
  1563. float sigma = 0;
  1564.  
  1565. sigma = (float)w/6;
  1566.  
  1567. float nucleu[20][20];
  1568. float sumaNucleu = 0;
  1569. float sigmaPi = sigma*sigma * 2 * CV_PI;
  1570. for (int i = 0; i < w; i++) {
  1571. for (int j = 0; j < w; j++) {
  1572. int k = (i - w / 2)*(i - w / 2) + (j - w / 2)*(j - w / 2);
  1573. nucleu[i][j] = (float)exp((-1)*k / (2 * sigma*sigma)) / sigmaPi;
  1574. sumaNucleu += nucleu[i][j];
  1575.  
  1576. }
  1577. }
  1578. printf("Suma elementelr din nucleu gaussian: %.2f\n", sumaNucleu);
  1579.  
  1580. int value = 0;
  1581. for (int i = w / 2; i < src.rows - w / 2; i++) {
  1582. for (int j = w / 2; j < src.cols - w / 2; j++) {
  1583.  
  1584. for (int ll = -w / 2; ll <= w / 2; ll++) {
  1585. for (int lc = -w / 2; lc <= w / 2; lc++) {
  1586. value += src.at<uchar>(i + ll, j + lc) * nucleu[ll + w / 2][lc + w / 2];
  1587. }
  1588. }
  1589. dst.at<uchar>(i, j) = value / sumaNucleu;
  1590. value = 0;
  1591. }
  1592. }
  1593.  
  1594. imshow("dst", dst);
  1595. return dst;
  1596. }
  1597.  
  1598. void canny() {
  1599. char fname[MAX_PATH];
  1600. while (openFileDlg(fname)) {
  1601. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1602. Mat temp = gaussian_filter(src, 3);
  1603.  
  1604. int sX[3][3] = { {-1, 0 , 1},
  1605. { -2, 0, 2},
  1606. {-1, 0, 1} };
  1607.  
  1608. int sY[3][3] = { {1, 2, 1},
  1609. {0, 0, 0},
  1610. {-1, -2, -1} };
  1611.  
  1612. Mat modul = Mat(temp.size(), CV_8UC1);
  1613. Mat directie = Mat(temp.size(), CV_8UC1);
  1614.  
  1615. for (int i = 1; i < temp.rows - 1; i++) {
  1616. for (int j = 1; j < temp.cols - 1; j++) {
  1617. float gradientX = 0;
  1618. float gradientY = 0;
  1619. for (int k = 0; k < 3; k++) {
  1620. for (int l = 0; l < 3; l++) {
  1621. gradientX += sX[k][l] * temp.at<uchar>(i + k - 1, j + l - 1);
  1622. gradientY += sY[k][l] * temp.at<uchar>(i + k - 1, j + l - 1);
  1623. }
  1624. }
  1625.  
  1626. modul.at<uchar>(i, j) = sqrt(gradientX*gradientX + gradientY *gradientY) / 5.65;
  1627.  
  1628. float teta = atan2((float)gradientY, (float)gradientX);
  1629. int dir = 0;
  1630. if ((teta > 3 * PI / 8 && teta < 5 * PI / 8) || (teta > -5 * PI / 8 && teta < -3 * PI / 8)) dir = 0;
  1631. if ((teta > PI / 8 && teta < 3 * PI / 8) || (teta > -7 * PI / 8 && teta < -5 * PI / 8)) dir = 1;
  1632. if ((teta > -PI / 8 && teta < PI / 8) || teta > 7 * PI / 8 && teta < -7 * PI / 8) dir = 2;
  1633. if ((teta > 5 * PI / 8 && teta < 7 * PI / 8) || (teta > -3 * PI / 8 && teta < -PI / 8)) dir = 3;
  1634. directie.at<uchar>(i, j) = dir;
  1635. }
  1636. }
  1637.  
  1638. imshow("modul1", modul);
  1639.  
  1640. for (int i = 1; i < temp.rows - 1; i++) {
  1641. for (int j = 1; j < temp.cols - 1; j++) {
  1642. switch (directie.at<uchar>(i, j))
  1643. {
  1644. case 0:
  1645. if (modul.at<uchar>(i, j) != max(modul.at<uchar>(i, j), max(modul.at<uchar>(i - 1, j), modul.at<uchar>(i + 1, j)))) {
  1646. modul.at<uchar>(i, j) = 0;
  1647. }
  1648. break;
  1649. case 1:
  1650. if (modul.at<uchar>(i, j) != max(modul.at<uchar>(i, j), max(modul.at<uchar>(i - 1, j - 1), modul.at<uchar>(i + 1, j + 1)))) {
  1651. modul.at<uchar>(i, j) = 0;
  1652. }
  1653. break;
  1654. case 2:
  1655. if (modul.at<uchar>(i, j) != max(modul.at<uchar>(i, j), max(modul.at<uchar>(i, j + 1), modul.at<uchar>(i - 1, j)))) {
  1656. modul.at<uchar>(i, j) = 0;
  1657. }
  1658. break;
  1659. case 3:
  1660. if (modul.at<uchar>(i, j) != max(modul.at<uchar>(i, j), max(modul.at<uchar>(i + 1, j + 1), modul.at<uchar>(i - 1, j - 1)))) {
  1661. modul.at<uchar>(i, j) = 0;
  1662. }
  1663. default: break;
  1664. }
  1665. }
  1666. }
  1667.  
  1668. imshow("modul", modul);
  1669.  
  1670. // Generare paleta de culori pentru afisarea directiilor gradientului
  1671. Scalar colorLUT[4] = { 0 };
  1672. colorLUT[0] = Scalar(0, 0, 255); //red
  1673. colorLUT[1] = Scalar(0, 255, 0); // green
  1674. colorLUT[2] = Scalar(255, 0, 0); // blue
  1675. colorLUT[3] = Scalar(0, 255, 255); // yellow
  1676. //matricea pt. afisarea muchiilor in codul de culori al directiilor
  1677. Mat_<Scalar> ImgDir = Mat::zeros(modul.size(), CV_8UC3);
  1678.  
  1679. // Afisarea pixelilor de muchie in culoarea codului de directie
  1680. for (int i = 1; i < modul.rows - 1; i++)
  1681. for (int j = 1; j < modul.cols - 1; j++)
  1682. if (modul.at<uchar>(i, j))
  1683. ImgDir(i, j) = colorLUT[directie.at<uchar>(i, j)];
  1684. imshow("Imagine directii", ImgDir);
  1685.  
  1686. int modulHistogram[256];
  1687. for (int i = 0; i < modul.rows; i++) {
  1688. for (int j = 0; j < modul.cols; j++) {
  1689. modulHistogram[modul.at<uchar>(i,j)] ++;
  1690. }
  1691. }
  1692.  
  1693. float p = 0.05;
  1694. int puncMucie = p * (modul.rows * modul.cols - modulHistogram[0]);
  1695. int nonMuchie = (1 - p) * (modul.rows * modul.cols - modulHistogram[0]);
  1696.  
  1697. int pragAdaptiv = 0;
  1698. for (int i = 0; (i < 256) && (pragAdaptiv <= nonMuchie); i++) {
  1699. pragAdaptiv += modulHistogram[i] * i;
  1700. }
  1701.  
  1702.  
  1703.  
  1704.  
  1705. imshow("src", src);
  1706. //imshow("dst", dst);
  1707. waitKey();
  1708. }
  1709. }
  1710.  
  1711.  
  1712. int main()
  1713. {
  1714. int op;
  1715. do
  1716. {
  1717. system("cls");
  1718. destroyAllWindows();
  1719. printf("Menu:\n");
  1720. printf(" 1 - Open image\n");
  1721. printf(" 2 - Open BMP images from folder\n");
  1722. printf(" 3 - Resize image\n");
  1723. printf(" 4 - Process video\n");
  1724. printf(" 5 - Snap frame from live video\n");
  1725. printf(" 6 - Mouse callback demo\n");
  1726. printf(" 7 - L1 Negative Image \n");
  1727. printf(" 8 - Gray level modification with addfactor \n");
  1728. printf(" 9 - Gray level modification with mulfactor \n");
  1729. printf(" 10 - Create color image \n");
  1730. printf(" 11 - Create matrix and calculate reverse. \n");
  1731. printf(" 12 - Separate R, G, B. \n");
  1732. printf(" 13 - RGB to Grayscale. \n");
  1733. printf(" 14 - Grayscale to binary\n");
  1734. printf(" 15 - RGB to HSV\n");
  1735. printf(" 16 - Is point inside the image.\n");
  1736. printf(" 17 - Calculate properties\n");
  1737. printf(" 18 - BFS\n");
  1738. printf(" 19 - Generate color image\n");
  1739. printf(" 20 - Follow border\n" );
  1740. printf(" 21 - Dilatare\n");
  1741. printf(" 22 - Eroziune\n");
  1742. printf(" 23 - Inchidere\n");
  1743. printf(" 24 - Deschidere\n");
  1744. printf(" 25 - Contur\n");
  1745. printf(" 26 - Average, standard deviation, histogram\n");
  1746. printf(" 27 - Automated binarisation\n");
  1747. printf(" 28 - Negative image\n");
  1748. printf(" 29 - Contrast modification\n");
  1749. printf(" 30 - Gamma correction\n");
  1750. printf(" 31 - Light correction\n");
  1751. printf(" 32 - Filtru trece sus\n");
  1752. printf(" 33 - Filtru trece jos\n");
  1753. printf(" 34 - Salt & pepper median filter\n");
  1754. printf(" 35 - Canny.\n");
  1755. printf(" 0 - Exit\n\n");
  1756. printf("Option: ");
  1757. scanf("%d",&op);
  1758. switch (op)
  1759. {
  1760. case 1:
  1761. testOpenImage();
  1762. break;
  1763. case 2:
  1764. testOpenImagesFld();
  1765. break;
  1766. case 3:
  1767. testResize();
  1768. break;
  1769. case 4:
  1770. testVideoSequence();
  1771. break;
  1772. case 5:
  1773. testSnap();
  1774. break;
  1775. case 6:
  1776. testMouseClick();
  1777. break;
  1778. case 7:
  1779. negative_image();
  1780. break;
  1781. case 8:
  1782. gray_level_add_factor();
  1783. break;
  1784. case 9:
  1785. gray_level_mul_factor();
  1786. break;
  1787. case 10:
  1788. createColorImage();
  1789. break;
  1790. case 11:
  1791. inverseMatrix();
  1792. break;
  1793. case 12:
  1794. separateRGB();
  1795. case 13:
  1796. rgbToGrayscale();
  1797. case 14:
  1798. grayscaleToBinary();
  1799. case 15:
  1800. rgbToHSV();
  1801. case 16:
  1802. isInsideImage();
  1803. case 17:
  1804. calcProperties();
  1805. break;
  1806. case 18:
  1807. bfs();
  1808. case 19:
  1809. generateColorImg();
  1810. case 20:
  1811. follow_border();
  1812. case 21:
  1813. dilatare();
  1814. case 22:
  1815. eroziune();
  1816. case 23:
  1817. incidere();
  1818. case 24:
  1819. deschidere();
  1820. case 25:
  1821. contur();
  1822. case 26:
  1823. histogram();
  1824. case 27:
  1825. automated_binarization();
  1826. case 28:
  1827. negativ();
  1828. case 29:
  1829. contrast_modif();
  1830. case 30:
  1831. gamma_correction();
  1832. case 31:
  1833. light_modif();
  1834. case 32:
  1835. filtru_trece_sus();
  1836. case 33:
  1837. filtru_trece_jos();
  1838. case 34:
  1839. saltPepperFilter();
  1840. case 35:
  1841. canny();
  1842. }
  1843. }
  1844. while (op!=0);
  1845. return 0;
  1846. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement