Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Don't read my rectangle

I adapted this code to read the white rectangle with the word 23-1965

But, it does not detect it sees a lot of other side dishes, but not what interests me. Who tells me where I'm wrong? Also attach the image to be processedimage description

/** * Simple shape detector program. * It loads an image and tries to find simple shapes (rectangle, triangle, circle, etc) in it. * This program is a modified version of squares.cpp found in the OpenCV sample dir. */

include <opencv2 highgui="" highgui.hpp="">

include <opencv2 imgproc="" imgproc.hpp="">

include <cmath>

include <iostream>

using namespace std;

/* * Helper function to find a cosine of angle between vectors * from pt0->pt1 and pt0->pt2 */ static double angle(cv::Point pt1, cv::Point pt2, cv::Point pt0) { double dx1 = pt1.x - pt0.x; double dy1 = pt1.y - pt0.y; double dx2 = pt2.x - pt0.x; double dy2 = pt2.y - pt0.y; return (dx1dx2 + dy1dy2)/sqrt((dx1dx1 + dy1dy1)(dx2dx2 + dy2dy2) + 1e-10); }

/** * Helper function to display text in the center of a contour */ void setLabel(cv::Mat& im, const std::string label, std::vector<cv::point>& contour) { int fontface = cv::FONT_HERSHEY_SIMPLEX; double scale = 0.4; int thickness = 1; int baseline = 0;

cv::Size text = cv::getTextSize(label, fontface, scale, thickness, &baseline);
cv::Rect r = cv::boundingRect(contour);

cv::Point pt(r.x + ((r.width - text.width) / 2), r.y + ((r.height + text.height) / 2));
cv::rectangle(im, pt + cv::Point(0, baseline), pt + cv::Point(text.width, -text.height), CV_RGB(255,255,255), CV_FILLED);
cv::putText(im, label, pt, fontface, scale, CV_RGB(0,0,0), thickness, 8);

}

int main(int argc, char** argv) { //cv::Mat src = cv::imread("polygon.png"); cv::Mat src = cv::imread(argv[1]); if (src.empty()) return -1;

// Converti in scala di grigi
cv::Mat gray;
cv::cvtColor(src, gray, CV_BGR2GRAY);

// Utilizzare Canny invece di soglia per la cattura di piazze con ombreggiatura gradiente
cv::Mat bw;
cv::Canny(gray, bw, 0, 50, 5);

// Trova contorni
std::vector<std::vector<cv::Point> > contours;
cv::findContours(bw.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);   

std::vector<cv::Point> approx;
cv::Mat dst = src.clone();


for(unsigned int l=0;l<contours.size();l++)
{

    if (contours[l].size() == 4 /*&& contours[l].size() <= 6*/)
    {

            //cout << "# of contour points: " << contours[l].size() << endl ;
        cout << endl ;
            for(unsigned int k=0;k<contours[l].size();k++)
            {
            //cout << "Point(x,y)=" << contours[l][k] << endl;
            cout << contours[l][k] << ";";
            }
            //cout << " Area: " << contourArea(contours[l]) << endl;
    }
}

//cv::imshow("src", src);
//cv::imshow("dst", dst);
cv::waitKey(0);
return 0;

}

click to hide/show revision 2
No.2 Revision

updated 2016-02-10 09:06:13 -0600

berak gravatar image

Don't read my rectangle

I adapted this code to read the white rectangle with the word 23-1965

But, it does not detect it sees a lot of other side dishes, but not what interests me. Who tells me where I'm wrong? Also attach the image to be processedimage description

/**
 * Simple shape detector program.
 * It loads an image and tries to find simple shapes (rectangle, triangle, circle, etc) in it.
 * This program is a modified version of squares.cpp `squares.cpp` found in the OpenCV sample dir.
 */

include <opencv2 highgui="" highgui.hpp="">

include <opencv2 imgproc="" imgproc.hpp="">

include <cmath>

include <iostream>

*/ #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <cmath> #include <iostream> using namespace std;

/* std; /** * Helper function to find a cosine of angle between vectors * from pt0->pt1 and pt0->pt2 */ static double angle(cv::Point pt1, cv::Point pt2, cv::Point pt0) { double dx1 = pt1.x - pt0.x; double dy1 = pt1.y - pt0.y; double dx2 = pt2.x - pt0.x; double dy2 = pt2.y - pt0.y; return (dx1dx2 + dy1dy2)/sqrt((dx1dx1 + dy1dy1)(dx2dx2 + dy2dy2) (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10); }

} /** * Helper function to display text in the center of a contour */ void setLabel(cv::Mat& im, const std::string label, std::vector<cv::point>& std::vector<cv::Point>& contour) { int fontface = cv::FONT_HERSHEY_SIMPLEX; double scale = 0.4; int thickness = 1; int baseline = 0;

0;

    cv::Size text = cv::getTextSize(label, fontface, scale, thickness, &baseline);
 cv::Rect r = cv::boundingRect(contour);

 cv::Point pt(r.x + ((r.width - text.width) / 2), r.y + ((r.height + text.height) / 2));
 cv::rectangle(im, pt + cv::Point(0, baseline), pt + cv::Point(text.width, -text.height), CV_RGB(255,255,255), CV_FILLED);
 cv::putText(im, label, pt, fontface, scale, CV_RGB(0,0,0), thickness, 8);

}

} int main(int argc, char** argv) { //cv::Mat src = cv::imread("polygon.png"); cv::Mat src = cv::imread(argv[1]); if (src.empty()) return -1;

-1;

    // Converti in scala di grigi
 cv::Mat gray;
 cv::cvtColor(src, gray, CV_BGR2GRAY);

 // Utilizzare Canny invece di soglia per la cattura di piazze con ombreggiatura gradiente
 cv::Mat bw;
 cv::Canny(gray, bw, 0, 50, 5);

 // Trova contorni
 std::vector<std::vector<cv::Point> > contours;
 cv::findContours(bw.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);   

 std::vector<cv::Point> approx;
 cv::Mat dst = src.clone();


 for(unsigned int l=0;l<contours.size();l++)
 {

     if (contours[l].size() == 4 /*&& contours[l].size() <= 6*/)
     {

             //cout << "# of contour points: " << contours[l].size() << endl ;
         cout << endl ;
             for(unsigned int k=0;k<contours[l].size();k++)
             {
             //cout << "Point(x,y)=" << contours[l][k] << endl;
             cout << contours[l][k] << ";";
             }
             //cout << " Area: " << contourArea(contours[l]) << endl;
     }
 }

 //cv::imshow("src", src);
 //cv::imshow("dst", dst);
 cv::waitKey(0);
 return 0;
}

}