Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "opencv2/opencv.hpp"
- #include <stdio.h>
- using namespace std;
- using namespace cv;
- int main(){
- Mat img=imread("t.jpg",1);
- //blur(img,img,Size(3,3));
- medianBlur(img, img, 3);
- Mat gray;
- cvtColor(img,gray,CV_BGR2GRAY);
- Canny(gray, gray, 10, 100, 3);
- dilate(gray, gray, Mat(), Point(-1,-1),3);
- Mat mask;
- threshold( gray, mask, 10, 255,THRESH_BINARY_INV );
- medianBlur(mask, mask, 15);
- vector< vector <Point> > contours; // Vector for storing contour
- vector< Vec4i > hierarchy;
- int largest_contour_index=0;
- int largest_area=0;
- Mat dst(mask.rows,mask.cols,CV_8UC1,Scalar::all(0)); //create destination image
- findContours( mask.clone(), contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
- for( int i = 0; i< contours.size(); i++ ){
- double a=contourArea( contours[i],false); // Find the area of contour
- if(a>largest_area){
- largest_area=a;
- largest_contour_index=i; //Store the index of largest contour
- }
- }
- rectangle(img,boundingRect(contours[largest_contour_index]),Scalar(0,255,0),1,8,0);
- imshow("src",img);
- imshow("gray",gray);
- imshow("mask",mask);
- imshow("mask",mask);
- waitKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement