Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. if ((new File(path + "trial.jpg")).exists()) {
  2. opencv_core.IplImage originalImage = opencv_imgcodecs.cvLoadImage(path + "trial.jpg", opencv_imgcodecs.CV_IMWRITE_JPEG_QUALITY);
  3. opencv_core.IplImage iplImage = opencv_imgcodecs.cvLoadImage(path + "trial.jpg", opencv_imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
  4. opencv_core.IplImage edgeDetectedImage = applyCannyRectangleEdgeDetection(iplImage, 80);
  5. opencv_core.CvSeq largestContour = findLargestContour(edgeDetectedImage);
  6. opencv_core.CvPoint[] cvPoints = new opencv_core.CvPoint[4];
  7. for(int i=0; i<largestContour.total();i++)
  8. {
  9. opencv_core.CvPoint cvPoint = new opencv_core.CvPoint(cvGetSeqElem(largestContour, i));
  10. cvPoints[i] = cvPoint;
  11. }
  12. cvDrawLine(originalImage, cvPoints[0], cvPoints[1], opencv_core.CvScalar.YELLOW, 10, 10, 10);
  13. cvDrawLine(originalImage, cvPoints[1], cvPoints[2], opencv_core.CvScalar.YELLOW, 10, 10, 10);
  14. cvDrawLine(originalImage, cvPoints[2], cvPoints[3], opencv_core.CvScalar.YELLOW, 10,10, 10);
  15. cvDrawLine(originalImage, cvPoints[3], cvPoints[0], opencv_core.CvScalar.YELLOW, 10, 10,10);
  16. opencv_imgcodecs.cvSaveImage(path + "img1.jpg", originalImage);
  17. if ((new File(path + "img1.jpg").exists())) {
  18. imageView.setImageDrawable(Drawable.createFromPath(path + "img1.jpg"));
  19. }
  20. }
  21.  
  22. private opencv_core.IplImage applyCannyRectangleEdgeDetection(opencv_core.IplImage iplImage, int percent) {
  23. opencv_core.IplImage destImage = downScaleImage(iplImage, percent);
  24. OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
  25. Frame grayImageFrame = converterToMat.convert(destImage);
  26. opencv_core.Mat grayImageMat = converterToMat.convertToMat(grayImageFrame);
  27. GaussianBlur(grayImageMat, grayImageMat, new opencv_core.Size(5, 5), 0.0, 0.0, BORDER_DEFAULT);
  28. destImage = converterToMat.convertToIplImage(grayImageFrame);
  29. cvErode(destImage, destImage);
  30. cvDilate(destImage, destImage);
  31. cvCanny(destImage, destImage, 20, 55);
  32. return destImage;
  33. }
  34.  
  35. private opencv_core.IplImage downScaleImage(opencv_core.IplImage srcImage, int percent) {
  36. opencv_core.IplImage destImage = cvCreateImage(cvSize((srcImage.width() * percent) / 100, (srcImage.height() * percent) / 100), srcImage.depth(), srcImage.nChannels());
  37. cvResize(srcImage, destImage);
  38. return destImage;
  39. }
  40.  
  41. private opencv_core.CvSeq findLargestContour(opencv_core.IplImage edgeDetectedImage) {
  42. opencv_core.IplImage foundContoursOfImage = cvCloneImage(edgeDetectedImage);
  43. opencv_core.CvMemStorage memory = new opencv_core.CvMemStorage().create();
  44. opencv_core.CvSeq contours = new opencv_core.CvSeq();
  45. cvFindContours(foundContoursOfImage, memory, contours, Loader.sizeof(opencv_core.CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, new opencv_core.CvPoint(0, 0));
  46. int maxWidth = 0;
  47. int maxHeight = 0;
  48. opencv_core.CvRect contr = null;
  49. opencv_core.CvSeq seqFound = null;
  50. opencv_core.CvSeq nextSeq;
  51. for (nextSeq = contours; nextSeq != null; nextSeq = nextSeq.h_next()) {
  52. contr = cvBoundingRect(nextSeq, 0);
  53. if ((contr.width() >= maxWidth) && (contr.height() >= maxHeight)) {
  54. maxHeight = contr.height();
  55. maxWidth = contr.width();
  56. seqFound = nextSeq;
  57. }
  58. }
  59. opencv_core.CvSeq result = cvApproxPoly(seqFound, Loader.sizeof(opencv_core.CvContour.class), memory, CV_POLY_APPROX_DP, cvContourPerimeter(seqFound) * 0.1, 0);
  60. return result;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement