# Mapping Camera Coordinates to a 2D Floor Plan

**URL:** <https://discourse.processing.org/t/mapping-camera-coordinates-to-a-2d-floor-plan/16539>\
**Category:** Libraries\
**Created:** [December 20, 2019, 4:06pm UTC](https://discourse.processing.org/t/mapping-camera-coordinates-to-a-2d-floor-plan/16539 "2019-12-20T16:06:32Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 20, 2019, 8:28pm UTC](https://discourse.processing.org/t/mapping-camera-coordinates-to-a-2d-floor-plan/16539/4 "2019-12-20T20:28:07Z")

</div>

Great! There are many ways of tackling this problem – in OpenCV for Processing, or BoofCV, or using an existing third party library, or rolling your own.

### 1. Perspective

If you want the simplest way, just preprocess your image with WarpPerspective. Note that in openCV, warpPerspective is the same as projective transformation / homography – it is a transform of any quad into any other quad.

- [WarpPerspective.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/WarpPerspective/WarpPerspective.pde) … you can also check out the MarkerDetection example
  - OpenCV docs: [warpPerspective](https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#warpperspective)
  - optionally, further past discussion of [using warpPrespective directly](https://forum.processing.org/two/discussion/26107/opencv-perspective-transformation-and-region-of-interest)

Now you can run openCV on a 2D representation of the image data, and the coordinates of what you detect will automatically be in the floor plan space.

You can also do this using BoofCV with RemovePerspective (“Given 4 corners of a shape which is known to be rectangular, remove the perspective distortion from it”):

- BoofCV for Processing [RemovePerspective.pde](https://github.com/lessthanoptimal/BoofProcessing/blob/master/examples/RemovePerspective/RemovePerspective.pde)
  - BoofCV docs: [Remove Perspective Distortion](https://boofcv.org/index.php?title=Example_Remove_Perspective_Distortion) | [RemovePerspectiveDistortion](http://boofcv.org/javadoc/boofcv/alg/distort/RemovePerspectiveDistortion.html)

### 2. Compute points for homography

If, on the other hand, you want to run detection on the raw uncorrected feed and then convert points to floor plan space after the fact, then you want to create a projective homography between quad A and B, and use point-in, point-out.

In OpenCV for Processing, creating the homography itself is accessed through

```auto
import org.opencv.calib3d.Calib3d;
Calib3d.findHomography( ... )

```

For discussion of the implementation, see [https://stackoverflow.com/questions/27972703/java-findhomography-transform-image-point-to-ground-plane-point](https://stackoverflow.com/questions/27972703/java-findhomography-transform-image-point-to-ground-plane-point)

But if you just want a pre-written implementation that should work, download and install the Laserschein library (untested!), and import the two classes Homography and HomographyMatrix. A Homography object will give you 4 + 4 points for a matrix, and then a point and inverse point mapping method – exactly what you want, all done with PVectors.

> **[allesblinkt/Laserschein](https://github.com/allesblinkt/Laserschein/)**
>
> A Processing library for controlling laser projectors (ILDA) - allesblinkt/Laserschein

- [https://github.com/allesblinkt/Laserschein/blob/master/src/laserschein/Homography.java](https://github.com/allesblinkt/Laserschein/blob/master/src/laserschein/Homography.java)

It might also be worth checking out the Keystone Library for Processing.

- [https://github.com/davidbouchard/keystone](https://github.com/davidbouchard/keystone)
  - [Urgent help using Keystone Library](https://discourse.processing.org/t/urgent-help-using-keystone-library/2758/7)

* * *

See also related pasts discussions:

- [Tracking user location in a physical grid using a webcam?](https://discourse.processing.org/t/tracking-user-location-in-a-physical-grid-using-a-webcam/14607)
- [Planar equation from camera image](https://discourse.processing.org/t/planar-equation-from-camera-image/8164)

---

_[View the full topic](https://discourse.processing.org/t/mapping-camera-coordinates-to-a-2d-floor-plan/16539)._
