1

First port here, don't hesitate in correcting me if I forgot to specify something essential in this post.

I have a little problem with some calculations I made, and I'm unable to solve this alone.

I'm using Optical Character Recognition (OCR) with Python to read some street signs. Most of the street signs are read correctly, but some of them are rotated, and so the OCR image frame and the street sign frame don't align with each other.

Image with the two rectangles - the black one being the OCR image frame, and the red one being the street sign enter image description here The width and height of the red rectangle (a, b) are in millimeters, and the width and height of the black rectangle (x, y) are in pixels.

Is there any way to know what which coordinates (in pixels) of the black rectangle, the red rectangle's vertexes touch? Is it actually even feasible?

Thank you for your help!

Peter
  • 13
  • You maybe able to convert mm to pixel (or v.v.) using https://www.unitconverters.net/typography/millimeter-to-pixel-x.htm – NoChance Jul 11 '19 at 22:02
  • I would try to convert them, but sometimes the street signs are further in the photo, and some are closer. Thankfully enough, they have a standard size (30cm x 8cm), but I'm unsure how to proceed with the calculations to know the coordinates – Peter Jul 11 '19 at 22:04
  • Unless the street sign is parallel to the camera’s image plane, its image isn’t even rectangular, although you can probably get away with approximating it as one. – amd Jul 12 '19 at 00:29

2 Answers2

1

If we call the small piece of the long side $w$ and the small piece of the vertical side $z$ we have $$(x-w)^2+z^2=a^2\\(y-z)^2+w^2=b^2$$ and we are searching for $w,z$ if we know all the other variables. $$x^2-2xw+w^2+z^2=a^2\\ y^2-2yz+z^2+w^2=b^2\\ x^2-y^2+2yz-2wx=a^2-b^2\\ w=\frac{x^2-y^2+2yz-a^2+b^2}{2x}$$ Plug this into one of the equations and you have a quadratic in $z$

Ross Millikan
  • 374,822
  • Are you assuming that $x$ and $a$ are measured in the same units? – Henry Jul 11 '19 at 22:00
  • Have you ensured that the inner rectangle is a rectangle, i.e. that going round the four sides you get back to where you started? – Henry Jul 11 '19 at 22:02
  • @Henry: Yes, I am. They are both in the picture, so they should both be in pixels. That is the data we have available. Yes, I am assuming that the inner shape is a parallelogram, though it was specified to be a rectangle in the question. That will be true if the sign is perpendicular to the line of sight. If one side of the sign is further from the camera than the other the length of the sides will not be equal. – Ross Millikan Jul 11 '19 at 22:04
  • x and a are not the same units. x is in pixels, while a is in millimeters, and I did write that in the post. If it helps for the equations, the street signs always have the same dimensions (30cm x 8cm = 300mm x 80mm).

    Moreover, I'm somewhat confused by your equations (my brain's completely fried at this point from trying so hard to solve this problem). w and z are not known, but you still include both in your equations?

    – Peter Jul 11 '19 at 22:07
  • I am solving for $w$ and $z$. You should be able to measure them from the photo, as you can $a$ and $b$. Are you sure you are asking the right question? Maybe it should be given an angular offset of the $ab$ rectangle how large is the axis aligned $xy$ rectangle. I believe that has already been done on the site. – Ross Millikan Jul 11 '19 at 22:10
  • This post may be useful if that is what you are after – Ross Millikan Jul 11 '19 at 22:15
  • Indeed, the problem seems to be pretty similar. However, on my side, I do have $z$ and $kz$ (which are $a$ and $b$), but not $m$ nor $n$. Comparing to that post, I do not have an angle $α$ either that can be used for all the OCR photos, for some signs are skewed more than others... – Peter Jul 11 '19 at 22:24
  • 1
    You can use $x$ and $y$ to identify the rotation angle. The more the sign is rotated, the squarer the bounding box is. It still doesn't seem you are asking the right question-it seems you should be able to detect the intersection points in the image and measure $w,z$. If the sign is tilted with respect to the line of sight its image will not be a rectangle and calculations based on that will not work. – Ross Millikan Jul 11 '19 at 22:33
  • After messing around with the proportions, it seems like your recommendation ("You can use x and y to identify the rotation angle") helped me getting the coordinates I needed. Marking your answer as a solution, but the solution is in the comments :) Thank you! – Peter Jul 12 '19 at 16:51
0

If you really do have a rectangle within a rectangle, you could approach the problem as follows ...

Let $\theta$ be the small angle between $a$ and $x$ in your diagram.

I think that the ratio $r\equiv \frac xy$ is sufficient to calculate the angle $\theta$ , which is then sufficient to calculate the conversion factor between pixels and millimeters.

e.g. if we let $x_p$ represent the known length in pixels, then $x_p=cx$ ( $c$ has units of pixels per millimeter. )

In what follows, $x,y,a,b$ are all in millimeters

$$ \begin{eqnarray} x &=& b \sin \theta+a \cos \theta \\ y &=& a \sin \theta+b \cos \theta \\ \implies r= \frac xy &=& \frac{b +a\tan \theta}{a+b \tan \theta} \end{eqnarray} $$ which can be solved for $\theta$ ... $$ \tan \theta=\frac{ b-ra}{rb-a } $$ This can now be used to calculate $c$ ... $$c=\frac{x_p}{x}=\frac{x_p}{b \sin \theta + a \cos \theta} $$

Now your co-ordinates (in pixels) are things like $ac \cos \theta $ and $bc \sin \theta $

WW1
  • 10,497