Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. +(UIImage*)confirmedImage
  2. :(UIImage *)sourceImage
  3. :(CGFloat)contentScale
  4. :(CGPoint)point1
  5. :(CGPoint)point2
  6. :(CGPoint)point3
  7. :(CGPoint)point4
  8. {
  9.  
  10. vector<Point2f> pointsToSort;
  11. pointsToSort.push_back(Point2f(point1.x, point1.y));
  12. pointsToSort.push_back(Point2f(point2.x, point2.y));
  13. pointsToSort.push_back(Point2f(point3.x, point3.y));
  14. pointsToSort.push_back(Point2f(point4.x, point4.y));
  15.  
  16. NSLog(@"npoints before sorting in objective c");
  17.  
  18. vector<Point2f> sortedPoints = [self sortCorners:pointsToSort];
  19. CGPoint p1 = CGPointMake(CGFloat(sortedPoints[0].x), CGFloat(sortedPoints[0].y));
  20. CGPoint p2 = CGPointMake(CGFloat(sortedPoints[1].x), CGFloat(sortedPoints[1].y));
  21. CGPoint p3 = CGPointMake(CGFloat(sortedPoints[2].x), CGFloat(sortedPoints[2].y));
  22. CGPoint p4 = CGPointMake(CGFloat(sortedPoints[3].x), CGFloat(sortedPoints[3].y));
  23.  
  24. NSLog(@"npoint1.x %f", p1.x);
  25. NSLog(@"point1.y %f", p1.y);
  26. NSLog(@"npoint1.x %f", p2.x);
  27. NSLog(@"point1.y %f", p2.y);
  28. NSLog(@"npoint1.x %f", p3.x);
  29. NSLog(@"point1.y %f", p3.y);
  30. NSLog(@"npoint1.x %f", p4.x);
  31. NSLog(@"point1.y %f", p4.y);
  32.  
  33. NSLog(@"npoints after sorting in objective c");
  34.  
  35. CGFloat scaleFactor = contentScale;
  36.  
  37. NSLog(@"n scaleFactor: %f", scaleFactor);
  38.  
  39. CGPoint ptBottomLeft = CGPointMake(CGFloat(p1.x / scaleFactor), CGFloat(p1.y / scaleFactor));
  40. CGPoint ptBottomRight = CGPointMake(CGFloat(p2.x / scaleFactor), CGFloat(p2.y / scaleFactor));
  41. CGPoint ptTopRight = CGPointMake(CGFloat(p3.x / scaleFactor), CGFloat(p3.y / scaleFactor));
  42. CGPoint ptTopLeft = CGPointMake(CGFloat(p4.x / scaleFactor), CGFloat(p4.y / scaleFactor));
  43.  
  44. CGFloat w1 = sqrt( pow(ptBottomRight.x - ptBottomLeft.x , 2) + pow(ptBottomRight.x - ptBottomLeft.x, 2));
  45. CGFloat w2 = sqrt( pow(ptTopRight.x - ptTopLeft.x , 2) + pow(ptTopRight.x - ptTopLeft.x, 2));
  46.  
  47. CGFloat h1 = sqrt( pow(ptTopRight.y - ptBottomRight.y , 2) + pow(ptTopRight.y - ptBottomRight.y, 2));
  48. CGFloat h2 = sqrt( pow(ptTopLeft.y - ptBottomLeft.y , 2) + pow(ptTopLeft.y - ptBottomLeft.y, 2));
  49.  
  50. NSLog(@"n w1 : %f", w1);
  51. NSLog(@"n w2 : %f", w2);
  52. NSLog(@"n h1 : %f", h1);
  53. NSLog(@"n h2 : %f", h2);
  54.  
  55. CGFloat maxWidth = (w1 < w2) ? w1 : w2;
  56. CGFloat maxHeight = (h1 < h2) ? h1 : h2;
  57.  
  58. NSLog(@"n maxWidth : %f", maxWidth);
  59. NSLog(@"n maxHeight : %f", maxHeight);
  60. cv::Point2f src[4], dst[4];
  61. src[0].x = ptTopLeft.x;
  62. src[0].y = ptTopLeft.y;
  63. src[1].x = ptTopRight.x;
  64. src[1].y = ptTopRight.y;
  65. src[2].x = ptBottomLeft.x;
  66. src[2].y = ptBottomLeft.y;
  67. src[3].x = ptBottomRight.x;
  68. src[3].y = ptBottomRight.y;
  69.  
  70. dst[0].x = 0;
  71. dst[0].y = 0;
  72. dst[1].x = maxWidth - 1;
  73. dst[1].y = 0;
  74. dst[2].x = 0;
  75. dst[2].y = maxHeight - 1;
  76. dst[3].x = maxWidth - 1;
  77. dst[3].y = maxHeight - 1;
  78.  
  79. // dst[0].x = 0;
  80. // dst[0].y = 0;
  81. // dst[1].x = 0;
  82. // dst[1].y = maxHeight - 1;
  83. // dst[2].x = maxWidth - 1;
  84. // dst[2].y = maxHeight - 1;
  85. // dst[3].x = maxWidth - 1;
  86. // dst[3].y = 0;
  87.  
  88. //ORIG
  89. // dst[0].x = 0;
  90. // dst[0].y = 0;
  91. // dst[1].x = maxWidth - 1;
  92. // dst[1].y = 0;
  93. // dst[2].x = maxWidth - 1;
  94. // dst[2].y = maxHeight - 1;
  95. // dst[3].x = 0;
  96. // dst[3].y = maxHeight - 1;
  97.  
  98. cv::Size matSize = cv::Size(maxWidth, maxHeight);
  99.  
  100. cv::Mat undistorted = cv::Mat(matSize, CV_8UC1);
  101. cv::Mat original = [OpenCVWrapperInternal cvMatFromUIImage:sourceImage];
  102. cv::warpPerspective(original, undistorted, cv::getPerspectiveTransform(src, dst), matSize);
  103. original.release();
  104.  
  105. // Rotate
  106. cv::Mat rotated;
  107. rotate(undistorted, rotated, 2);
  108. // Flip
  109. // cv:: Mat flipped;
  110. // cv::flip(rotated, flipped, 1);
  111.  
  112.  
  113. UIImage *newImage = [OpenCVWrapperInternal UIImageFromCVMat:undistorted :sourceImage];
  114.  
  115. undistorted.release();
  116. // flipped.release();
  117. rotated.release();
  118.  
  119. return newImage;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement