Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

coordinate and dimension of rectangle

Hello guys, I apologize for my bad English. I have a problem. I found this source.

It works, because it identifies the rectangles that make up the picture. Only that I should extract from the each rectangle to analyze their content with tesseract.

I thought about putting in a file the coordinates of each rectangle and its dimensions (height and width). So then the process with another program on purpose. I just do not understand how. Who tells me how to do it?

My use is to isolate the only part of the picture of the orange plate.image description to read a 2251073.

My code attachment

/** * 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();


//Al momento il cuore del mio algoritmo, mette dei contorni così spessi da  coprire tutto ciò che non è un
cv::drawContours(dst, contours, -1, (0,255,0), 3);  

for (int i = 0; i < contours.size(); i++)

{ // Contorno approssimativo con una precisione proporzionale // al perimetro contorno cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true)*0.02, true);

// Salta piccole o non-convesse oggetti if (std::fabs(cv::contourArea(contours[i])) < 100 || !cv::isContourConvex(approx)) continue;

if (approx.size() == 3) { //setLabel(dst, "TRI", contours[i]); // Triangles } else if (approx.size() >= 4 && approx.size() <= 6) { // Number of vertices of polygonal curve int vtc = approx.size();

// Get the cosines of all corners std::vector<double> cos; for (int j = 2; j < vtc+1; j++) cos.push_back(angle(approx[j%vtc], approx[j-2], approx[j-1]));

// Sort ascending the cosine values std::sort(cos.begin(), cos.end());

// Get the lowest and the highest cosine double mincos = cos.front(); double maxcos = cos.back();

// Use the degrees obtained above and the number of vertices // to determine the shape of the contour if (vtc == 4 && mincos >= -0.1 && maxcos <= 0.3) {

cout << "---" << i << "---" << endl; for (int j = 0; j < contours[i].size(); j++) {

cout << contours[i][j].x << "x" << contours[i][j].y << " "; cout << endl;

}

//setLabel(dst, "RECT", contours[i]); }else if (vtc == 5 && mincos >= -0.34 && maxcos <= -0.27) { //setLabel(dst, "PENTA", contours[i]); }else if (vtc == 6 && mincos >= -0.55 && maxcos <= -0.45) { //setLabel(dst, "HEXA", contours[i]); } } else { // Detect and label circles /*double area = cv::contourArea(contours[i]); cv::Rect r = cv::boundingRect(contours[i]); int radius = r.width / 2;

if (std::abs(1 - ((double)r.width / r.height)) <= 0.2 && std::abs(1 - (area / (CV_PI * std::pow(radius, 2)))) <= 0.2) setLabel(dst, "CIR", contours[i]);*/ } }

imwrite( argv[2], dst );

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