2

Ok, so I have an image generated by a numpy array of values with arbitrary units. The image are showing a rotated square with a hole in it. What I am trying to do is to write some code to find the edges of this square and then save the values inside the square in one array and the values outside the square in one array. And the values in the hole should be excluded. This whole procedure I want to use for a lot of images but all with the same square in them.

I do not really know how to approach this but have been thinking about making loops trying to find where the values change fast (i.e being an edge) but that is complicated by that the units are arbitrary so the change in values would be different for every single image.

I tried to use canny plot without any luck since it only discovered the really sharp edges in another part of the figure.

So basically I need some ideas of the approach, if a lot of loops are the way to go or if I should try to find some other way.

So this is the image: Rotated square And this is showing the values for one y value: One y value, all x values

5
  • Definitely try with loops first, which would act as the baseline if that proves to be slow and you might look to vectorize thereafter. Commented Oct 15, 2015 at 17:56
  • A sample image or two would be a big help. Commented Oct 15, 2015 at 17:58
  • @WarrenWeckesser I added two images now. Commented Oct 15, 2015 at 18:06
  • I suggest beginning with the Sobel edge detection algorithm. It is the simplest to implement and leaves the most image intact, as the masks are only 3x3 matrices. Commented Oct 15, 2015 at 18:12
  • Can you upload the raw image? Without the figure axes. Commented Oct 16, 2015 at 8:53

1 Answer 1

2

A good way to do edge detection for images is by using a Sobel Filter. Using the following package.

import numpy as np
import scipy.ndimage

...

#If your image is a 2D numpy array named "img", then do the following

img_sobel = scipy.ndimage.filters.sobel(img)

This produces another image that is filtered, and enhances the edges of the image. It essentially produces an image gradient where the largest gradient can be found at the edges of your image. Next, you can do some binary filtering to make the edges show up better. Basically, you want to set values higher than half the maximum sobel filtered value equal to 1 and values with less than half equal to zero like so (I've cleaned up the code based on imaluengo's suggestion):

bn_img = np.zeros([img_sobel.shape[0],img_sobel.shape[1]])
sbl_max = np.amax(abs(img_sobel))
bn_img = np.abs(img_sobel) >= (sbl_max/2.0) #You can change 
                                            #this threshold 
                                            #to suit your needs

Below is an example image of how I used this to find the edge of a lunar image, so that the center of the moon can be found even with the dark side of the moon cannot be seen.

Lunar Image using Sobel Filter

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.