fork download
  1. #include<vector>
  2. #include<opencv\cv.h>
  3. #include<opencv\highgui.h>
  4. #include<opencv2/imgproc/imgproc.hpp>
  5. #include<cstdlib>
  6. #include<iostream>
  7. #include<cmath>
  8. #include<omp.h>
  9.  
  10. using namespace cv;
  11. using namespace std;
  12.  
  13. Scalar meanpixel(Mat matrx)
  14. {
  15. Scalar meanpix;
  16.  
  17. #pragma omp parallel for
  18. for(int i=0;i<matrx.rows;i++)
  19. {
  20. for(int j=0;j<matrx.cols;j++)
  21. {
  22. meanpix[0]=meanpix[0]+double(matrx.at<Vec3b>(i,j)[0]);
  23. meanpix[1]=meanpix[1]+double(matrx.at<Vec3b>(i,j)[1]);
  24. meanpix[2]=meanpix[2]+double(matrx.at<Vec3b>(i,j)[2]);
  25. }
  26. }
  27. meanpix[0]=floor((meanpix[0]/(matrx.rows*matrx.cols))+0.5);
  28. meanpix[1]=floor((meanpix[1]/(matrx.rows*matrx.cols))+0.5);
  29. meanpix[2]=floor((meanpix[2]/(matrx.rows*matrx.cols))+0.5);
  30. return meanpix;
  31. }
  32.  
  33. Mat covarmatrixcal(Mat matrx1, Scalar meanscal)
  34. {
  35.  
  36. double rr=0,rg=0,rb=0,gr=0,gg=0,gb=0,br=0,bg=0,bb=0;
  37. int k=0;
  38. k=(matrx1.rows*matrx1.cols);
  39. Vec3b temp1;
  40. #pragma omp parallel for
  41. for(int i = 0; i < matrx1.rows;i++)
  42. {
  43. for(int j=0;j<matrx1.cols;j++)
  44. {
  45. temp1=matrx1.at<Vec3b>(i,j);
  46. rr=rr+((double(temp1[0])-meanscal[0])*(double(temp1[0])-meanscal[0]));
  47. rg=rg+((double(temp1[0])-meanscal[0])*(double(temp1[1])-meanscal[1]));
  48. rb=rb+((double(temp1[0])-meanscal[0])*(double(temp1[2])-meanscal[2]));
  49. gg=gg+((double(temp1[1])-meanscal[1])*(double(temp1[1])-meanscal[1]));
  50. gb=gb+((double(temp1[1])-meanscal[1])*(double(temp1[2])-meanscal[2]));
  51. bb=bb+((double(temp1[2])-meanscal[2])*(double(temp1[2])-meanscal[2]));
  52. }
  53. }
  54. rr=rr/(k-1);
  55. rg=rg/(k-1);
  56. rb=rb/(k-1);
  57. gr=rg;
  58. gg=gg/(k-1);
  59. gb=gb/(k-1);
  60. br=rb;
  61. bg=gb;
  62. bb=bb/(k-1);
  63.  
  64. double m[3][3]={{rr,rg,rb},{gr,gg,gb},{br,bg,bb}};
  65.  
  66. Mat covmatrix(3,3,CV_64F,m);
  67. return covmatrix.clone();
  68.  
  69. }
  70.  
  71. double mahadistance(Mat invcov, Scalar meanvec, Vec3b patternvec)
  72. {
  73. double distmeasure=0;
  74. Scalar x1,x2;
  75.  
  76. //mahalanobis distance is equal to (x-mean)^T*inv(cov)*(x-mean)
  77. x1[0]=(patternvec[0]-meanvec[0]);
  78. x1[1]=(patternvec[1]-meanvec[1]);
  79. x1[2]=(patternvec[2]-meanvec[2]);
  80.  
  81. #pragma omp parallel for
  82. for(int k = 0; k < 3;k++)
  83. {
  84. x2[0]=x2[0]+(x1[k]*invcov.at<double>(k,0));
  85. x2[1]=x2[1]+(x1[k]*invcov.at<double>(k,1));
  86. x2[2]=x2[2]+(x1[k]*invcov.at<double>(k,2));
  87. }
  88.  
  89. distmeasure=((x2[0]*x1[0])+(x2[1]*x1[1])+(x2[2]*x1[2]));
  90. return distmeasure;
  91. }
  92.  
  93. Mat inversemat(Mat matrx)
  94. {
  95. double determ;
  96. determ = (matrx.at<double>(0,0)*((matrx.at<double>(1,1)*matrx.at<double>(2,2))-(matrx.at<double>(1,2)*matrx.at<double>(2,1))))-(matrx.at<double>(0,1)*((matrx.at<double>(1,0)*matrx.at<double>(2,2))-(matrx.at<double>(2,0)*matrx.at<double>(1,2))))+(matrx.at<double>(0,2)*((matrx.at<double>(1,0)*matrx.at<double>(2,1))-(matrx.at<double>(1,1)*matrx.at<double>(2,0))));
  97. Mat mt=matrx.t();
  98. double m11,m12,m13,m21,m22,m23,m31,m32,m33;
  99. m11=(mt.at<double>(1,1)*mt.at<double>(2,2))-(mt.at<double>(2,1)*mt.at<double>(1,2));
  100. m12=(mt.at<double>(1,0)*mt.at<double>(2,2))-(mt.at<double>(2,0)*mt.at<double>(1,2));
  101. m13=(mt.at<double>(1,0)*mt.at<double>(2,1))-(mt.at<double>(2,0)*mt.at<double>(1,1));
  102. m21=(mt.at<double>(0,1)*mt.at<double>(2,2))-(mt.at<double>(2,1)*mt.at<double>(0,2));
  103. m22=(mt.at<double>(0,0)*mt.at<double>(2,2))-(mt.at<double>(2,0)*mt.at<double>(0,2));
  104. m23=(mt.at<double>(0,0)*mt.at<double>(2,1))-(mt.at<double>(2,0)*mt.at<double>(0,1));
  105. m31=(mt.at<double>(0,1)*mt.at<double>(1,2))-(mt.at<double>(1,1)*mt.at<double>(0,2));
  106. m32=(mt.at<double>(0,0)*mt.at<double>(1,2))-(mt.at<double>(1,0)*mt.at<double>(0,2));
  107. m33=(mt.at<double>(0,0)*mt.at<double>(1,1))-(mt.at<double>(1,0)*mt.at<double>(0,1));
  108.  
  109. double imat[3][3]={{(m11/determ),(-m12/determ),(m13/determ)},{(-m21/determ),(m22/determ),(-m23/determ)},{(m31/determ),(-m32/determ),(m33/determ)}};
  110. Mat invmat(3,3,CV_64F,imat);
  111. return invmat.clone(); //as only the address to where the data is stored is passed to the function the object is destroyed so an empty matrix is returned with initial values -9.23842e+61
  112. }
  113.  
  114. Mat imagesegmentation(Mat image, Mat icovar, Scalar meanmat,double distance, Mat mask, Mat element)
  115. {
  116. #pragma omp parallel for
  117. for(int i = 0; i < image.rows;i++)
  118. {
  119. for(int j=0; j<image.cols;j++) //in case it fails changed it from i=1 to i=0
  120. {
  121. Vec3b pixel= image.at<Vec3b>(i,j); //prints wierd characters
  122. double mdist=mahadistance(icovar,meanmat,pixel);
  123. if(mdist<distance)
  124. mask.at<uchar>(i,j)=255;
  125. else
  126. mask.at<uchar>(i,j)=0;
  127. }
  128. }
  129. Mat mask1=mask;
  130. morphologyEx(mask,mask1,2,element);
  131. return mask;
  132. }
  133.  
  134. Mat Cornerdetect(Mat image, Mat corners,int numcorners)
  135. {
  136. Mat grayscale;
  137. #pragma omp parallel sections
  138. {
  139. #pragma omp section
  140. { cvtColor(image,grayscale,CV_BGR2GRAY); }
  141. //goodfeatures(grayimage, output to store corners, quality factor, distance factor)
  142. #pragma omp section
  143. { goodFeaturesToTrack(grayscale,corners,numcorners,0.01,70); }
  144. // Mark these corners on the original image
  145. }
  146.  
  147. if(corners.rows!=0)
  148. {
  149. cornerSubPix(grayscale, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
  150. }
  151. return corners;
  152. }
  153.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:22: fatal error: opencv\cv.h: No such file or directory
 #include<opencv\cv.h>
                      ^
compilation terminated.
stdout
Standard output is empty