Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.45 KB | None | 0 0
  1. // OpenCVTest.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #define _CRT_SECURE_NO_DEPRECATE
  5. #define _USE_MATH_DEFINES
  6.  
  7.  
  8. #include "stdafx.h"
  9. #include <iostream>
  10. #include <opencv2/core.hpp>
  11. #include <stdio.h>
  12. //#include "cxcore.hpp"
  13. #include <opencv\cxcore.h>
  14. #include <opencv2/imgcodecs.hpp>
  15. #include <conio.h>
  16. //#include "imgproc.hpp"
  17. #include <opencv2/imgproc.hpp>
  18. #include <opencv2/opencv.hpp>
  19. #include <opencv2/highgui.hpp>
  20. //#include "highgui.hpp"
  21. #include <stdio.h>
  22.  
  23. #include <fstream>
  24. #include <math.h>
  25.  
  26.  
  27.  
  28. /// Shortcuts for std and opencv
  29. using namespace std;
  30. using namespace cv;
  31.  
  32. /// Global variables
  33. #define SegmentSizeX 58//Size of segments in x
  34. #define SegmentSizeY 47//Size of segments in y
  35.  
  36. float OfficialPercentile; //Percentile size
  37. int i;
  38. int j;
  39.  
  40. ///* Histogram creation *///
  41.  
  42. void myhist(Mat &med, Mat &decoy) {
  43.     decoy.setTo(0);
  44.     float maxi = 0;
  45.  
  46.     float *y = decoy.ptr<float>(0);
  47.     for (int i = 0; i < med.rows; i++)
  48.     {
  49.         uchar *x = med.ptr<uchar>(i);
  50.         for (int j = 0; j < med.cols; j++)
  51.         {
  52.             for (int k = 0; k < 256; k++)
  53.             {
  54.                 if (k == (int)x[j])
  55.                 {
  56.                     y[k] += 1;
  57.                     if (maxi < y[k])
  58.                     {
  59.                         maxi = y[k];
  60.                     }
  61.                     break;
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     y[256] = maxi;
  67. }
  68.  
  69.  
  70. ///* Function that draws the histogram *///
  71. void drawHist(Mat &decoy, char *winName)
  72. {
  73.     Mat histFig(640, 512, CV_8U);
  74.     histFig.setTo(256);
  75.     namedWindow(winName);
  76.  
  77.     float *y = decoy.ptr<float>(0);
  78.     float max = y[256];
  79.     float factor = ((float)640 / max);
  80.  
  81.     for (int i = 0; i < 256; i++)
  82.     {
  83.         y[i] *= factor;
  84.         Point pt1, pt2;
  85.         pt1 = Point(2 * i, 640);
  86.         pt2 = Point(2 * i, 640 - (int)y[i]);
  87.         const Scalar col = Scalar(0, 0, 0);
  88.         line(histFig, pt1, pt2, col, 2, CV_AA);
  89.  
  90.     }
  91.     imshow(winName, histFig);
  92. }
  93.  
  94.  
  95. ///* Function that writes matrices to a file *///
  96. void writeMatToFile(Mat &m, const char* filename)
  97. {
  98.     ofstream fout(filename);
  99.  
  100.     if (!fout)
  101.     {
  102.         cout << "File Not Opened" << endl;  
  103.         return;
  104.     }
  105.  
  106.     for (int i = 0; i<m.rows; i++)
  107.     {
  108.         for (int j = 0; j<m.cols; j++)
  109.         {
  110.             fout << m.at<float>(i, j) << "\t";
  111.         }
  112.         fout << endl;
  113.     }
  114.  
  115.     fout.close();
  116. }
  117.  
  118. void segmentDivisions(Mat &decoyM, Mat *SegmentArray) {
  119.     int i;
  120.     int j;
  121.     int k = 0;
  122.  
  123.     //printf("cols: %d \n",decoyM.cols);
  124.     //printf("rows: %d \n", decoyM.rows);
  125.     for (i = 0; i < decoyM.rows; i += SegmentSizeX) {
  126.         for (j = 0; j < decoyM.cols; j += SegmentSizeY) {
  127.             //printf("Test inside void i:%d j:%d k:%d ", i, j, k);
  128.             Rect roiArr(j, i, SegmentSizeY, SegmentSizeX);
  129.             Mat holdingRect = decoyM(roiArr);
  130.             k = floor((decoyM.cols / SegmentSizeY)*(i / SegmentSizeX) + j / SegmentSizeY);
  131.             SegmentArray[k] = holdingRect.clone();
  132.  
  133.         }
  134.     }
  135.  
  136. }
  137.  
  138. void StarMatrix(Mat &decoyM, Mat *SegmentStar) {
  139.     int i;
  140.     int j;
  141.     int k = 0;
  142.  
  143.     //printf("cols: %d \n",decoyM.cols);
  144.     //printf("rows: %d \n", decoyM.rows);
  145.     for (i = 0; i < decoyM.rows; i += SegmentSizeX) {
  146.         for (j = 0; j < decoyM.cols; j += SegmentSizeY) {
  147.             //printf("Test inside void i:%d j:%d k:%d ", i, j, k);
  148.             Rect roiArr(j, i, SegmentSizeY, SegmentSizeX);
  149.             Mat holdingRect = decoyM(roiArr);
  150.             k = floor((decoyM.cols / SegmentSizeY)*(i / SegmentSizeX) + j / SegmentSizeY);
  151.             SegmentStar[k] = holdingRect.clone();
  152.         }
  153.     }
  154. }
  155.  
  156.  
  157. void PercentileCalc(Mat &decoy) {
  158.     int total = 0;
  159.     int total2 = 0;
  160.     int total3 = 0;
  161.     float percentile;
  162.     int number;
  163.  
  164.     /// Acumulates the intensity (values) of the picture
  165.     for (i = 0; i < 256; i++)
  166.     {
  167.         total += decoy.at<float>(i);
  168.     }
  169.  
  170.     /// Defining the 40% percentile
  171.     float percenti = total * 0.4; // 0.4 = 40%
  172.                                   //printf("The 40% of the accumuated value is \n %f", percenti);
  173.  
  174.                                   /// Need this function as we using i-1, to avoid negative values
  175.     for (i = 0; i < 1; i++)
  176.     {
  177.         total2 += decoy.at<float>(i);
  178.     }
  179.  
  180.     for (i = 1; i < 256; i++)
  181.     {
  182.         /// Checks for what itensity and which color-value is bounded by the accumulated values
  183.         /// of the percentile
  184.         if (percenti > total2)
  185.         {
  186.             /// Acumulates the value as long as its not more than the percenti
  187.             /// This value will eventually become larger than the percenti (upperbounded)
  188.             total2 += decoy.at<float>(i);
  189.  
  190.             /// Accumulates the value, one position less than the previous one
  191.             /// This function will not surpass the percenti value (lowerbounded)
  192.             total3 += decoy.at<float>(i - 1);
  193.  
  194.             /// This value will be the color-value for which is correspond to the lower-bounded value
  195.             number = i;
  196.         }
  197.     }
  198.  
  199.     /// Function to calulate the value between the lower and upper-bounded values
  200.     percentile = (percenti - total3) / (total2 - total3);
  201.     //printf("\n Percenti = %f \n Total 2 = %d \n Total 3 = %d \n", percenti, total2, total3);
  202.  
  203.     /// This function sums the value found to the lower color-number, thus this will yield the
  204.     /// exact color value for which the percentile value exist
  205.     OfficialPercentile = number + percentile;
  206.     printf("The percentile is found for colorvalue %f", number + percentile);
  207. }
  208.  
  209. void RemoveBackground(Mat &SegM, float pres) {
  210.     for (int i = 0; i < SegmentSizeX; i++) {
  211.         for (int j = 0; j < SegmentSizeY; j++) {
  212.             if (SegM.at<float>(i, j) <= pres) {
  213.                 //SegM->at<float>(i, j) = 0;
  214.             }
  215.         }
  216.     }
  217.     //printf("Value of point is %d \n", data[i]);
  218.     //printf("Value of point is %d \n", data[j]);
  219.  
  220.     /*
  221.     for (i = 0; i < width_final; i++) {
  222.     for (j = 0; j < height_final; j++) {
  223.  
  224.     if (data[i] < 15 ) {
  225.     data[i] = 0;
  226.     }
  227.     if (data[j] < 15) {
  228.     data[j] = 0;
  229.     }
  230.     //printf("Value of point is %d \n", data[i]);
  231.     //printf("Value of point is %d \n", data[j]);
  232.     }
  233.     }
  234.     */
  235. }
  236.  
  237. int main(int argc, char* argv[])
  238. {
  239.     //Mat(MAX_EGDE, MAX_EGDE, CV_64F);
  240.  
  241.     /// Assigning size of the Matrix-Arrays
  242.     Mat SegmentArray[(752 / SegmentSizeY)*(580 / SegmentSizeX)];
  243.     Mat StarMatrixArray[144];
  244.  
  245.  
  246.  
  247.     /// Uploading binary file and making a matrix out of it
  248.     FILE* f = fopen("C:\\Users\\Runar\\Desktop\\DTU Work\\Image_Ana\\Prosject\\Images\\ICON C1 RS2\\A150722_12132639.unc", "rb");
  249.     unsigned char info[34];
  250.     fread(info, sizeof(unsigned char), 34, f);
  251.  
  252.     /// Extract image height and width from header
  253.     int width = *(int*)&info[28];
  254.     int height = *(int*)&info[30];
  255.  
  256.     /// Defining the length of the picture
  257.     unsigned int width_h = info[31];
  258.     unsigned int width_l = info[30];
  259.     unsigned int width_final = (width_h << 8) + width_l;
  260.     //printf("width %d \n", width_final);
  261.  
  262.     unsigned int height_h = info[29];
  263.     unsigned int height_t = info[28];
  264.     unsigned int height_final = 2 * ((height_h << 8) + height_t); //Multiplied with 2 (see Image format description)
  265.                                                                   //printf("height %d \n", height_final);
  266.  
  267.                                                                   //To see values in the header file, uncomment this section
  268.                                                                   /*
  269.                                                                   for (i = 0; i < 34; i++) {
  270.                                                                   printf("%d \n", info[i]);
  271.                                                                   }
  272.                                                                   printf("2 \n");
  273.                                                                   */
  274.  
  275.                                                                   /// Writing of the binary data to a matrix continues
  276.     int size = 1 * width_final * height_final;
  277.     unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
  278.     fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
  279.     fclose(f);
  280.  
  281.     Mat image = Mat(width_final*height_final, 1, CV_8U, (unsigned*)data);
  282.     Mat reshaped = image.reshape(0, height_final);
  283.     cvNamedWindow("image", WINDOW_AUTOSIZE);
  284.     imshow("image", reshaped);
  285.  
  286.     /// Snappshot of a certain area of the picture
  287.     Mat outImg;
  288.     Rect roi(730, 295, 8, 15);
  289.     Mat Rectangle_outImg = reshaped(roi);
  290.  
  291.     resize(Rectangle_outImg, outImg, cv::Size(reshaped.cols*1.5, reshaped.rows*1.5), 0, 0, CV_INTER_LINEAR);
  292.     cvNamedWindow("resized window", CV_WINDOW_AUTOSIZE);
  293.     imshow("resized window", outImg);
  294.  
  295.     /// To check the amount of pixels in the new image, a picture is saved in the workspace
  296.     /// and then by checking properties of the picture in the folder, pixel number can be validated
  297.  
  298.     //imwrite("C:\\Users\\Runar\\Desktop\\DTU Work\\Image_Ana\\Prosject\\Images\\ICON C1 RS2\\EdgdeOfStar",outImg);
  299.  
  300.  
  301.  
  302.  
  303.     /* Functions should start here */
  304.  
  305.     /// Calculates the segmentsize, saving it as int (whole number)
  306.     int SegmentSize = (width_final / SegmentSizeY)*(height_final / SegmentSizeX);
  307.  
  308.     ///* Step 2: Calculations of Segments *///
  309.    
  310.     /// MERGINGSEGEMENTS:
  311.     // There is several way to do this, but the best way I found is to use cv::hconcat(mat1, mat2, dst)
  312.     // for horizontal merge orcv::vconcat(mat1, mat2, dst) for vertical.
  313.  
  314.     //printf("SegmentArray: %d", (750 / SegmentSize)*(580 / SegmentSize));
  315.    
  316.     segmentDivisions(reshaped, SegmentArray);
  317.     //Reading out to a file
  318.     //Mat TestMatr = Mat(100, 100, CV_32F);
  319.     cout << SegmentArray[115] << endl;
  320.    
  321.    
  322.     ///* Step 2 Calculating the brigthest star within each segments*///
  323.     /*
  324.     ///Function that defines the max, min and its position
  325.     double Min, Max, MinSeg, MaxSeg;
  326.     Point p_min, p_max;
  327.  
  328.     //Save min and max as an array?
  329.    
  330.     Mat StarOutMat;
  331.     Mat mergedMatrix;
  332.  
  333.     int threshold = 35;
  334.  
  335.     for (i = 8; i < 9; i++) {
  336.        
  337.         ///Finding the max, min value and the position
  338.         //cout << SegmentArray[i] << endl;
  339.         minMaxLoc(SegmentArray[i], &Min, &Max, &p_min, &p_max);
  340.         printf("For segment %d : ", i);
  341.         cout << "min: " << Min << " at " << p_min << "max: " << Max << " at " << p_max << endl;
  342.        
  343.         int MaxValue = Max;
  344.  
  345.         if (MaxValue > threshold) {
  346.            
  347.             ///Setting region of interest
  348.             int xUpperBorder = p_max.x + 6;
  349.             int xLowerBorder = p_max.x - 6;
  350.             int yUpperBorder = p_max.y + 6;
  351.             int yLowerBorder = p_max.y - 6;
  352.  
  353.             /// Have to divide per 16 (y) segment 9 (x)
  354.  
  355.             /// If the star is at an end, we have to merge two matrices
  356.            
  357.            
  358.             if ((xUpperBorder > SegmentSizeY && yUpperBorder > SegmentSizeX) || (xLowerBorder < 0 && yLowerBorder < 0)) {
  359.                
  360.                 /// If a star is in a corner, 4 matrices (or new matrix) has to be made
  361.                 cout << "This star is out of reach" << endl;
  362.             }
  363.            
  364.             if (xUpperBorder > SegmentSizeY) {
  365.  
  366.                 if (i % 47 == 0) {
  367.                     cout << "The region of interest is outside of the picture" << endl;
  368.                 }
  369.  
  370.                 else {
  371.  
  372.                     hconcat(SegmentArray[i], SegmentArray[i + 1], mergedMatrix);
  373.                     cout << "Fails for x1 coordinate" << endl;
  374.  
  375.                     /// Resizing and interpolating the segment and defining start windowd
  376.                     resize(mergedMatrix, StarOutMat, cv::Size(), 2, 2, INTER_LINEAR);
  377.  
  378.                     /// Calculates the same procedure for new matrix
  379.                     minMaxLoc(StarOutMat, &Min, &Max, &p_min, &p_max);
  380.                     cout << "min: " << Min << " at " << p_min << "max: " << Max << " at " << p_max << endl;
  381.  
  382.                     int New_yCoord = p_max.y - 6;
  383.                     int New_xCoord = p_max.x - 6;
  384.                     printf(" \n new ycoord %d and new xcoord %d \n", New_yCoord, New_xCoord);
  385.  
  386.                     //cout << StarOutMat << endl;
  387.  
  388.                     /// Making a matrix around the star
  389.                     Rect roiStar(New_xCoord, New_yCoord, 12, 12);
  390.                     Mat HoldingMatrix = StarOutMat(roiStar);
  391.                     Mat StarMatrix = HoldingMatrix.clone(); //Clones it in a matrix so original variables dont change
  392.                                                             //k = floor((decoyM.cols / SegmentSizeY)*(i / SegmentSizeX) + j / SegmentSizeY);
  393.                                                             //SegmentStar[k] = holdingRect.clone();
  394.                     cout << StarMatrix << endl;
  395.                     StarMatrixArray[i] = StarMatrix;
  396.  
  397.                 }
  398.             }
  399.  
  400.             if (xLowerBorder < 0) {
  401.  
  402.                 if (i % 46 == 0) {
  403.                     cout << "The region of interest is outside of the picture" << endl;
  404.                 }
  405.                
  406.                 else {
  407.  
  408.                     hconcat(SegmentArray[i - 1], SegmentArray[i], mergedMatrix);
  409.                     cout << "Fails for x_2 coordinate" << endl;
  410.  
  411.                     /// Resizing and interpolating the segment and defining start windowd
  412.                     resize(mergedMatrix, StarOutMat, cv::Size(), 2, 2, INTER_LINEAR);
  413.  
  414.                     /// Calculates the same procedure for new matrix
  415.                     minMaxLoc(StarOutMat, &Min, &Max, &p_min, &p_max);
  416.                     cout << "min: " << Min << " at " << p_min << "max: " << Max << " at " << p_max << endl;
  417.  
  418.                     int New_yCoord = p_max.y - 6;
  419.                     int New_xCoord = p_max.x - 6;
  420.                     printf(" \n new ycoord %d and new xcoord %d \n", New_yCoord, New_xCoord);
  421.  
  422.                     //cout << StarOutMat << endl;
  423.  
  424.                     /// Making a matrix around the star
  425.                     Rect roiStar(New_xCoord, New_yCoord, 12, 12);
  426.                     Mat HoldingMatrix = StarOutMat(roiStar);
  427.                     Mat StarMatrix = HoldingMatrix.clone(); //Clones it in a matrix so original variables dont change
  428.                                                             //k = floor((decoyM.cols / SegmentSizeY)*(i / SegmentSizeX) + j / SegmentSizeY);
  429.                                                             //SegmentStar[k] = holdingRect.clone();
  430.                     cout << StarMatrix << endl;
  431.                     StarMatrixArray[i] = StarMatrix;
  432.  
  433.                     printf("xLowerBorder is now %d", xLowerBorder);
  434.                 }
  435.  
  436.  
  437.             }
  438.  
  439.             if (yUpperBorder > SegmentSizeX) {
  440.  
  441.                 if (i < 16) {
  442.                     cout << "The region of interest is outside of the picture" << endl;
  443.                 }
  444.  
  445.                 else {
  446.  
  447.                     vconcat(SegmentArray[i], SegmentArray[i-16], mergedMatrix);
  448.                     cout << "Fails for yUpperBorder coordinate" << endl;
  449.  
  450.                     /// Resizing and interpolating the segment and defining start windowd
  451.                     resize(mergedMatrix, StarOutMat, cv::Size(), 2, 2, INTER_LINEAR);
  452.  
  453.                     /// Calculates the same procedure for new matrix
  454.                     minMaxLoc(StarOutMat, &Min, &Max, &p_min, &p_max);
  455.                     cout << "min: " << Min << " at " << p_min << "max: " << Max << " at " << p_max << endl;
  456.  
  457.                     int New_yCoord = p_max.y - 6;
  458.                     int New_xCoord = p_max.x - 6;
  459.                     printf(" \n new ycoord %d and new xcoord %d \n", New_yCoord, New_xCoord);
  460.  
  461.                     //cout << StarOutMat << endl;
  462.  
  463.                     /// Making a matrix around the star
  464.                     Rect roiStar(New_xCoord, New_yCoord, 12, 12);
  465.                     Mat HoldingMatrix = StarOutMat(roiStar);
  466.                     Mat StarMatrix = HoldingMatrix.clone(); //Clones it in a matrix so original variables dont change
  467.                                                             //k = floor((decoyM.cols / SegmentSizeY)*(i / SegmentSizeX) + j / SegmentSizeY);
  468.                                                             //SegmentStar[k] = holdingRect.clone();
  469.                     cout << StarMatrix << endl;
  470.                     StarMatrixArray[i] = StarMatrix;
  471.  
  472.                     printf("Upperborder is now %d", xLowerBorder);
  473.  
  474.                 }
  475.             }
  476.  
  477.             if (yLowerBorder < 0) {
  478.  
  479.                 if (i > 144 ){
  480.                     cout << "The region of interest is outside of the picture" << endl;
  481.                 }
  482.  
  483.                 else {
  484.  
  485.                     vconcat(SegmentArray[i], SegmentArray[i + 16], mergedMatrix);
  486.                     cout << "Fails for yLowerBorder coordinate" << endl;
  487.  
  488.                     /// Resizing and interpolating the segment and defining start windowd
  489.                     resize(mergedMatrix, StarOutMat, cv::Size(), 2, 2, INTER_LINEAR);
  490.  
  491.                     /// Calculates the same procedure for new matrix
  492.                     minMaxLoc(StarOutMat, &Min, &Max, &p_min, &p_max);
  493.                     cout << "min: " << Min << " at " << p_min << "max: " << Max << " at " << p_max << endl;
  494.  
  495.                     int New_yCoord = p_max.y - 6;
  496.                     int New_xCoord = p_max.x - 6;
  497.                     printf(" \n new ycoord %d and new xcoord %d \n", New_yCoord, New_xCoord);
  498.  
  499.                     //cout << StarOutMat << endl;
  500.  
  501.                     /// Making a matrix around the star
  502.                     Rect roiStar(New_xCoord, New_yCoord, 12, 12);
  503.                     Mat HoldingMatrix = StarOutMat(roiStar);
  504.                     Mat StarMatrix = HoldingMatrix.clone(); //Clones it in a matrix so original variables dont change
  505.                                                             //k = floor((decoyM.cols / SegmentSizeY)*(i / SegmentSizeX) + j / SegmentSizeY);
  506.                                                             //SegmentStar[k] = holdingRect.clone();
  507.                     cout << StarMatrix << endl;
  508.                     StarMatrixArray[i] = StarMatrix;
  509.  
  510.                     printf("yUpperBorder is now %d", xLowerBorder);
  511.  
  512.                 }
  513.             }
  514.  
  515.             else {
  516.                 cout << "TEST TEST TEST \n\n" << endl;
  517.  
  518.                 /// Resizing and interpolating the segment and defining start windowd
  519.                 resize(SegmentArray[i], StarOutMat, cv::Size(), 2, 2, INTER_LINEAR);
  520.                 //cout << StarOutMat << endl;
  521.  
  522.                 int yCoord = p_max.y - 6;
  523.                 int xCoord = p_max.x - 6;
  524.  
  525.                 /// Making a matrix around the star
  526.                 Rect roiStar(xCoord, yCoord, 12, 12);
  527.                 Mat HoldingMatrix = StarOutMat(roiStar);
  528.                 Mat StarMatrix = HoldingMatrix.clone(); //Clones it in a matrix so original variables dont change
  529.                 //k = floor((decoyM.cols / SegmentSizeY)*(i / SegmentSizeX) + j / SegmentSizeY);
  530.                 //SegmentStar[k] = holdingRect.clone();
  531.                 StarMatrixArray[i] = StarMatrix;
  532.  
  533.                 //printf("This segment works %d", i);
  534.                 //cout << StarMatrixArray[i] << endl;
  535.             }
  536.         }
  537.     }
  538.     */
  539.    
  540.  
  541.    
  542.    
  543.     /*
  544.     resize(SegmentArray[41], StarOutMat, cv::Size(), 1, 1, INTER_CUBIC);
  545.     minMaxLoc(StarOutMat, &MinSeg, &MaxSeg, &p_min, &p_max);
  546.     int yCoord = p_max.y - 6;
  547.     int xCoord = p_max.x - 6;
  548.  
  549.     printf("yCoord is %d and xCoord is %d ", yCoord, xCoord);
  550.     printf("\n\n");
  551.  
  552.     Rect roiStar(xCoord, yCoord, 12, 12);
  553.     Mat HoldingMatrix = StarOutMat(roiStar);
  554.     Mat StarMatrix = HoldingMatrix.clone(); //Clones it in a matrix so original variables dont change
  555.     cout << StarMatrix << endl;
  556.     */
  557.    
  558.  
  559.    
  560.    
  561.  
  562.  
  563.     //myhist(StarMatrix, decoy);
  564.     //drawHist(decoy, "Histogram without interpolation");
  565.     //const char* filename = "StarOut.txt";
  566.     //writeMatToFile(StarMatrix, filename);
  567.     //Rect roiStar2(xCoord, yCoord, 12, 12);
  568.     //Mat StarMatrix2 = StarOutMat(roiStar);
  569.  
  570.     // Get the values
  571.     //printf("\ntestPoint is : %d\n", testPoint);
  572.     //printf("MaxSeg is this number: %f", p_max); // 7 - 47
  573.  
  574.  
  575.     int k = 0;
  576.    
  577.  
  578.  
  579.     //const char* filename = "Output.txt";
  580.     //writeMatToFile(SegmentArray[4], filename);
  581.    
  582.  
  583.    
  584.  
  585.  
  586.     ///* Step 3: Calculation of Histogram *///
  587.  
  588.     /*if (argc < 2)
  589.     {
  590.     //std::cout << "less arguments" << std::end1;
  591.     printf("Fails here");
  592.     return -1;
  593.     }*/
  594.  
  595.     //Reading out to a file
  596.     //const char* filename = "output3.txt";
  597.     //writeMatToFile(SegmentArray[], filename);
  598.  
  599.     /// Define the limit of the histogram, calculates and draws it
  600.     Mat decoy(1, 257, CV_32F);
  601.     //for (i = 0; i < 10; i++) {
  602.  
  603.     printf("\n");
  604.     myhist(SegmentArray[10], decoy);
  605.     drawHist(decoy, "Histogram of section");
  606.    
  607.  
  608.  
  609.  
  610.     //imwrite("C:\\Users\\Runar\\Desktop\\DTU Work\\Image_Ana\\Prosject\\Images\\WorkInPorgress\\Histogram.png", decoy);
  611.     //}
  612.  
  613.     ///* Step 4: Calculation of Percentile *///
  614.     PercentileCalc(decoy);
  615.     printf("Percentile %f", OfficialPercentile);
  616.     //count <
  617.  
  618.     /// Step 5: Calculating the removal of background *///
  619.    
  620.     Mat Test101 = SegmentArray[22];
  621.  
  622.     RemoveBackground(Test101, OfficialPercentile); // SegM = Seg(k) and pres = pre
  623.     cout << Test101 << endl;
  624.  
  625.     //const char* filename = "Output.txt";
  626.     //writeMatToFile(Test101, filename);
  627.  
  628.    
  629.  
  630.    
  631.  
  632.  
  633.     /*Mat holdingRect = decoyM(roiArr);
  634.     k = floor((decoyM.cols / SegmentSizeY)*(i / SegmentSizeX) + j / SegmentSizeY);
  635.     SegmentArray[k] = holdingRect.clone();
  636.     */
  637.     ///* Calculation of the cart-2-polar *///
  638.  
  639.     /// Average of the first picture
  640.     /// These variables should be changed to the average / or updated per picture
  641.     int averageX = 291; //midle area for j variable
  642.     int averageY = 367; //midle area for i variable
  643.  
  644.                         /// Variables used to for cart-2-polar transformation
  645.     double magnitude;
  646.     double angle;
  647.     double distanceX;
  648.     double distanceY;
  649.  
  650.     /// Size of arrays, spesified for area of interest
  651.     float Avalue[15][8];
  652.     float Mvalue[15][8];
  653.  
  654.     /// A area is manually found, and arrays of their magnitude and angle is created
  655.     for (int i = 295; i < 310; i++)
  656.     {
  657.         /// Is uesed if we want to utilize the intensity (value) of each byte
  658.         unsigned char* pByte = reshaped.ptr<unsigned char>(i);
  659.  
  660.         for (int j = 730; j < 738; j++) {
  661.  
  662.             /// We need to check if the area is in the RHP, LFH and what complex plane its in
  663.             if (j > averageX) //Then we need abs sign
  664.             {
  665.                 distanceX = abs(averageX - j);
  666.             }
  667.             if (j < averageX)
  668.             {
  669.                 distanceX = (averageX - j);
  670.             }
  671.             if (i > averageY) //Then we need abs sign
  672.             {
  673.                 distanceY = abs(averageY - i);
  674.             }
  675.             if (i < averageY)
  676.             {
  677.                 distanceY = (averageY - i);
  678.             }
  679.  
  680.             /// Printing out the angle and distance
  681.             angle = atan2(distanceY, distanceX);
  682.             magnitude = sqrt(abs(distanceX*distanceX) + abs(distanceY*distanceY));
  683.  
  684.             /// Need to substract from the area we are cuurrently looking at
  685.             Avalue[i - 295][j - 730] = angle;
  686.             Mvalue[i - 295][j - 730] = magnitude;
  687.  
  688.  
  689.             /// Function if converting to degrees is desired
  690.             /*
  691.             double rad2angle;
  692.             rad2angle = angle*(180 / (M_PI));
  693.             */
  694.  
  695.             /// If the desired values is liked to be printed out in the console application
  696.             /*
  697.             printf("Mag and angle: %f & %f with value: ", magnitude, angle);
  698.             printf("value %d ", pByte[j]);
  699.             printf("\n");
  700.             */
  701.         }
  702.     }
  703.  
  704.  
  705.     /// The folling function stores the value in seperate matrices
  706.     /*
  707.     Mat AngelMatrix = Mat(15,8, CV_32F, Avalue);
  708.     Mat MagnitudeMatrix = Mat(15, 8, CV_32F, Mvalue);
  709.     printf("\n");
  710.     //To print Mat values:
  711.     //cout << MagnitudeMatrix.at<float>(0, 2) << endl;
  712.     cout << MagnitudeMatrix << endl;
  713.     printf("\n");
  714.     //cout << AngelMatrix.at<float>(0, 2) << endl;
  715.     cout << AngelMatrix << endl;
  716.     */
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.     //imshow("Removed background picture", threshold_Image);
  725.     //cout << threshold_Image << endl;
  726.  
  727.  
  728.  
  729.     waitKey(0);
  730.     return(0);
  731.  
  732.  
  733.  
  734.  
  735.  
  736. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement