Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 68.00 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. #include math.h
  7. #include queue
  8. #include random
  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(Videosrubic.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 finishedn);
  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. VideoCapture cap(Videosmegamind.avi);
  95. if (!cap.isOpened()) openenig the video device failed
  96. {
  97. printf(Cannot open video capture device.n);
  98. return;
  99. }
  100.  
  101. Mat frame;
  102. char numberStr[256];
  103. char fileName[256];
  104.  
  105. video resolution
  106. Size capS = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
  107. (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
  108.  
  109. Display window
  110. const char WIN_SRC = Src; window for the source frame
  111. namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
  112. cvMoveWindow(WIN_SRC, 0, 0);
  113.  
  114. const char WIN_DST = Snapped; window for showing the snapped frame
  115. namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
  116. cvMoveWindow(WIN_DST, capS.width + 10, 0);
  117.  
  118. char c;
  119. int frameNum = -1;
  120. int frameCount = 0;
  121.  
  122. for (;;)
  123. {
  124. cap frame; get a new frame from camera
  125. if (frame.empty())
  126. {
  127. printf(End of the video filen);
  128. break;
  129. }
  130.  
  131. ++frameNum;
  132.  
  133. imshow(WIN_SRC, frame);
  134.  
  135. c = cvWaitKey(10); waits a key press to advance to the next frame
  136. if (c == 27) {
  137. press ESC to exit
  138. printf(ESC pressed - capture finished);
  139. break; ESC pressed
  140. }
  141. if (c == 115){ 's' pressed - snapp the image to a file
  142. frameCount++;
  143. fileName[0] = NULL;
  144. sprintf(numberStr, %d, frameCount);
  145. strcat(fileName, ImagesA);
  146. strcat(fileName, numberStr);
  147. strcat(fileName, .bmp);
  148. bool bSuccess = imwrite(fileName, frame);
  149. if (!bSuccess)
  150. {
  151. printf(Error writing the snapped imagen);
  152. }
  153. else
  154. imshow(WIN_DST, frame);
  155. }
  156. }
  157.  
  158. }
  159.  
  160. void MyCallBackFunc(int event, int x, int y, int flags, void param)
  161. {
  162. More examples httpopencvexamples.blogspot.com201401detect-mouse-clicks-and-moves-on-image.html
  163. Mat src = (Mat)param;
  164. Vec3b buf;
  165. if (event == CV_EVENT_FLAG_LBUTTON)
  166. {
  167.  
  168.  
  169. printf(Pos(x,y) %d,%d Color(RGB) %d,%d,%dn,
  170. x, y,
  171. (int)(src).atVec3b(y, x)[2],
  172. (int)(src).atVec3b(y, x)[1],
  173. (int)(src).atVec3b(y, x)[0]);
  174.  
  175. }
  176. if (event == CV_EVENT_LBUTTONDBLCLK){
  177. buf[2] = (src).atVec3b(y, x)[2];
  178. buf[1] = (src).atVec3b(y, x)[1];
  179. buf[0] = (src).atVec3b(y, x)[0];
  180. float r_max = -1, r_min = 10000, c_max = -1, c_min = 10000;
  181. int area = 0, V = 0, W = 0;
  182. float fi, P = 0, com_rows = 0, com_cols = 0, T;
  183. for (int i = 0; i src-rows; i++){
  184. for (int j = 0; j src-cols; j++){
  185. if (src-atVec3b(i, j) == buf)
  186. {
  187. area++;
  188. com_rows += i;
  189. com_cols += j;
  190. if (src-atVec3b(i + 1, j) != buf
  191. src-atVec3b(i + 1, j + 1) != buf
  192. src-atVec3b(i + 1, j - 1) != buf
  193. src-atVec3b(i, j - 1) != buf
  194. src-atVec3b(i, j + 1) != buf
  195. src-atVec3b(i - 1, j - 1) != buf
  196. src-atVec3b(i - 1, j) != buf
  197. src-atVec3b(i - 1, j + 1) != buf)
  198. P++;
  199.  
  200. if (ir_max)r_max = i;
  201. if (ir_min)r_min = i;
  202. if (jc_max)c_max = j;
  203. if (jc_min)c_min = j;
  204. }
  205. }
  206. }
  207.  
  208. com_rows = ((float)1 area)com_rows;
  209. com_cols = ((float)1 area)com_cols;
  210. for (int i = 0; i src-rows; i++){
  211. for (int j = 0; j src-cols; j++){
  212. if (src-atVec3b(i, j) == buf)
  213. {
  214. V += ((i - com_rows)(j - com_cols));
  215. W += ((j - com_cols)(j - com_cols) - (i - com_rows)(i - com_rows));
  216. }
  217. }
  218. }
  219. printf(Area is %dn, area);
  220. printf(Center of mass is (%f,%f)n, com_rows, com_cols);
  221. V = 2;
  222. fi = 0.5atan2(V, W);
  223. fi = fi 180 CV_PI;
  224. if (fi 0)fi = fi + 180;
  225. printf(Axis of elongation is %f degreesn, fi);
  226. P = (CV_PI 4)P;
  227. printf(Perimeter is %fn, P);
  228. T = (4 CV_PI)(area (PP));
  229. printf(Thinness ratio is %fn, T);
  230. printf(Aspect ratio is %fn, (c_max - c_min + 1) (r_max - r_min + 1));
  231.  
  232. }
  233. trasam axa de alungire
  234. A(cmin,rbara+tg(fi)(cmin-cbara))
  235. B(cmax,rbara+rg(fi)(cmax-cbara))
  236. line(img,point(xa,ya),point(xb,yb),CV_RGB(130,70,240),2);
  237. Mat d=s.clone() face copie la imagine
  238. proiectie pe orizontala cu sir de frecventa lipim de stanga, verticala de pamant
  239. }
  240.  
  241. void testMouseClick()
  242. {
  243. Mat src;
  244. Read image from file
  245. char fname[MAX_PATH];
  246. while (openFileDlg(fname))
  247. {
  248. src = imread(fname);
  249. Create a window
  250. namedWindow(My Window, 1);
  251.  
  252. set the callback function for any mouse event
  253. setMouseCallback(My Window, MyCallBackFunc, &src);
  254.  
  255. show the image
  256. imshow(My Window, src);
  257.  
  258. Wait until user press some key
  259. waitKey(0);
  260. }
  261. }
  262.  
  263. Histogram display function - display a histogram using bars (simlilar to L3 PI)
  264. Input
  265. name - destination (output) window name
  266. hist - pointer to the vector containing the histogram values
  267. hist_cols - no. of bins (elements) in the histogram = histogram image width
  268. hist_height - height of the histogram image
  269. Call example
  270. showHistogram (MyHist, hist_dir, 255, 200);
  271.  
  272. void showHistogram(const string& name, int hist, const int hist_cols, const int hist_height)
  273. {
  274. Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); constructs a white image
  275.  
  276. computes histogram maximum
  277. int max_hist = 0;
  278. for (int i = 0; ihist_cols; i++)
  279. if (hist[i] max_hist)
  280. max_hist = hist[i];
  281. double scale = 1.0;
  282. scale = (double)hist_height max_hist;
  283. int baseline = hist_height - 1;
  284.  
  285. for (int x = 0; x hist_cols; x++) {
  286. Point p1 = Point(x, baseline);
  287. Point p2 = Point(x, baseline - cvRound(hist[x] scale));
  288. line(imgHist, p1, p2, CV_RGB(255, 0, 255)); histogram bins colored in magenta
  289. }
  290.  
  291. imshow(name, imgHist);
  292. }
  293.  
  294. void negative_image(){
  295. uchar pixel;
  296. char fname[10000];
  297. while (openFileDlg(fname))
  298. {
  299. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  300.  
  301. Mat img = imread(fname, -1);
  302. for (int i = 0; i img.rows; i++)
  303. {
  304. for (int j = 0; j img.cols; j++)
  305. {
  306. img.atuchar(i, j) = 255 - img.atuchar(i, j);
  307. }
  308. }
  309.  
  310.  
  311. imshow(negative image, img);
  312. waitKey(0);
  313. }
  314. }
  315.  
  316. void additive_grey_change(){
  317. int val;
  318. char fname[10000];
  319. while (openFileDlg(fname))
  320. {
  321. printf(introdu valoarean);
  322. stdcin val;
  323. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  324.  
  325. imshow(My Window, img);
  326. Mat img = imread(fname, -1);
  327. for (int i = 0; i img.rows; i++)
  328. {
  329. for (int j = 0; j img.cols; j++)
  330. {
  331. if ((int)img.atuchar(i, j) + (int)val 255)
  332. {
  333. img.atuchar(i, j) = 255;
  334. }
  335. else{
  336. img.atuchar(i, j) = img.atuchar(i, j) + val;
  337. }
  338. }
  339. }
  340. imshow(gray change, img);
  341. waitKey(0);
  342. }
  343. }
  344. void color_matrix(){
  345. float vals[256 256];
  346. Mat img(256, 256, CV_8UC3, vals);
  347. for (int i = 0; i img.rows; i++)
  348. {
  349. for (int j = 0; j img.cols; j++)
  350. {
  351. if (i 128 && j 128)
  352. img.atVec3b(i, j) = Vec3b(255, 255, 255);
  353. if (i 128 && j 128)
  354. img.atVec3b(i, j) = Vec3b(0, 0, 255);
  355. if (i 128 && j 128)
  356. img.atVec3b(i, j) = Vec3b(0, 255, 0);
  357. if (i 128 && j 128)
  358. img.atVec3b(i, j) = Vec3b(0, 255, 255);
  359.  
  360. }
  361. }
  362. imshow(window, img);
  363. waitKey(0);
  364. }
  365. void compute_inverse(){
  366.  
  367. float vals[9] = { 1, 2, 0, 4, 1, 0, 0, 0, 1 };
  368. Mat M(3, 3, CV_32F, vals);
  369. stdcout M stdendl;
  370. M = M.inv();
  371. stdcout M stdendl;
  372. stdgetchar();
  373. stdgetchar();
  374.  
  375. }
  376. void split_channels(){
  377. char fname[10000];
  378.  
  379. while (openFileDlg(fname))
  380. {
  381.  
  382. Mat img = imread(fname, CV_LOAD_IMAGE_COLOR);
  383. imshow(original image, img);
  384. Mat imR(img.rows, img.cols, CV_8UC1), imG(img.rows, img.cols, CV_8UC1), imB(img.rows, img.cols, CV_8UC1);
  385. for (int i = 0; i img.rows; i++)
  386. {
  387. for (int j = 0; j img.cols; j++)
  388. {
  389. imR.atuchar(i, j) = img.atVec3b(i, j)[2];
  390. imG.atuchar(i, j) = img.atVec3b(i, j)[1];
  391. imB.atuchar(i, j) = img.atVec3b(i, j)[0];
  392. }
  393. }
  394. imshow(R, imR);
  395. imshow(G, imG);
  396. imshow(B, imB);
  397. waitKey(0);
  398. }
  399. }
  400. void color_to_grayscale(){
  401. char fname[10000];
  402.  
  403. while (openFileDlg(fname))
  404. {
  405.  
  406. Mat img = imread(fname, CV_LOAD_IMAGE_COLOR);
  407. imshow(original image, img);
  408. Mat imgr(img.rows, img.cols, CV_8UC1);
  409. for (int i = 0; i img.rows; i++)
  410. {
  411. for (int j = 0; j img.cols; j++)
  412. {
  413. imgr.atuchar(i, j) = (img.atVec3b(i, j)[2] + img.atVec3b(i, j)[1] + img.atVec3b(i, j)[0]) 3;
  414. }
  415. }
  416. imshow(grayscale, imgr);
  417. waitKey(0);
  418. }
  419. }
  420. void grayscale_to_bw(){
  421. char fname[10000];
  422.  
  423. while (openFileDlg(fname))
  424. {
  425.  
  426. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  427. imshow(original image, img);
  428. Mat imgr(img.rows, img.cols, CV_8UC1);
  429. for (int i = 0; i img.rows; i++)
  430. {
  431. for (int j = 0; j img.cols; j++)
  432. {
  433. if (img.atuchar(i, j) 160){
  434. imgr.atuchar(i, j) = 255;
  435. }
  436. else{
  437. imgr.atuchar(i, j) = 0;
  438. }
  439. }
  440. }
  441. imshow(black and white, imgr);
  442. waitKey(0);
  443. }
  444. }
  445. void rgb_to_hsv(){
  446. char fname[10000];
  447. float r, g, b, M, m, C, H, S, V;
  448. while (openFileDlg(fname))
  449. {
  450.  
  451. Mat img = imread(fname, CV_LOAD_IMAGE_COLOR);
  452. imshow(original image, img);
  453. Mat imH(img.rows, img.cols, CV_8UC1), imS(img.rows, img.cols, CV_8UC1), imV(img.rows, img.cols, CV_8UC1);
  454. for (int i = 0; i img.rows; i++)
  455. {
  456. for (int j = 0; j img.cols; j++)
  457. {
  458. r = (float)img.atVec3b(i, j)[2] 255;
  459. g = (float)img.atVec3b(i, j)[1] 255;
  460. b = (float)img.atVec3b(i, j)[0] 255;
  461.  
  462. M = max(max(r, g), b);
  463. m = min(min(r, g), b);
  464. C = M - m;
  465.  
  466. V = M;
  467.  
  468. if (C)
  469. S = C V;
  470. else
  471. S = 0;
  472.  
  473. if (C){
  474. if (M == r)H = 60 (g - b) C;
  475. if (M == g)H = 120 (b - r) C;
  476. if (M == b)H = 240 (r - g) C;
  477. }
  478. else{
  479. H = 0;
  480. }
  481. if (H 0)H = H + 360;
  482.  
  483. imH.atuchar(i, j) = H 255 360;
  484. imS.atuchar(i, j) = S 255;
  485. imV.atuchar(i, j) = V 255;
  486. }
  487. }
  488. imshow(H, imH);
  489. imshow(S, imS);
  490. imshow(V, imV);
  491. waitKey(0);
  492. }
  493. }
  494. int histValues[256];
  495. float normalizedHistoValues[256];
  496.  
  497.  
  498. void histogram(){
  499.  
  500. char fname[10000];
  501.  
  502. while (openFileDlg(fname))
  503. {
  504.  
  505. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  506. imshow(original image, img);
  507.  
  508. for (int i = 0; i img.rows; i++)
  509. {
  510. for (int j = 0; j img.cols; j++)
  511. {
  512. histValues[img.atuchar(i, j)]++;
  513. }
  514. }
  515. showHistogram(MyHist, histValues, 256, 500);
  516. for (int i = 0; i 256; i++)
  517. {
  518. histValues[i] = 0;
  519. }
  520. }
  521.  
  522. }
  523. int result[256];
  524. void normalizeHistogram(int histogram, int size){
  525.  
  526. for (int i = 0; i 256; i++)
  527. normalizedHistoValues[i] = (float)histValues[i] size;
  528.  
  529. }
  530. void ml_thresholding(){
  531. int WH = 5;
  532. float TH = 0.0003;
  533. float miu;
  534. int dimension = 1;
  535. boolean ok;
  536. char fname[10000];
  537.  
  538. while (openFileDlg(fname))
  539. {
  540.  
  541. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  542. imshow(original image, img);
  543.  
  544. for (int i = 0; i img.rows; i++)
  545. {
  546. for (int j = 0; j img.cols; j++)
  547. {
  548. histValues[img.atuchar(i, j)]++;
  549. }
  550. }
  551. normalizeHistogram(histValues, img.rowsimg.cols);
  552.  
  553. for (int k = 0 + WH; k 255 - WH; k++){
  554. miu = 0;
  555. ok = true;
  556. for (int i = k - WH; i k + WH; i++){
  557. miu += normalizedHistoValues[i];
  558. if (normalizedHistoValues[k] normalizedHistoValues[i])ok = false;
  559. }
  560. miu = miu (2 WH + 1);
  561. if (normalizedHistoValues[k] miu + TH &&ok){
  562. result[dimension++] = k;
  563.  
  564. }
  565. }
  566. result[0] = 0;
  567. result[dimension] = 255;
  568. dimension++;
  569. for (int i = 0; i dimension; i++){
  570. stdcout result[i] n stdendl;
  571. }
  572.  
  573. for (int i = 0; i img.rows; i++)
  574. {
  575. for (int j = 0; j img.cols; j++)
  576. {
  577. int maxx = -1;
  578. for (int k = 0; k dimension - 1; k++){
  579. if (img.atuchar(i, j)result[k] && img.atuchar(i, j)result[k + 1]){
  580. if (img.atuchar(i, j) - result[k] result[k + 1] - img.atuchar(i, j))img.atuchar(i, j) = result[k];
  581. else img.atuchar(i, j) = result[k + 1];
  582. }
  583. }
  584.  
  585. }
  586. }
  587. imshow(thresholded image, img);
  588. }
  589. }
  590. uchar limit_value(int value){
  591. if (value 0)return 0;
  592. else if (value 255)return 255;
  593. else return value;
  594. }
  595. void floyd_steinberg(){
  596. int WH = 5;
  597. float TH = 0.0003;
  598. float miu;
  599. int dimension = 1;
  600. boolean ok;
  601. char fname[10000];
  602.  
  603. while (openFileDlg(fname))
  604. {
  605.  
  606. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  607. imshow(original image, img);
  608.  
  609. for (int i = 0; i img.rows; i++)
  610. {
  611. for (int j = 0; j img.cols; j++)
  612. {
  613. histValues[img.atuchar(i, j)]++;
  614. }
  615. }
  616. normalizeHistogram(histValues, img.rowsimg.cols);
  617.  
  618. for (int k = 0 + WH; k 255 - WH; k++){
  619. miu = 0;
  620. ok = true;
  621. for (int i = k - WH; i k + WH; i++){
  622. miu += normalizedHistoValues[i];
  623. if (normalizedHistoValues[k] normalizedHistoValues[i])ok = false;
  624. }
  625. miu = miu (2 WH + 1);
  626. if (normalizedHistoValues[k] miu + TH &&ok){
  627. result[dimension++] = k;
  628.  
  629. }
  630. }
  631. result[0] = 0;
  632. result[dimension] = 255;
  633. dimension++;
  634. for (int i = 0; i dimension; i++){
  635. stdcout result[i] n stdendl;
  636. }
  637.  
  638. for (int i = 0; i img.rows - 1; i++)
  639. {
  640. for (int j = 1; j img.cols - 1; j++)
  641. {
  642. int oldValue = img.atuchar(i, j);
  643. int newValue;
  644. float error;
  645. for (int k = 0; k dimension - 1; k++){
  646. if (img.atuchar(i, j)result[k] && img.atuchar(i, j)result[k + 1]){
  647. if (img.atuchar(i, j) - result[k] result[k + 1] - img.atuchar(i, j))newValue = result[k];
  648. else newValue = result[k + 1];
  649. }
  650. }
  651. error = oldValue - newValue;
  652.  
  653. img.atuchar(i, j) = newValue;
  654. img.atuchar(i, j + 1) = img.atuchar(i, j + 1) + limit_value((int)(7 error 16));
  655. img.atuchar(i + 1, j - 1) = img.atuchar(i + 1, j - 1) + limit_value((int)(3 error 16));
  656. img.atuchar(i + 1, j) = img.atuchar(i + 1, j) + limit_value((int)(5 error 16));
  657. img.atuchar(i + 1, j + 1) = img.atuchar(i + 1, j + 1) + limit_value((int)(error 16));
  658.  
  659. }
  660. }
  661. imshow(floyd image, img);
  662.  
  663.  
  664. }
  665. }
  666.  
  667. void geom_feat()
  668. {
  669. Mat src;
  670. Read image from file
  671. char fname[MAX_PATH] = CUsersStudentDownloadsMultiple Objectsgeometrical_features.bmp;
  672. src = imread(fname, CV_LOAD_IMAGE_COLOR);
  673. Create a window
  674. namedWindow(My Window, 1);
  675.  
  676. set the callback function for any mouse event
  677. setMouseCallback(My Window, MyCallBackFunc, &src);
  678.  
  679. show the image
  680. imshow(My Window, src);
  681.  
  682. Wait until user press some key
  683. waitKey(0);
  684. }
  685.  
  686. void BFS_traversal(){
  687. char fname[10000];
  688.  
  689. while (openFileDlg(fname))
  690. {
  691. int label = 0;
  692. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  693. imshow(original image, img);
  694. Mat labels(img.rows, img.cols, CV_32SC1);
  695. for (int i = 0; i img.rows; i++)
  696. {
  697. for (int j = 0; j img.cols; j++)
  698. {
  699. labels.atint(i, j) = 0;
  700. }
  701. }
  702. Mat colored(img.rows, img.cols, CV_8UC3);
  703. for (int i = 0; i img.rows; i++)
  704. {
  705. for (int j = 0; j img.cols; j++)
  706. {
  707. if (img.atuchar(i, j) == 0 && labels.atint(i, j) == 0){
  708. label++;
  709. stdqueuePoint2i Q;
  710. labels.atint(i, j) = label;
  711. Q.push({ i, j });
  712. while (!Q.empty()){
  713. Point2i p = Q.front();
  714. Q.pop();
  715. int dx[10] = { -1, 0, 1, -1, 1, -1, 0, 1 };
  716. int dy[10] = { -1, -1, -1, 0, 0, 1, 1, 1 };
  717.  
  718. for (int k = 0; k 8; k++){
  719. if (img.atuchar(p.x + dx[k], p.y + dy[k]) == 0 && labels.atint(p.x + dx[k], p.y + dy[k]) == 0){
  720. labels.atint(p.x + dx[k], p.y + dy[k]) = label;
  721. Q.push({ p.x + dx[k], p.y + dy[k] });
  722. }
  723. }
  724.  
  725.  
  726. }
  727.  
  728. }
  729. }
  730. }
  731. stddefault_random_engine gen;
  732. stduniform_int_distributionint d(0, 255);
  733. Vec3b colors[1000];
  734. for (int i = 1; i = label; i++)
  735. {
  736. colors[i][0] = d(gen);
  737. colors[i][1] = d(gen);
  738. colors[i][2] = d(gen);
  739. }
  740. for (int i = 0; i img.rows; i++)
  741. {
  742. for (int j = 0; j img.cols; j++)
  743. {
  744. if (labels.atint(i, j) 0){
  745. colored.atVec3b(i, j) = colors[labels.atint(i, j)];
  746. }
  747. }
  748. }
  749. imshow(colored labeled img, colored);
  750. waitKey(0);
  751. }
  752. }
  753.  
  754. void two_pass_traversal(){
  755. char fname[10000];
  756. int dx[10] = { -1, 0, 1, -1 };
  757. int dy[10] = { -1, -1, -1, 0, };
  758. stdvectorstdvectorint edges;
  759. while (openFileDlg(fname))
  760. {
  761. int newLabels[1000] = {};
  762. int label = 0;
  763. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  764. imshow(original image, img);
  765. Mat labels(img.rows, img.cols, CV_32SC1);
  766. for (int i = 0; i img.rows; i++)
  767. {
  768. for (int j = 0; j img.cols; j++)
  769. {
  770. labels.atint(i, j) = 0;
  771. }
  772. }
  773. Mat colored(img.rows, img.cols, CV_8UC3);
  774. for (int i = 0; i img.rows; i++)
  775. {
  776. for (int j = 0; j img.cols; j++)
  777. {
  778. stdvectorint L;
  779. if (img.atuchar(i, j) == 0 && labels.atint(i, j) == 0){
  780. for (int k = 0; k 4; k++){
  781. if (labels.atint(i + dx[k], j + dy[k]) 0){
  782. L.push_back(labels.atint(i + dx[k], j + dy[k]));
  783. }
  784. }
  785.  
  786. if (L.size() == 0){
  787. label++;
  788. edges.resize(label + 1);
  789. labels.atint(i, j) = label;
  790. }
  791. else{
  792. int x = 10000;
  793. for (int k = 0; k L.size(); k++)
  794. {
  795. if (L[k] x)x = L[k];
  796. }
  797. labels.atint(i, j) = x;
  798. for (int k = 0; k L.size(); k++)
  799. {
  800. if (L[k] != x){
  801. edges[x].push_back(L[k]);
  802. edges[L[k]].push_back(x);
  803. }
  804. }
  805. }
  806. }
  807. }
  808. }
  809. int newlabel = 0;
  810. for (int i = 0; i label; i++){
  811. if (newLabels[i] == 0){
  812. newlabel++;
  813. stdqueueint Q;
  814. newLabels[i] = newlabel;
  815. Q.push(i);
  816. while (!Q.empty()){
  817. int x = Q.front();
  818. Q.pop();
  819. for (int j = 0; j edges[x].size(); j++){
  820. if (newLabels[edges[x][j]] == 0){
  821. newLabels[edges[x][j]] = newlabel;
  822. Q.push(edges[x][j]);
  823. }
  824. }
  825. }
  826. }
  827. }
  828. for (int i = 0; i img.rows; i++)
  829. {
  830. for (int j = 0; j img.cols; j++)
  831. {
  832. if (labels.atint(i, j)0)
  833. labels.atint(i, j) = newLabels[labels.atint(i, j)];
  834. }
  835. stdcout stdendl;
  836. }
  837.  
  838. stddefault_random_engine gen;
  839. stduniform_int_distributionint d(0, 255);
  840. Vec3b colors[1000];
  841. for (int i = 1; i = label; i++)
  842. {
  843. colors[i][0] = d(gen);
  844. colors[i][1] = d(gen);
  845. colors[i][2] = d(gen);
  846. }
  847. for (int i = 0; i img.rows; i++)
  848. {
  849. for (int j = 0; j img.cols; j++)
  850. {
  851. if (labels.atint(i, j) 0){
  852. colored.atVec3b(i, j) = colors[labels.atint(i, j)];
  853. }
  854. }
  855. }
  856. imshow(colored labeled img, colored);
  857. waitKey(0);
  858. }
  859. }
  860. int ac[100000], dc[1000000];
  861. void border_tracing(){
  862. char fname[10000];
  863. int dx[10] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  864. int dy[10] = { 0, -1, -1, -1, 0, 1, 1, 1 };
  865. Point pn, pn_1, p1, p0;
  866. int n = 0, dir = 7, k = 1;
  867. int ok = 0;
  868. ac[0] = 7;
  869. while (openFileDlg(fname))
  870. {
  871.  
  872. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  873. imshow(original image, img);
  874. Mat imgr(img.rows, img.cols, CV_8UC1);
  875. for (int i = 0; i imgr.rows; i++)
  876. {
  877. for (int j = 0; j imgr.cols; j++)
  878. {
  879. imgr.atuchar(i, j) = 0;
  880. }
  881. }
  882. for (int i = 0; i img.rows; i++)
  883. {
  884. for (int j = 0; j img.cols; j++)
  885. {
  886. if (img.atuchar(i, j) 150){
  887. p0.x = j;
  888. p0.y = i;
  889. pn = p0;
  890. do{
  891. ok = 1;
  892. if (dir % 2 == 0){
  893. dir = (dir + 7) % 8;
  894. }
  895. else{
  896. dir = (dir + 6) % 8;
  897. }
  898.  
  899. while (img.atuchar(pn.y + dy[dir], pn.x + dx[dir]) != 0){
  900. dir = (dir + 1) % 8;
  901. }
  902. ac[k] = dir;
  903. dc[k++] = (ac[k] - ac[k - 1] + 8) % 8;
  904. pn_1 = pn;
  905. pn.x = pn.x + dx[dir];
  906. pn.y = pn.y + dy[dir];
  907. imgr.atuchar(pn.y, pn.x) = 255;
  908. if (n == 0)p1 = pn;
  909. n++;
  910. } while (!((pn == p1) && (pn_1 == p0) && (n = 2)));
  911. }
  912. if (ok == 1)j = img.cols;
  913. }
  914. if (ok == 1)i = img.rows;
  915. }
  916. stdcout ac ;
  917. for (int i = 0; i k; i++){
  918. stdcout ac[i] ;
  919. }
  920. stdcout stdendl dc ;
  921. for (int i = 0; i k; i++){
  922. stdcout dc[i] ;
  923. }
  924. imshow(grayscale, imgr);
  925. waitKey(0);
  926. }
  927. }
  928.  
  929. void dilation(){
  930. char fname[10000];
  931.  
  932. while (openFileDlg(fname))
  933. {
  934.  
  935. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  936. imshow(original image, img);
  937. Mat dilated(img.rows, img.cols, CV_8UC1, Scalar(255));
  938. Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
  939. int n;
  940. stdcout n is;
  941. stdcin n;
  942. img.copyTo(intermediary);
  943. for (int k = 0; k n; k++){
  944.  
  945. for (int i = 0; i img.rows; i++)
  946. {
  947. for (int j = 0; j img.cols; j++)
  948. {
  949. if (intermediary.atuchar(i, j) == 0){
  950. dilated.atuchar(i, j) = 0;
  951. dilated.atuchar(i + 1, j) = 0;
  952. dilated.atuchar(i - 1, j) = 0;
  953. dilated.atuchar(i, j + 1) = 0;
  954. dilated.atuchar(i, j - 1) = 0;
  955. }
  956. }
  957. }
  958. dilated.copyTo(intermediary);
  959. dilated(img.rows, img.cols, CV_8UC1, Scalar(255));
  960. }
  961. imshow(dilated, dilated);
  962. waitKey(0);
  963. }
  964. }
  965.  
  966. void erosion(){
  967. char fname[10000];
  968.  
  969. while (openFileDlg(fname))
  970. {
  971.  
  972. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  973. imshow(original image, img);
  974. Mat eroded(img.rows, img.cols, CV_8UC1, Scalar(255));
  975. Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
  976. int n;
  977. stdcout n is;
  978. stdcin n;
  979. img.copyTo(intermediary);
  980. for (int k = 0; k n; k++){
  981.  
  982. for (int i = 1; i img.rows - 1; i++)
  983. {
  984. for (int j = 1; j img.cols - 1; j++)
  985. {
  986. if (intermediary.atuchar(i, j) == 0 && intermediary.atuchar(i + 1, j) == 0 && intermediary.atuchar(i - 1, j) == 0 && intermediary.atuchar(i, j + 1) == 0 && intermediary.atuchar(i, j - 1) == 0){
  987. eroded.atuchar(i, j) = 0;
  988.  
  989. }
  990. }
  991. }
  992. eroded.copyTo(intermediary);
  993. for (int i = 0; i img.rows; i++)
  994. {
  995. for (int j = 0; j img.cols; j++)
  996. {
  997. eroded.atuchar(i, j) = 255;
  998. }
  999. }
  1000. }
  1001. imshow(eroded, intermediary);
  1002. waitKey(0);
  1003. }
  1004. }
  1005. void opening(){
  1006. char fname[10000];
  1007.  
  1008. while (openFileDlg(fname))
  1009. {
  1010.  
  1011. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1012. imshow(original image, img);
  1013. Mat eroded(img.rows, img.cols, CV_8UC1, Scalar(255));
  1014. Mat dilated(img.rows, img.cols, CV_8UC1, Scalar(255));
  1015. Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
  1016. img.copyTo(intermediary);
  1017. for (int k = 0; k n; k++){
  1018.  
  1019. for (int i = 1; i img.rows - 1; i++)
  1020. {
  1021. for (int j = 1; j img.cols - 1; j++)
  1022. {
  1023. if (intermediary.atuchar(i, j) == 0 && intermediary.atuchar(i + 1, j) == 0 && intermediary.atuchar(i - 1, j) == 0 && intermediary.atuchar(i, j + 1) == 0 && intermediary.atuchar(i, j - 1) == 0){
  1024. eroded.atuchar(i, j) = 0;
  1025.  
  1026. }
  1027. }
  1028. }
  1029. eroded.copyTo(intermediary);
  1030. for (int i = 1; i img.rows - 1; i++)
  1031. {
  1032. for (int j = 1; j img.cols - 1; j++)
  1033. {
  1034. if (intermediary.atuchar(i, j) == 0){
  1035. dilated.atuchar(i, j) = 0;
  1036. dilated.atuchar(i + 1, j) = 0;
  1037. dilated.atuchar(i - 1, j) = 0;
  1038. dilated.atuchar(i, j + 1) = 0;
  1039. dilated.atuchar(i, j - 1) = 0;
  1040. }
  1041. }
  1042. }
  1043. }
  1044. imshow(opening, dilated);
  1045. waitKey(0);
  1046. }
  1047. }
  1048.  
  1049. void closing(){
  1050. char fname[10000];
  1051.  
  1052. while (openFileDlg(fname))
  1053. {
  1054.  
  1055. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1056. imshow(original image, img);
  1057. Mat eroded(img.rows, img.cols, CV_8UC1, Scalar(255));
  1058. Mat dilated(img.rows, img.cols, CV_8UC1, Scalar(255));
  1059. Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
  1060. img.copyTo(intermediary);
  1061. for (int k = 0; k n; k++){
  1062.  
  1063. for (int i = 1; i img.rows - 1; i++)
  1064. {
  1065. for (int j = 1; j img.cols - 1; j++)
  1066. {
  1067. if (intermediary.atuchar(i, j) == 0){
  1068. dilated.atuchar(i, j) = 0;
  1069. dilated.atuchar(i + 1, j) = 0;
  1070. dilated.atuchar(i - 1, j) = 0;
  1071. dilated.atuchar(i, j + 1) = 0;
  1072. dilated.atuchar(i, j - 1) = 0;
  1073. }
  1074. }
  1075. }
  1076. dilated.copyTo(intermediary);
  1077. for (int i = 1; i img.rows - 1; i++)
  1078. {
  1079. for (int j = 1; j img.cols - 1; j++)
  1080. {
  1081. if (intermediary.atuchar(i, j) == 0 && intermediary.atuchar(i + 1, j) == 0 && intermediary.atuchar(i - 1, j) == 0 && intermediary.atuchar(i, j + 1) == 0 && intermediary.atuchar(i, j - 1) == 0){
  1082. eroded.atuchar(i, j) = 0;
  1083.  
  1084. }
  1085. }
  1086. }
  1087.  
  1088.  
  1089. }
  1090. imshow(closing, eroded);
  1091. waitKey(0);
  1092. }
  1093. }
  1094.  
  1095. void boundary(){
  1096. char fname[10000];
  1097.  
  1098. while (openFileDlg(fname))
  1099. {
  1100.  
  1101. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1102. imshow(original image, img);
  1103. Mat boundary(img.rows, img.cols, CV_8UC1, Scalar(255));
  1104. Mat eroded(img.rows, img.cols, CV_8UC1, Scalar(255));
  1105. Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
  1106. img.copyTo(intermediary);
  1107. for (int k = 0; k n; k++){
  1108.  
  1109. for (int i = 1; i img.rows - 1; i++)
  1110. {
  1111. for (int j = 1; j img.cols - 1; j++)
  1112. {
  1113. if (intermediary.atuchar(i, j) == 0 && intermediary.atuchar(i + 1, j) == 0 && intermediary.atuchar(i - 1, j) == 0 && intermediary.atuchar(i, j + 1) == 0 && intermediary.atuchar(i, j - 1) == 0){
  1114. eroded.atuchar(i, j) = 0;
  1115.  
  1116. }
  1117. }
  1118. }
  1119. dilated.copyTo(intermediary);
  1120. for (int i = 1; i img.rows - 1; i++)
  1121. {
  1122. for (int j = 1; j img.cols - 1; j++)
  1123. {
  1124. if (img.atuchar(i, j) == 0 && eroded.atuchar(i, j) != 0){
  1125. boundary.atuchar(i, j) = 0;
  1126.  
  1127. }
  1128. }
  1129. }
  1130.  
  1131.  
  1132. }
  1133. imshow(boundary, boundary);
  1134. waitKey(0);
  1135. }
  1136. }
  1137. void filling(){
  1138. char fname[10000];
  1139. Point2d p = Point2d(250, 100);
  1140. while (openFileDlg(fname))
  1141. {
  1142.  
  1143. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1144. imshow(original image, img);
  1145. Mat expansion(img.rows, img.cols, CV_8UC1, Scalar(255));
  1146. Mat final(img.rows, img.cols, CV_8UC1, Scalar(255));
  1147. Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
  1148. intermediary.atuchar(100, 250) = 0;
  1149. for (int k = 0; k n; k++){
  1150. int ok;
  1151. do
  1152. {
  1153. ok = 0;
  1154. for (int i = 1; i img.rows - 1; i++)
  1155. {
  1156. for (int j = 1; j img.cols - 1; j++)
  1157. {
  1158. if (intermediary.atuchar(i, j) == 0){
  1159. if (img.atuchar(i, j) != 0){
  1160. expansion.atuchar(i, j) = 0;
  1161. }
  1162. if (img.atuchar(i + 1, j) != 0){ expansion.atuchar(i + 1, j) = 0; }
  1163. if (img.atuchar(i - 1, j) != 0){ expansion.atuchar(i - 1, j) = 0; }
  1164. if (img.atuchar(i, j + 1) != 0){ expansion.atuchar(i, j + 1) = 0; }
  1165. if (img.atuchar(i, j - 1) != 0){ expansion.atuchar(i, j - 1) = 0; }
  1166. }
  1167. }
  1168. }
  1169. for (int i = 0; i img.rows; i++)
  1170. {
  1171. for (int j = 0; j img.cols; j++)
  1172. {
  1173. if (intermediary.atuchar(i, j) != expansion.atuchar(i, j))
  1174. ok = 1;
  1175. }
  1176. }
  1177. expansion.copyTo(intermediary);
  1178. } while (ok);
  1179. for (int i = 1; i img.rows - 1; i++)
  1180. {
  1181. for (int j = 1; j img.cols - 1; j++)
  1182. {
  1183. if (img.atuchar(i, j) == 0 intermediary.atuchar(i, j) == 0){
  1184. final.atuchar(i, j) = 0;
  1185.  
  1186. }
  1187. }
  1188. }
  1189.  
  1190.  
  1191. }
  1192. imshow(fill, final);
  1193. waitKey(0);
  1194. }
  1195. }
  1196. float mean(char fname[]){
  1197. float miu;
  1198. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1199. imshow(original image, img);
  1200. int sum = 0;
  1201. for (int i = 0; i img.rows; i++)
  1202. {
  1203. for (int j = 0; j img.cols; j++)
  1204. {
  1205. sum += img.atuchar(i, j);
  1206. }
  1207. }
  1208. miu = (float)(sum (img.rowsimg.cols));
  1209. return miu;
  1210. }
  1211. void mean_and_std_dev(){
  1212. char fname[10000];
  1213. float miu, st_dev;
  1214. while (openFileDlg(fname))
  1215. {
  1216.  
  1217. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1218. imshow(original image, img);
  1219. int sum = 0;
  1220. for (int i = 0; i img.rows; i++)
  1221. {
  1222. for (int j = 0; j img.cols; j++)
  1223. {
  1224. sum += img.atuchar(i, j);
  1225. }
  1226. }
  1227. miu = (float)(sum (img.rowsimg.cols));
  1228. stdcout mean isn;
  1229. stdcout miu stdendl;
  1230. sum = 0;
  1231. for (int i = 0; i img.rows; i++)
  1232. {
  1233. for (int j = 0; j img.cols; j++)
  1234. {
  1235. sum += (img.atuchar(i, j) - miu)(img.atuchar(i, j) - miu);
  1236. }
  1237. }
  1238. st_dev = sqrt(sum (img.rowsimg.cols));
  1239. stdcout std dev isn;
  1240. stdcout st_dev stdendl;
  1241. waitKey(0);
  1242. }
  1243. }
  1244. void basic_global_thresh(){
  1245. char fname[10000];
  1246. float miu, epsilon = 1, miu1 = 0, miu2 = 0;
  1247. int imin = 256, imax = 0;
  1248. int t0, t1;
  1249. while (openFileDlg(fname))
  1250. {
  1251. int histValues[256] = { 0 };
  1252.  
  1253. miu = mean(fname);
  1254. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1255. for (int i = 0; i img.rows; i++)
  1256. {
  1257. for (int j = 0; j img.cols; j++)
  1258. {
  1259. histValues[img.atuchar(i, j)]++;
  1260. }
  1261. }
  1262. for (int i = 0; i img.rows; i++)
  1263. {
  1264. for (int j = 0; j img.cols; j++)
  1265. {
  1266. if (img.atuchar(i, j) imin)imin = img.atuchar(i, j);
  1267. if (img.atuchar(i, j) imax)imax = img.atuchar(i, j);
  1268. }
  1269. }
  1270. t0 = (imin + imax) 2;
  1271. t1 = t0 + 3;
  1272. while (t1 - t01 t1 - t0 -1)
  1273. {
  1274. miu1 = 0;
  1275. miu2 = 0;
  1276. int n1 = 0, n2 = 0;
  1277. for (int i = 0; i t0; i++)
  1278. {
  1279. miu1 += ihistValues[i];
  1280. n1 += histValues[i];
  1281. }
  1282. for (int i = t0; i 255; i++)
  1283. {
  1284. miu2 += i histValues[i];
  1285. n2 += histValues[i];
  1286. }
  1287. miu1 = miu1 n1;
  1288. miu2 = miu2 n2;
  1289. t0 = t1;
  1290. t1 = (miu1 + miu2) 2;
  1291.  
  1292. }
  1293. stdcout t0;
  1294. for (int i = 0; i img.rows; i++)
  1295. {
  1296. for (int j = 0; j img.cols; j++)
  1297. {
  1298. if (img.atuchar(i, j) t0)
  1299. img.atuchar(i, j) = 0;
  1300. else
  1301. img.atuchar(i, j) = 255;
  1302. }
  1303. }
  1304. imshow(alb negru, img);
  1305. }
  1306. }
  1307. void bright_change(){
  1308. char fname[10000];
  1309.  
  1310. while (openFileDlg(fname))
  1311. {
  1312. int n;
  1313. stdcout value isn;
  1314. stdcin n;
  1315. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1316. imshow(original, img);
  1317. for (int i = 0; i img.rows; i++)
  1318. {
  1319. for (int j = 0; j img.cols; j++)
  1320. {
  1321. if (img.atuchar(i, j) + n 255)img.atuchar(i, j) = 255;
  1322. else {
  1323. if (img.atuchar(i, j) + n 0)img.atuchar(i, j) = 0;
  1324. else
  1325. img.atuchar(i, j) += n;
  1326. }
  1327. }
  1328. }
  1329. imshow(modified, img);
  1330.  
  1331. }
  1332. }
  1333. void contrast_change(){
  1334. char fname[10000];
  1335. int imin = 256, imax = 0;
  1336. int a, b, c, d;
  1337. while (openFileDlg(fname))
  1338. {
  1339. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1340. imshow(original, img);
  1341. for (int i = 0; i img.rows; i++)
  1342. {
  1343. for (int j = 0; j img.cols; j++)
  1344. {
  1345. if (img.atuchar(i, j) imin)imin = img.atuchar(i, j);
  1346. if (img.atuchar(i, j) imax)imax = img.atuchar(i, j);
  1347. }
  1348. }
  1349. a = imin;
  1350. b = imax;
  1351. c = 20;
  1352. d = 245;
  1353. for (int i = 0; i img.rows; i++)
  1354. {
  1355. for (int j = 0; j img.cols; j++)
  1356. {
  1357. img.atuchar(i, j) = (c + (img.atuchar(i, j) - a)((float)(d - c) (b - a)));
  1358.  
  1359. }
  1360. }
  1361. imshow(modified, img);
  1362.  
  1363. }
  1364. }
  1365. void gamma_change(){
  1366. char fname[10000];
  1367. while (openFileDlg(fname))
  1368. {
  1369. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1370. imshow(original, img);
  1371. Mat copy = img.clone();
  1372. for (int i = 0; i img.rows; i++)
  1373. {
  1374. for (int j = 0; j img.cols; j++)
  1375. {
  1376. img.atuchar(i, j) = 255 pow((float)img.atuchar(i, j) 255, 0.5);
  1377. copy.atuchar(i, j) = 255 pow((float)copy.atuchar(i, j) 255, 2);
  1378.  
  1379. }
  1380. }
  1381. imshow(reduced gamma, img);
  1382. imshow(increased gamma, copy);
  1383. }
  1384. }
  1385. void histo_eq(){
  1386. Mat img;
  1387. char fname[MAX_PATH];
  1388. openFileDlg(fname);
  1389. img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1390. imshow(original img, img);
  1391. int L = 255;
  1392. int M = img.colsimg.rows;
  1393. int histogramArray[256] = {};
  1394. float PDG[256] = {};
  1395. float CPDG[256] = {};
  1396.  
  1397. for (int i = 0; i img.rows; i++) {
  1398. for (int j = 0; j img.cols; j++) {
  1399. histogramArray[img.atuchar(i, j)] = histogramArray[img.atuchar(i, j)] + 1;
  1400. }
  1401. }
  1402.  
  1403. PDG[0] = (float)histogramArray[0] M;
  1404. CPDG[0] = PDG[0];
  1405. for (int i = 1; i = 255; i++) {
  1406. PDG[i] = (float)histogramArray[i] M;
  1407. CPDG[i] = PDG[i] + CPDG[i - 1];
  1408. }
  1409. for (int i = 0; i img.rows; i++) {
  1410. for (int j = 0; j img.cols; j++) {
  1411. img.atuchar(i, j) = 255 CPDG[img.atuchar(i, j)];
  1412. }
  1413. }
  1414. imshow(result img, img);
  1415. waitKey(0);
  1416. }
  1417. void mean_filter_3x3(){
  1418. char fname[10000];
  1419. while (openFileDlg(fname))
  1420. {
  1421. int filter[3][3] = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
  1422. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1423. imshow(original, img);
  1424. Mat copy = img.clone();
  1425. for (int i = 0; i 3; i++)
  1426. {
  1427. for (int j = 0; j 3; j++)
  1428. {
  1429. stdcout filter.atuchar(i, j) ;
  1430. }
  1431. stdcout stdendl;
  1432. }
  1433. for (int i = 1; i img.rows - 1; i++)
  1434. {
  1435. for (int j = 1; j img.cols - 1; j++)
  1436. {
  1437. int sum = 0;
  1438. for (int k = -1; k = 1; k++){
  1439. for (int l = -1; l = 1; l++){
  1440. sum += img.atuchar(i + k, j + l)filter[1 + k][1 + l];
  1441. }
  1442. }
  1443. copy.atuchar(i, j) = sum 9;
  1444. }
  1445. }
  1446. imshow(blurred, copy);
  1447. }
  1448. }
  1449. void mean_filter_5x5(){
  1450. char fname[10000];
  1451. while (openFileDlg(fname))
  1452. {
  1453. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1454. imshow(original, img);
  1455. Mat copy = img.clone();
  1456.  
  1457. for (int i = 2; i img.rows - 2; i++)
  1458. {
  1459. for (int j = 2; j img.cols - 2; j++)
  1460. {
  1461. int sum = 0;
  1462. for (int k = -2; k = 2; k++){
  1463. for (int l = -2; l = 2; l++){
  1464. sum += img.atuchar(i + k, j + l);
  1465. }
  1466. }
  1467. copy.atuchar(i, j) = sum 25;
  1468. }
  1469. }
  1470. imshow(blurred, copy);
  1471. }
  1472. }
  1473. void gaussian_filter_3x3(){
  1474. char fname[10000];
  1475. while (openFileDlg(fname))
  1476. {
  1477. int filter[3][3] = { { 1, 2, 1 }, { 2, 4, 2 }, { 1, 2, 1 } };
  1478.  
  1479. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1480. imshow(original, img);
  1481. Mat copy = img.clone();
  1482. for (int i = 1; i img.rows - 1; i++)
  1483. {
  1484. for (int j = 1; j img.cols - 1; j++)
  1485. {
  1486. int sum = 0;
  1487. for (int k = -1; k = 1; k++){
  1488. for (int l = -1; l = 1; l++){
  1489. sum += img.atuchar(i + k, j + l)filter[1 + k][1 + l];
  1490. }
  1491. }
  1492. copy.atuchar(i, j) = sum 16;
  1493. }
  1494. }
  1495. imshow(blurred, copy);
  1496. }
  1497. }
  1498. void laplace_filter_3x3(){
  1499. char fname[10000];
  1500. while (openFileDlg(fname))
  1501. {
  1502. int filter[3][3] = { { -1, -1, -1 }, { -1, 8, -1 }, { -1, -1, -1 } };
  1503. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1504. imshow(original, img);
  1505. Mat copy = img.clone();
  1506. for (int i = 1; i img.rows - 1; i++)
  1507. {
  1508. for (int j = 1; j img.cols - 1; j++)
  1509. {
  1510. int sum = 0;
  1511. for (int k = -1; k = 1; k++){
  1512. for (int l = -1; l = 1; l++){
  1513. sum += img.atuchar(i + k, j + l)filter[1 + k][1 + l];
  1514. }
  1515. }
  1516. if (sum 0)sum = 0;
  1517. if (sum 255)sum = 255;
  1518. copy.atuchar(i, j) = sum;
  1519. }
  1520. }
  1521. imshow(blurred, copy);
  1522. }
  1523. }
  1524. void high_pass_filter_3x3(){
  1525. char fname[10000];
  1526. while (openFileDlg(fname))
  1527. {
  1528. int filter[3][3] = { { -1, -1, -1 }, { -1, 9, -1 }, { -1, -1, -1 } };
  1529. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1530. imshow(original, img);
  1531. Mat copy = img.clone();
  1532. for (int i = 1; i img.rows - 1; i++)
  1533. {
  1534. for (int j = 1; j img.cols - 1; j++)
  1535. {
  1536. int sum = 0;
  1537. for (int k = -1; k = 1; k++){
  1538. for (int l = -1; l = 1; l++){
  1539. sum += img.atuchar(i + k, j + l)filter[1 + k][1 + l];
  1540. }
  1541. }
  1542. if (sum 0)sum = 0;
  1543. if (sum 255)sum = 255;
  1544. copy.atuchar(i, j) = sum;
  1545. }
  1546. }
  1547. imshow(blurred, copy);
  1548. }
  1549. }
  1550. void centering_transform(Mat img) {
  1551. for (int i = 0; i img.rows; i++) {
  1552. for (int j = 0; j img.cols; j++) {
  1553. img.atfloat(i, j) = ((i + j) & 1) -img.atfloat(i, j) img.atfloat(i, j);
  1554. }
  1555. }
  1556. }
  1557. void logMagFourier() {
  1558. char fname[10000];
  1559. while (openFileDlg(fname))
  1560. {
  1561. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1562.  
  1563. imshow(original img, src);
  1564.  
  1565. Mat srcf;
  1566. src.convertTo(srcf, CV_32FC1);
  1567.  
  1568. centering_transform(srcf);
  1569.  
  1570. Mat fourier;
  1571. dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
  1572.  
  1573. Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
  1574. split(fourier, channels);
  1575.  
  1576. Mat mag, phi;
  1577. magnitude(channels[0], channels[1], mag);
  1578. phase(channels[0], channels[1], phi);
  1579.  
  1580. Mat sho(mag.rows, mag.cols, CV_8UC1);
  1581.  
  1582. float maxlog = 0;
  1583. for (int i = 0; i mag.rows; i++) {
  1584. for (int j = 0; j mag.cols; j++) {
  1585. (log(mag.atfloat(i, j) + 1) maxlog maxlog = log(mag.atfloat(i, j) + 1) NULL);
  1586. }
  1587. }
  1588.  
  1589. for (int i = 0; i mag.rows; i++) {
  1590. for (int j = 0; j mag.cols; j++) {
  1591. sho.atuchar(i, j) = 255 (log(mag.atfloat(i, j) + 1) maxlog);
  1592. }
  1593. }
  1594.  
  1595. imshow(Result, sho);
  1596. waitKey(0);
  1597. }
  1598.  
  1599. }
  1600.  
  1601. void lowpass() {
  1602. char fname[10000];
  1603. while (openFileDlg(fname))
  1604. {
  1605. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1606.  
  1607. Mat srcf;
  1608. src.convertTo(srcf, CV_32FC1);
  1609.  
  1610. imshow(original, src);
  1611.  
  1612. centering_transform(srcf);
  1613.  
  1614. Mat fourier;
  1615. dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
  1616.  
  1617. Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
  1618. split(fourier, channels);
  1619.  
  1620. Mat mag, phi;
  1621. magnitude(channels[0], channels[1], mag);
  1622. phase(channels[0], channels[1], phi);
  1623.  
  1624. float R = 10;
  1625. for (int i = 0; i mag.rows; i++) {
  1626. for (int j = 0; j mag.cols; j++) {
  1627. channels[0].atfloat(i, j) = ((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j)) = RR channels[0].atfloat(i, j) 0);
  1628. channels[1].atfloat(i, j) = ((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j)) = RR channels[1].atfloat(i, j) 0);
  1629. }
  1630. }
  1631.  
  1632. Mat dstf, dst;
  1633. merge(channels, 2, fourier);
  1634. dft(fourier, dstf, DFT_INVERSE DFT_REAL_OUTPUT DFT_SCALE);
  1635.  
  1636. centering_transform(dstf);
  1637.  
  1638. normalize(dstf, dst, 0, 255, NORM_MINMAX, CV_8UC1);
  1639.  
  1640. imshow(Result, dst);
  1641. waitKey(0);
  1642. }
  1643. }
  1644.  
  1645. void highpass() {
  1646. char fname[10000];
  1647. while (openFileDlg(fname))
  1648. {
  1649. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1650.  
  1651. imshow(original, src);
  1652.  
  1653. Mat srcf;
  1654. src.convertTo(srcf, CV_32FC1);
  1655.  
  1656. centering_transform(srcf);
  1657.  
  1658. Mat fourier;
  1659. dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
  1660.  
  1661. Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
  1662. split(fourier, channels);
  1663.  
  1664. Mat mag, phi;
  1665. magnitude(channels[0], channels[1], mag);
  1666. phase(channels[0], channels[1], phi);
  1667. float R = 10;
  1668. for (int i = 0; i mag.rows; i++) {
  1669. for (int j = 0; j mag.cols; j++) {
  1670. channels[0].atfloat(i, j) = ((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j)) RR channels[0].atfloat(i, j) 0);
  1671. channels[1].atfloat(i, j) = ((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j)) RR channels[1].atfloat(i, j) 0);
  1672. }
  1673. }
  1674.  
  1675. Mat dstf, dst;
  1676. merge(channels, 2, fourier);
  1677. dft(fourier, dstf, DFT_INVERSE DFT_REAL_OUTPUT DFT_SCALE);
  1678.  
  1679. centering_transform(dstf);
  1680.  
  1681. normalize(dstf, dst, 0, 255, NORM_MINMAX, CV_8UC1);
  1682.  
  1683. imshow(Rez, dst);
  1684. waitKey(0);
  1685. }
  1686. }
  1687.  
  1688. void gaussianLowpass() {
  1689. char fname[10000];
  1690. while (openFileDlg(fname))
  1691. {
  1692. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1693. imshow(original, src);
  1694.  
  1695. Mat srcf;
  1696. src.convertTo(srcf, CV_32FC1);
  1697.  
  1698. centering_transform(srcf);
  1699.  
  1700. Mat fourier;
  1701. dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
  1702.  
  1703. Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
  1704. split(fourier, channels);
  1705.  
  1706. Mat mag, phi;
  1707. magnitude(channels[0], channels[1], mag);
  1708. phase(channels[0], channels[1], phi);
  1709. float R = 10, A = 10;
  1710. for (int i = 0; i mag.rows; i++) {
  1711. for (int j = 0; j mag.cols; j++) {
  1712. channels[0].atfloat(i, j) = channels[0].atfloat(i, j) (pow(2.7, -(((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j))) (AA))));
  1713. channels[1].atfloat(i, j) = channels[1].atfloat(i, j) (pow(2.7, -(((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j))) (AA))));
  1714. }
  1715. }
  1716.  
  1717. Mat dstf, dst;
  1718. merge(channels, 2, fourier);
  1719. dft(fourier, dstf, DFT_INVERSE DFT_REAL_OUTPUT DFT_SCALE);
  1720.  
  1721. centering_transform(dstf);
  1722.  
  1723. normalize(dstf, dst, 0, 255, NORM_MINMAX, CV_8UC1);
  1724.  
  1725. imshow(Result, dst);
  1726. waitKey(0);
  1727. }
  1728. }
  1729.  
  1730. void gaussianHighpass() {
  1731. char fname[10000];
  1732. while (openFileDlg(fname))
  1733. {
  1734. Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1735. imshow(original, src);
  1736.  
  1737. Mat srcf;
  1738. src.convertTo(srcf, CV_32FC1);
  1739.  
  1740. centering_transform(srcf);
  1741.  
  1742. Mat fourier;
  1743. dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
  1744.  
  1745. Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
  1746. split(fourier, channels);
  1747.  
  1748. Mat mag, phi;
  1749. magnitude(channels[0], channels[1], mag);
  1750. phase(channels[0], channels[1], phi);
  1751. float R = 10, A = 5;
  1752. for (int i = 0; i mag.rows; i++) {
  1753. for (int j = 0; j mag.cols; j++) {
  1754. channels[0].atfloat(i, j) = channels[0].atfloat(i, j) (1 - (pow(2.7, -(((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j))) (AA)))));
  1755. channels[1].atfloat(i, j) = channels[1].atfloat(i, j) (1 - (pow(2.7, -(((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j))) (AA)))));
  1756. }
  1757. }
  1758.  
  1759. Mat dstf, dst;
  1760. merge(channels, 2, fourier);
  1761. dft(fourier, dstf, DFT_INVERSE DFT_REAL_OUTPUT DFT_SCALE);
  1762.  
  1763. centering_transform(dstf);
  1764.  
  1765. normalize(dstf, dst, 0, 255, NORM_MINMAX, CV_8UC1);
  1766.  
  1767. imshow(Result, dst);
  1768. waitKey(0);
  1769. }
  1770. }
  1771. void median_filter(){
  1772. char fname[10000];
  1773. while (openFileDlg(fname))
  1774. {
  1775. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1776. imshow(original, img);
  1777. Mat copy = img.clone();
  1778. vectoruchar v;
  1779. int w;
  1780. stdcin w;
  1781. for (int i = w 2; i img.rows - w 2; i++)
  1782. {
  1783. for (int j = w 2; j img.cols - w 2; j++)
  1784. {
  1785. for (int k = -w 2; k = w 2; k++){
  1786. for (int l = -w 2; l = w 2; l++){
  1787. v.push_back(img.atuchar(i + k, j + l));
  1788. }
  1789. }
  1790. sort(v.begin(), v.end());
  1791. copy.atuchar(i, j) = v[v.size() 2 + 1];
  1792. v.clear();
  1793. }
  1794. }
  1795. imshow(result, copy);
  1796. }
  1797. }
  1798. void gaussian_filter_1x2d(){
  1799. char fname[10000];
  1800.  
  1801. while (openFileDlg(fname))
  1802. {
  1803. float filter[50][50];
  1804. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1805. imshow(original, img);
  1806. Mat copy = img.clone();
  1807. vectoruchar v;
  1808. int w;
  1809. stdcin w;
  1810. int x0 = w 2;
  1811. int y0 = w 2;
  1812. float sigma = w 6.0f;
  1813. float sumaelementelordinnucleuldeconvolutie = 0;
  1814. double t = (double)getTickCount();
  1815. for (int i = 0; i w; i++)
  1816. {
  1817. for (int j = 0; j w; j++)
  1818. {
  1819. filter[i][j] = (float)(1.0f 2 CV_PIsigmasigma)(float)exp(((i - x0)(i - x0) + (j - y0)(j - y0)) -(2 sigmasigma));
  1820. sumaelementelordinnucleuldeconvolutie += filter[i][j];
  1821. stdcout filter[i][j] ;
  1822. }
  1823. stdcout stdendl;
  1824. }
  1825. for (int i = w 2; i img.rows - w 2; i++)
  1826. {
  1827. for (int j = w 2; j img.cols - w 2; j++)
  1828. {
  1829. float sum = 0;
  1830. for (int k = -w 2; k = w 2; k++){
  1831. for (int l = -w 2; l = w 2; l++){
  1832. sum += img.atuchar(i + k, j + l)filter[w 2 + k][w 2 + l];
  1833. }
  1834. }
  1835. copy.atuchar(i, j) = (int)(sum sumaelementelordinnucleuldeconvolutie);
  1836. }
  1837. }
  1838. imshow(result, copy);
  1839. t = (((double)getTickCount() - t) getTickFrequency());
  1840. printf(time=%.3f [ms]n, t 1000);
  1841. }
  1842. }
  1843. void gaussian_filter_2x1d(){
  1844. char fname[10000];
  1845.  
  1846. while (openFileDlg(fname))
  1847. {
  1848. float filter[50][50];
  1849. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1850. imshow(original, img);
  1851. Mat copy = img.clone();
  1852. vectoruchar v;
  1853. int w;
  1854. stdcin w;
  1855. int x0 = w 2;
  1856. int y0 = w 2;
  1857. float sigma = w 6.0f;
  1858. float sumaelementelordinnucleuldeconvolutie1 = 0;
  1859. float sumaelementelordinnucleuldeconvolutie2 = 0;
  1860. vectorfloat Gx, Gy;
  1861. double t = (double)getTickCount();
  1862. for (int i = 0; i w; i++)
  1863. {
  1864. for (int j = 0; j w; j++)
  1865. {
  1866. filter[i][j] = (float)(1.0f 2 CV_PIsigmasigma)(float)exp(((i - x0)(i - x0) + (j - y0)(j - y0)) -(2 sigmasigma));
  1867. if (i == x0){
  1868. Gx.push_back(filter[i][j]);
  1869. sumaelementelordinnucleuldeconvolutie1 += filter[i][j];
  1870. }
  1871. if (j == y0){
  1872. Gy.push_back(filter[i][j]);
  1873. sumaelementelordinnucleuldeconvolutie2 += filter[i][j];
  1874. }
  1875.  
  1876. stdcout filter[i][j] ;
  1877. }
  1878. stdcout stdendl;
  1879. }
  1880. for (int i = w 2; i img.rows - w 2; i++)
  1881. {
  1882. for (int j = w 2; j img.cols - w 2; j++)
  1883. {
  1884. float sum = 0;
  1885. for (int k = -w 2; k = w 2; k++){
  1886. sum += img.atuchar(i + k, j)Gx[w 2 + k];
  1887. }
  1888. copy.atuchar(i, j) = (int)(sum sumaelementelordinnucleuldeconvolutie1);
  1889. }
  1890. }
  1891. img = copy.clone();
  1892. for (int i = w 2; i img.rows - w 2; i++)
  1893. {
  1894. for (int j = w 2; j img.cols - w 2; j++)
  1895. {
  1896. float sum = 0;
  1897. for (int k = -w 2; k = w 2; k++){
  1898. sum += img.atuchar(i, j + k)Gy[w 2 + k];
  1899. }
  1900. copy.atuchar(i, j) = (int)(sum sumaelementelordinnucleuldeconvolutie2);
  1901. }
  1902. }
  1903. imshow(result, copy);
  1904. t = (((double)getTickCount() - t) getTickFrequency());
  1905. printf(time=%.3f [ms]n, t 1000);
  1906. }
  1907. }
  1908. void canny(){
  1909. char fname[10000];
  1910. Mat m;
  1911. while (openFileDlg(fname))
  1912. {
  1913. Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  1914. int Sx[3][3] = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
  1915. int Sy[3][3] = { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };
  1916. imshow(original, img);
  1917. Mat copy = img.clone();
  1918. Mat copy2 = img.clone();
  1919. vectoruchar v;
  1920. int w = 3;
  1921. stdcin w;
  1922. for (int i = w 2; i img.rows - w 2; i++)
  1923. {
  1924. for (int j = w 2; j img.cols - w 2; j++)
  1925. {
  1926. int Gx = 0;
  1927. int Gy = 0;
  1928. for (int k = -w 2; k = w 2; k++){
  1929. for (int l = -w 2; l = w 2; l++){
  1930. Gx += img.atuchar(i + k, j + l)Sx[k + 1][l + 1];
  1931. Gy += img.atuchar(i + k, j + l)Sy[k + 1][l + 1];
  1932. }
  1933. }
  1934. float G = sqrt(GxGx + GyGy) (4 sqrt(2));
  1935. float fi = atan2(Gy, Gx);
  1936. float pi = CV_PI;
  1937.  
  1938. copy.atuchar(i, j) = (uchar)G;
  1939.  
  1940. v.clear();
  1941. }
  1942. }
  1943. copy2 = copy.clone();
  1944. for (int i = w 2; i img.rows - w 2; i++)
  1945. {
  1946. for (int j = w 2; j img.cols - w 2; j++)
  1947. {
  1948. int Gx = 0;
  1949. int Gy = 0;
  1950. for (int k = -w 2; k = w 2; k++){
  1951. for (int l = -w 2; l = w 2; l++){
  1952. Gx += img.atuchar(i + k, j + l)Sx[k + 1][l + 1];
  1953. Gy += img.atuchar(i + k, j + l)Sy[k + 1][l + 1];
  1954. }
  1955. }
  1956. float G = sqrt(GxGx + GyGy) (4 sqrt(2));
  1957. float fi = atan2(Gy, Gx);
  1958. float pi = CV_PI;
  1959.  
  1960. 0
  1961. if ((fipi 8 && fi-pi 8) (fi pi && fi7 pi 8) (fi -pi && fi-7 pi 8)){
  1962. if (copy.atuchar(i, j) = copy.atuchar(i, j + 1) && copy.atuchar(i, j) = copy.atuchar(i, j - 1))copy2.atuchar(i, j) = copy.atuchar(i, j);
  1963. else copy2.atuchar(i, j) = 0;
  1964.  
  1965. }
  1966. 1
  1967. if ((fi pi 8 && fi3 pi 8) (fi-7 pi 8 && fi-5 pi 8)){
  1968. if (copy.atuchar(i, j) = copy.atuchar(i - 1, j + 1) && copy.atuchar(i, j) = copy.atuchar(i + 1, j - 1))copy2.atuchar(i, j) = copy.atuchar(i, j);
  1969. else copy2.atuchar(i, j) = 0;
  1970.  
  1971. }
  1972. 2
  1973. if ((fi 3 pi 8 && fi 5 pi 8) (fi-3 pi 8 && fi-5 pi 8)){
  1974. if (copy.atuchar(i, j) = copy.atuchar(i - 1, j) && copy.atuchar(i, j) = copy.atuchar(i + 1, j))copy2.atuchar(i, j) = copy.atuchar(i, j);
  1975. else copy2.atuchar(i, j) = 0;
  1976.  
  1977. }
  1978. 3
  1979. if ((fi 5 pi 8 && fi 7 pi 8) (fi-pi 8 && fi-3 pi 8)){
  1980. if (copy.atuchar(i, j) = copy.atuchar(i + 1, j + 1) && copy.atuchar(i, j) = copy.atuchar(i - 1, j - 1))copy2.atuchar(i, j) = copy.atuchar(i, j);
  1981. else copy2.atuchar(i, j) = 0;
  1982.  
  1983. }
  1984. }
  1985. }
  1986. imshow(partial result, copy);
  1987. imshow(result, copy2);
  1988. }
  1989. }
  1990. void adaptive_thresh(){
  1991. Mat img=canny();
  1992. int histValues[256] = { 0 };
  1993. int nonZGradient = 0;
  1994. for (int i = 1; i img.rows-1; i++)
  1995. {
  1996. for (int j = 1; j img.cols-1; j++)
  1997. {
  1998. histValues[img.atuchar(i, j)]++;
  1999. if (img.atuchar(i, j) != 0)nonZGradient++;
  2000. }
  2001. }
  2002.  
  2003. float p = 0.1;
  2004. int noEdgePixels = pnonZGradient;
  2005. int Th=0;
  2006. stdcout noEdgePixels stdendl;;
  2007. for (int i = 255; i = 0; i--)
  2008. {
  2009. if (histValues[i] != 0){
  2010. noEdgePixels-=histValues[i];
  2011. }
  2012. if (noEdgePixels = 0){
  2013. Th = i;
  2014. i = 0;
  2015. }
  2016.  
  2017. }
  2018.  
  2019.  
  2020.  
  2021. int Tl = 0.4Th;
  2022. Th = 30;
  2023. Tl = 12;
  2024. stdcout Th Tl;
  2025. for (int i = 1; i img.rows - 1; i++)
  2026. {
  2027. for (int j = 1; j img.cols - 1; j++)
  2028. {
  2029. if (img.atuchar(i, j) = Tl){ img.atuchar(i, j) = 0; }
  2030. else{
  2031. if (img.atuchar(i, j) = Tl && img.atuchar(i, j) = Th){ img.atuchar(i, j) = 127; }
  2032. else{
  2033. if (img.atuchar(i, j) = Th){ img.atuchar(i, j) = 255; }
  2034. }
  2035. }
  2036. }
  2037. }
  2038. imshow(thr, img);
  2039. Mat final = img.clone();
  2040. int stop = 0;
  2041. while (!stop){
  2042. stop = 1;
  2043. for (int i = 1; i img.rows - 1; i++)
  2044. {
  2045. for (int j = 1; j img.cols - 1; j++)
  2046. {
  2047. if (img.atuchar(i, j) == 255){
  2048. for (int k = -1; k =1; k++){
  2049. for (int l = -1; l = 1; l++){
  2050. if (img.atuchar(i + k, j + l) == 127){
  2051. img.atuchar(i + k, j + l) = 255;
  2052.  
  2053. stop = 0;
  2054. }
  2055. }
  2056. }
  2057. }
  2058. }
  2059. }
  2060. }
  2061. for (int i = 1; i img.rows - 1; i++)
  2062. {
  2063. for (int j = 1; j img.cols - 1; j++)
  2064. {
  2065. if (img.atuchar(i, j) == 127)img.atuchar(i, j) = 0;
  2066. }
  2067. }
  2068. imshow(final, img);
  2069. waitKey(0);
  2070. }
  2071. int main()
  2072. {
  2073.  
  2074. int op;
  2075. do
  2076. {
  2077. system(cls);
  2078. destroyAllWindows();
  2079. printf(Menun);
  2080. printf(L1- 1 - Open imagen);
  2081. printf(L1- 2 - Open BMP images from foldern);
  2082. printf(L1- 3 - Resize imagen);
  2083. printf(L1- 4 - Process videon);
  2084. printf(L1- 5 - Snap frame from live videon);
  2085. printf(L1- 6 - Mouse callback demon);
  2086. printf(L1- 7 - Negative imagen);
  2087. printf(L1- 8 - Additive greyn);
  2088. printf(L1- 9 - Color matrixn);
  2089. printf(L1- 10 - Compute inversen);
  2090. printf(L2 - 11 - RGB24-split_channelsn);
  2091. printf(L2 - 12 - Color to grayscalen);
  2092. printf(L2 - 13 - grayscale to black&whiten);
  2093. printf(L2 - 14 - RGB to HSVn);
  2094. printf(L3 - 15 - Histogramn);
  2095. printf(L3 - 16 - Multi level thresholdingn);
  2096. printf(L3 - 17 - Floyd-Steinberg algorithmn);
  2097. printf(L4 - 18 - Geometrical featuresn);
  2098. printf(L5 - 19 - Breadth first traversaln);
  2099. printf(L5 - 20 - Two pass traversaln);
  2100. printf(L6 - 21 - Border tracing algorithmn);
  2101. printf(L7 - 22 - Dilationn);
  2102. printf(L7 - 23 - Erosionn);
  2103. printf(L7 - 24 - Openingn);
  2104. printf(L7 - 25 - Closingn);
  2105. printf(L7 - 26 - Boundary extrn);
  2106. printf(L7 - 27 - Region fillingn);
  2107. printf(L8 - 28 - Mean and std devn);
  2108. printf(L8 - 29 - Basic global threshold algon);
  2109. printf(L8 - 30 - Brightness changen);
  2110. printf(L8 - 31 - Contrast changen);
  2111. printf(L8 - 32 - Gamma correctionn);
  2112. printf(L8 - 33 - Histogram equalisationn);
  2113. printf(L9 - 34 - Mean filter(3x3)n);
  2114. printf(L9 - 35 - Mean filter(5x5)n);
  2115. printf(L9 - 36 - Gaussian filter(3x3)n);
  2116. printf(L9 - 37 - Laplace filter(3x3)n);
  2117. printf(L9 - 38 - High pass filter(3x3)n);
  2118. printf(L9 - 39 - Log magn Fouriern);
  2119. printf(L9 - 40 - Ideal low pass filtern);
  2120. printf(L9 - 41 - Ideal high pass filtern);
  2121. printf(L9 - 42 - Gaussian cut LPFn);
  2122. printf(L9 - 43 - Gaussian cut HPFn);
  2123. printf(L10 - 44 - Median filtern);
  2124. printf(L10 - 45 - Gaussian filter 1x2Dn);
  2125. printf(L10 - 46 - Gaussian filter 2x1Dn);
  2126. printf(L11 - 47 - Canny edge gaussian filteringn);
  2127. printf(L12 - 48 - Adaptive thresholdingn);
  2128.  
  2129. printf( 0 - Exitnn);
  2130. printf(Option );
  2131. scanf(%d, &op);
  2132. switch (op)
  2133. {
  2134. case 1
  2135. testOpenImage();
  2136. break;
  2137. case 2
  2138. testOpenImagesFld();
  2139. break;
  2140. case 3
  2141. testResize();
  2142. break;
  2143. case 4
  2144. testVideoSequence();
  2145. break;
  2146. case 5
  2147. testSnap();
  2148. break;
  2149. case 6
  2150. testMouseClick();
  2151. break;
  2152. case 7
  2153. negative_image();
  2154. break;
  2155. case 8
  2156. additive_grey_change();
  2157. break;
  2158. case 9
  2159. color_matrix();
  2160. break;
  2161. case 10
  2162. compute_inverse();
  2163. break;
  2164. case 11
  2165. split_channels();
  2166. break;
  2167. case 12
  2168. color_to_grayscale();
  2169. break;
  2170. case 13
  2171. grayscale_to_bw();
  2172. break;
  2173. case 14
  2174. rgb_to_hsv();
  2175. break;
  2176. case 15
  2177. histogram();
  2178. break;
  2179. case 16
  2180. ml_thresholding();
  2181. break;
  2182. case 17
  2183. floyd_steinberg();
  2184. break;
  2185. case 18
  2186. geom_feat();
  2187. break;
  2188. case 19
  2189. BFS_traversal();
  2190. break;
  2191. case 20
  2192. two_pass_traversal();
  2193. break;
  2194. case 21
  2195. border_tracing();
  2196. break;
  2197. case 22
  2198. dilation();
  2199. break;
  2200. case 23
  2201. erosion();
  2202. break;
  2203. case 24
  2204. opening();
  2205. break;
  2206. case 25
  2207. closing();
  2208. break;
  2209. case 26
  2210. boundary();
  2211. break;
  2212. case 27
  2213. filling();
  2214. break;
  2215. case 28
  2216. mean_and_std_dev();
  2217. break;
  2218. case 29
  2219. basic_global_thresh();
  2220. break;
  2221. case 30
  2222. bright_change();
  2223. break;
  2224. case 31
  2225. contrast_change();
  2226. break;
  2227. case 32
  2228. gamma_change();
  2229. break;
  2230. case 33
  2231. histo_eq();
  2232. break;
  2233. case 34
  2234. mean_filter_3x3();
  2235. break;
  2236. case 35
  2237. mean_filter_5x5();
  2238. break;
  2239. case 36
  2240. gaussian_filter_3x3();
  2241. break;
  2242. case 37
  2243. laplace_filter_3x3();
  2244. break;
  2245. case 38
  2246. high_pass_filter_3x3();
  2247. break;
  2248. case 39
  2249. logMagFourier();
  2250. break;
  2251. case 40
  2252. lowpass();
  2253. break;
  2254. case 41
  2255. highpass();
  2256. break;
  2257. case 42
  2258. gaussianLowpass();
  2259. break;
  2260. case 43
  2261. gaussianHighpass();
  2262. break;
  2263. case 44
  2264. median_filter();
  2265. break;
  2266. case 45
  2267. gaussian_filter_1x2d();
  2268. break;
  2269. case 46
  2270. gaussian_filter_2x1d();
  2271. break;
  2272. case 47
  2273. canny();
  2274. break;
  2275. case 48
  2276. adaptive_thresh();
  2277. break;
  2278. }
  2279. } while (op != 0);
  2280. return 0;
  2281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement