As I test scikit-image
methods ,I came across skimage.measure.perimeter(image)
but couldn't explain the output of this function.
import numpy as np
image=np.zeros((100,100))
image[10:30,10:30]=1 # this creates a white square
from skimage.measure import perimeter
x=perimeter(image)
print x #Should be (20+20+20+20) = 80
76.0 <<<<<<<<<< it returns this value
Did I misunderstand what this function should return . I know that perimeter is a path that surrounds an area .
Note:-
(1) The difference between the calculated perimeter and the returned perimeter not always by 4.as sometimes it can be 6 no matter it's a square a rectangle or any other polygon.
Update:=
(1) The function page
Which version of skimage are you using ? Version 0.6 and github sources do not have a skimage.measure.perimeter function.
Additionaly, I think you meant line 3 : image[10:30, 10:30] = 1
Edit
Ok, I think I got it. The function is defined in _regionprops.py since version 0.7.1. The result 76 is actually right. It computes the perimeter of a white square of shape (20, 20).
You think each side accounts for 20 pixels which total to 80 pixels. But by doing so you count twice the corner pixels. Removing corner pixels you end-up with a perimeter of 76 pixels.
For other shapes the difference may not be of 4 pixels.
Edit 2
Looking at the source code and the documentation:
Perimeter of object which approximates the contour as a line through the centers of border pixels using a 4-connectivity.
The code computes a border_image which consist of the image minus its erosion. This array corresponds to the idea of the perimeter I had in the first edit.
It then computes the perimeter by applying a convolution and a weighted sum to the border image. I think this is done to compute the length of the line going through the centers of border pixels as stated in the doc.
If you want more details, you should ask one of the dev. This is a very new function in the package.