Guest User

Untitled

a guest
Dec 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <opencv2/features2d/features2d.hpp>
  5.  
  6. using namespace std;
  7. using namespace cv;
  8.  
  9. #define THRESHOLD 100
  10.  
  11.  
  12. /*
  13. ** Compares intensity of pixels 1,5,9,13 of the circle surrounding a pixel at (i,j) of an image with its intensity ip.
  14. ** If 3 out of 4 satisfy the threshold FAST constraints : bright (i>ip+t) & dark (i<ip-t),
  15. ** the pixel at (i,j) is considered a possible key point
  16. */
  17. bool couldBeKeyPoint(Mat imageIn, int i, int j, int threshold) {
  18. uchar ip = imageIn.at<unsigned char>(i, j); //intensity of the potential key point
  19. uchar ip9 = imageIn.at<unsigned char>(i, j - 3); //intensity of pixel 1 of the surrounding circle
  20. uchar ip1 = imageIn.at<unsigned char>(i, j + 3); //intensity of pixel 9 of the surrounding circle
  21. uchar ip5 = imageIn.at<unsigned char>(i + 3, j); //intensity of pixel 5 of the surrounding circle
  22. uchar ip13 = imageIn.at<unsigned char>(i - 3, j); //intensity of pixel 13 of the surrounding circle
  23.  
  24. //checking FAST bright constraints on these 4 surrounding pixels
  25. bool b1 = (ip1 >= ip + threshold);
  26. bool b9 = (ip9 >= ip + threshold);
  27. bool b5 = (ip5 >= ip + threshold);
  28. bool b13 = (ip13 >= ip + threshold);
  29.  
  30. //cout << b1+b9+b5+b13 ;
  31. //at least three of these must all be brighter than I_p + t.
  32. if (b1+b9+b5+b13 >=3)
  33. return true;
  34.  
  35. bool d1 = (ip1 <= ip - threshold);
  36. bool d9 = (ip9 <= ip - threshold);
  37. bool d5 = (ip5 <= ip - threshold);
  38. bool d13 = (ip13 <= ip - threshold);
  39. //cout << d1+d9+d5+d13 << "n" ;
  40. //at least three of these must all be darker than I_p − t.
  41. if (d1+d9+d5+d13 >=3)
  42. return true;
  43.  
  44. return false;
  45. }
  46.  
  47. bool isKeyPoint(Mat imageIn, int i, int j, int threshold, int numberPixelsToCheck){
  48. cout << "iskeypoint";
  49. vector<unsigned char> pixelSurroundings;
  50.  
  51. pixelSurroundings.push_back(imageIn.at<unsigned char>(i, j));//the potential key point
  52. pixelSurroundings.push_back(imageIn.at<unsigned char>(i, j + 3));//pixel 1
  53. pixelSurroundings.push_back(imageIn.at<unsigned char>(i + 1, j - 3));//pixel 2
  54. pixelSurroundings.push_back(imageIn.at<unsigned char>(i + 2, j + 2));//pixel 3
  55. pixelSurroundings.push_back(imageIn.at<unsigned char>(i + 3, j + 1));//pixel 4
  56. pixelSurroundings.push_back(imageIn.at<unsigned char>(i + 3, j));//pixel 5
  57. pixelSurroundings.push_back(imageIn.at<unsigned char>(i + 3, j - 1));//pixel 6
  58. pixelSurroundings.push_back(imageIn.at<unsigned char>(i + 2, j - 2));//pixel 7
  59. pixelSurroundings.push_back(imageIn.at<unsigned char>(i + 1, j - 3));//pixel 8
  60. pixelSurroundings.push_back(imageIn.at<unsigned char>(i, j - 3));//pixel 9
  61. pixelSurroundings.push_back(imageIn.at<unsigned char>(i - 1, j - 3));//pixel 10
  62. pixelSurroundings.push_back(imageIn.at<unsigned char>(i - 2, j - 2));//pixel 11
  63. pixelSurroundings.push_back(imageIn.at<unsigned char>(i - 3, j - 1));//pixel 12
  64. pixelSurroundings.push_back(imageIn.at<unsigned char>(i - 3, j));//pixel 13
  65. pixelSurroundings.push_back(imageIn.at<unsigned char>(i - 3, j + 1));//pixel 14
  66. pixelSurroundings.push_back(imageIn.at<unsigned char>(i - 2, j + 2));//pixel 15
  67. pixelSurroundings.push_back(imageIn.at<unsigned char>(i - 1, j + 3));//pixel 16
  68.  
  69. if (numberPixelsToCheck > 16){
  70. numberPixelsToCheck = 12; //The author have used N=12 in the first version of the algorithm
  71. cout << "Error number of surrounding pixels to check should not exceed 16! Value 12 was used instead. " << std::endl ;
  72. }
  73.  
  74. unsigned char ip = pixelSurroundings[0];
  75. int brightScore = 0;
  76. int darkScore = 0;
  77. bool d = false,e=false;
  78. for(int j=1;j<pixelSurroundings.size();j++){
  79. unsigned char i = pixelSurroundings[j];
  80. d = (i >= ip + (unsigned char ) threshold);
  81. e = (i <= ip - (unsigned char ) threshold);
  82.  
  83. brightScore += d;
  84. darkScore += e;
  85. }
  86. cout << darkScore << " DARKSCORE n";
  87. cout << brightScore << " BRIGHTSCORE n";
  88. if (darkScore >= numberPixelsToCheck || brightScore >= numberPixelsToCheck){
  89. //cout << darkScore << " DARKSCORE n";
  90. //cout << brightScore << " BRIGHTSCORE n";
  91. return true; //the pixel is a key point
  92. }
  93.  
  94. return false;
  95. }
  96.  
  97. //renvoit un ensemble de détections
  98. //inputarray image
  99. vector<KeyPoint> FAST(Mat imageIn, vector<KeyPoint> keypoints, int threshold){
  100. if(!imageIn.data ) // Check for invalid input
  101. {
  102. cout << "Could not open or find the image" << std::endl ;
  103. //return {};
  104. }
  105. keypoints.clear();
  106. int i, j, count =0;
  107. for (i = 3; i < imageIn.rows - 3; i++)
  108. {
  109. for (j = 3; j < imageIn.cols - 3; j++)
  110. {
  111. if (couldBeKeyPoint(imageIn, i, j, threshold)){
  112. if (isKeyPoint(imageIn, i, j, threshold, 0)){
  113. keypoints.push_back(KeyPoint(j, i ,1));
  114. count++;
  115. cout << "keypoint found at " << i << " " << j << "n";
  116. }
  117. }
  118. }
  119. }
  120. cout << "NUMBER OF KEYPOINTS :" << keypoints.size() << "n";
  121. return keypoints;
  122. }
  123.  
  124.  
  125. int main(int argc, char** argv){
  126. vector<KeyPoint> keypointsMyFast;
  127. vector<KeyPoint> keypointsOpenCvFast;
  128. Mat src, destMyFast, destOpenCvFast;
  129. //src= imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
  130. //src= imread(argv[1], CV_8UC3);
  131. src= imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
  132. imshow( "ORGINAL",src);
  133. waitKey(1);
  134.  
  135. keypointsMyFast = FAST(src, keypointsMyFast,THRESHOLD);
  136. drawKeypoints(src, keypointsMyFast, destMyFast, Scalar(255,0,0));
  137. imshow( "MYFAST",destMyFast);
  138. waitKey(1);
  139. FAST(src,keypointsOpenCvFast,THRESHOLD,false);
  140. cout << "NUMBER OF open cv KEYPOINTS :" << keypointsOpenCvFast.size() << "n";
  141. drawKeypoints(src, keypointsOpenCvFast, destOpenCvFast, Scalar(255,0,0));
  142. imshow( "Display window",destOpenCvFast);
  143. waitKey(0);
  144.  
  145.  
  146. }
Add Comment
Please, Sign In to add comment