# Find a small square patch inside an "approxPolyDP" polygon

Hi,

I'm processing a still image in order to find where the water is. (see my other question here)

Now, I can find all the rather large polygons in the image, using approxPolyDP. But there is no guarantee that the water is the dominant part of the image.

In case I have a few large-ish polygons (> 5%) of the image), I'd like to find a small patch that fits in my polygon and test its texture using one of the classic statistical texture methods (local binary pattern, laws, gabor filters).

But I'm not sure how to find a small square that fits inside the polygon. The only thing I can think of is to use the center of gravity, and create a small box around it. But in the case of a complicated shape, the center of gravity might not be in the shape itself.

Here is an example where finding a small square patch in the polygon that I have outlined seems potentially tricky:

Original image:

Outline of the water (one of the large polygons), where I'd like to define a square patch :

edit retag close merge delete

Sort by » oldest newest most voted

Filter your mask with a kernel having the size you want to find and then take the maximum response, if this response is not the size of the kernel, there doesn't exist a large enough patch, otherwise you can use its location for a suitable patch.

Code:

import cv2
import numpy as np

# make sure the masked part is 1 (not 255)

# make a window with the minimum window size to find
min_s = 16
kernel = np.ones((min_s, min_s),dtype=np.uint8)
# filter the image with this kernel

# find the maximum
m = np.amax(res)
# if the maximum is the area of your window at least
# one location has been found
if m == min_s*min_s:
# show each location where we found the maximum
# i.e. a possible location for a patch

# if you are only interested in the first hit use this:
# find firs index and convert it to coordinates of the mask
# could do now other things with this location,
# but for now just show it in another color



Result (green: all possible locations, blue the first one):

more

My next idea was to apply a distance transform on the masked version, then threshold to get a local max. This should get me a patch towards the center of the texture. I imagine the distance transform is more expensive ?

( 2013-12-23 03:06:32 -0500 )edit

Good idea. Speed-wise it is hard to say what is faster, convolution depends on the kernel size you chose, e.g. if it is a larger one, FFT is used for speed up. On the other hand, the speed of the distance transform depends the most on the chosen distance metric. I guess, if you chose l1, then this will be faster.

( 2013-12-23 03:52:42 -0500 )edit

Official site

GitHub

Wiki

Documentation