Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include "opencv2/opencv.hpp"
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6. using namespace cv;
  7.  
  8.  
  9. int main(){
  10. Mat img=imread("t.jpg",1);
  11. //blur(img,img,Size(3,3));
  12. medianBlur(img, img, 3);
  13. Mat gray;
  14. cvtColor(img,gray,CV_BGR2GRAY);
  15. Canny(gray, gray, 10, 100, 3);
  16. dilate(gray, gray, Mat(), Point(-1,-1),3);
  17.  
  18. Mat mask;
  19. threshold( gray, mask, 10, 255,THRESH_BINARY_INV );
  20. medianBlur(mask, mask, 15);
  21.  
  22. vector< vector <Point> > contours; // Vector for storing contour
  23. vector< Vec4i > hierarchy;
  24. int largest_contour_index=0;
  25. int largest_area=0;
  26.  
  27. Mat dst(mask.rows,mask.cols,CV_8UC1,Scalar::all(0)); //create destination image
  28. findContours( mask.clone(), contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
  29. for( int i = 0; i< contours.size(); i++ ){
  30. double a=contourArea( contours[i],false); // Find the area of contour
  31. if(a>largest_area){
  32. largest_area=a;
  33. largest_contour_index=i; //Store the index of largest contour
  34. }
  35. }
  36.  
  37. rectangle(img,boundingRect(contours[largest_contour_index]),Scalar(0,255,0),1,8,0);
  38. imshow("src",img);
  39. imshow("gray",gray);
  40. imshow("mask",mask);
  41. imshow("mask",mask);
  42. waitKey();
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement