coordinate and dimension of rectangle [closed]
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. 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 ...
So, you have each rectangle on it's own. Each of them has black writing and a color. If you ignore the black, the remaining color should be the color of the rectangle. Compare that to a "true" orange, and the closest is the sign you want.
I thank you for the suggestion, but I do not know how to do. I'm not very handy with opencv, there I'm getting closer now!
Try playing around with color spaces, thresholding, and the inRange function. Once you understand how to use these better, you should be able to do what you ask.
You know tell me a tutorial? Or One example?
Hello, I tried to do what you told me. Except that the result is always all black. How do I see what I do?
include <opencv2 highgui="" highgui.hpp="">
include <opencv2 imgproc="" imgproc.hpp="">
include <cmath>
include <iostream>
using namespace std;
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;
Well, that's an awfully narrow range, so it's possible there are no pixels in that range. I know doing just what you did there with one of my images and a different set of bounds found exactly what I expected it to.
So does it work? Well, then I expand the range. Thank you, there is a tool that allows passatagli the image to define the range that I need?
There are things that make it easier to separate by color. Changing the color space of the image from BGR to one of the others like YUV or HSV could help. YUV is the grayscale intensity in the Y, and the U and V channels describe what color it is. HSV has V as the intensity, S is the saturation of the color and H is the hue of the color.
Try changing to these color spaces and see if that makes Orange easier to separate, letting you use a larger range without accidentally getting the wrong color.