Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have code for detect edge using opencv and c++; there is envelop with a stamp. now i want draw rectangle in stamp. blue area is the stamp . how it do? is this possibe? pic is here https://docs.google.com/document/d/1tldQ1J_simAaxSVN58sqjtjQZnCS8e8wBHOK0WlnHtQ/edit?usp=sharing[^]

my code is below. sorry for bad english

C++
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>



using namespace cv;
using namespace std;

int thresh = 180;
int max_thresh = 200;
Mat img_rgb,img_gray,img_bw,canny_output,drawing;
RNG rng(12345);

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    }

   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"


	cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
		 cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);


	 Mat captureframe,con,threshold_output;

    vector<vector><point> > contours; //
   vector<vec4i> hierarchy;

    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

	  cvtColor(frame,frame,CV_RGB2GRAY);

	  vector<vector><point> > contours;
    vector<vec4i> hierarchy;

    Canny( frame, canny_output, thresh, thresh*2, 3 );
    cv::dilate(canny_output, canny_output, cv::Mat(), cv::Point(-1,-1));
   

    findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    drawing = Mat::zeros( canny_output.size(), CV_8UC3 );

    vector<point> approxShape;
    for(size_t i = 0; i < contours.size(); i++){
        approxPolyDP(contours[i], approxShape, arcLength(Mat(contours[i]), true)*0.04, true);
        drawContours(drawing, contours, i, Scalar(255, 0, 0), CV_FILLED);   // fill BLUE
    }

	   cv::namedWindow("Canny");
    cv::imshow("Canny",canny_output);


	cv::namedWindow("Fill");
    cv::imshow("Fill",drawing);


		/*
		 cvtColor(frame,frame,CV_BGR2GRAY);
		GaussianBlur( frame, frame, Size(7,7), 3.0 );

		//  pyrDown(frame, frame, Size(frame.cols/2, frame.rows/2));
		// frame=frame<128;

		
		  threshold( frame, threshold_output, 128, 150, THRESH_BINARY );
		 
		  findContours( threshold_output, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );


  	int largest_area=0;
    int largest_contour_index=0;
    Rect bounding_rect;
    vector<vector><point>> contours; // Vector for storing contour
    vector<vec4i> hierarchy;
   
	 Canny( threshold_output, threshold_output, 15, 15*2, 3 );
   
	findContours( threshold_output, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
	 

    // iterate through each contour.
  int biggestContourIdx = -1;
    float biggestContourArea = 0;
    cv::Mat drawing = cv::Mat::zeros( threshold_output.size(), CV_8UC3 );
    for( int i = 0; i< contours.size(); i++ )
    {
        cv::Scalar color = cv::Scalar(0, 255, 0);
        drawContours( drawing, contours, i, color, 1, 8, hierarchy, 0, cv::Point() );

        float ctArea= cv::contourArea(contours[i]);
        if(ctArea > biggestContourArea)
        {
            biggestContourArea = ctArea;
            biggestContourIdx = i;
        }
		cout<<ctArea;
	cout<<"\n";
    }
	
	
    // if no contour found
    if(biggestContourIdx < 0)
    {
        std::cout << "no contour found" << std::endl;
        return 1;
    }

    // compute the rotated bounding rect of the biggest contour! (this is the part that does what you want/need)
    cv::RotatedRect boundingBox = cv::minAreaRect(contours[biggestContourIdx]);
    // one thing to remark: this will compute the OUTER boundary box, so maybe you have to erode/dilate if you want something between the ragged lines


	
    // draw the rotated rect
    cv::Point2f corners[4];
    boundingBox.points(corners);
	unsigned int area=0;
	
	//	cout<<abs(corners[0].x-corners[1].x);
		//cout<<"\t";

		//cout<<abs(corners[0].y-corners[1].y);
	//	cout<<"\t";

	area=abs(corners[0].x-corners[1].x)*(corners[0].y-corners[1].y);
	////<<area;

//cout<<"\n";
    cv::line(drawing, corners[0], corners[1], cv::Scalar(255,0,255));
    cv::line(drawing, corners[1], corners[2], cv::Scalar(255,0,255));
    cv::line(drawing, corners[2], corners[3], cv::Scalar(255,0,255));
    cv::line(drawing, corners[3], corners[0], cv::Scalar(255,0,255));

	 imshow("MyVideo1", drawing);


	*/
  

         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break; 
       }
    }
    return 0;

}
Posted
Updated 10-Jun-15 21:21pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900