User avatar
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 569
Joined: Sat Nov 02, 2013 2:06 pm

Updating python-skimage

Wed May 30, 2018 9:00 pm


Whilst following a document scanner guide by Adrian Rosebrock I have encountered some issues with a python package python-skimage.
One of the modules associated with python-skimage. The script calls for the import of threshold_local which cannot be found.

On further investigation it seems that the version of python-skimage installed is an older version and does not contain threshold_local.
I have tried to install the latest version of python-skimage using:

Code: Select all

sudo apt-get install python-skimage
However the problem exists.

I have managed to work around this by modifying the two files containing threshold_local but this is a hack.

Can anyone hep me find out why I cannot update this package?

I am using Python2

When running the code below I received an import error:

Code: Select all

[email protected]:~/pyimagesearch $ python --image images/receipt.jpg Traceback (most recent call last): File "", line 6, in <module> from skimage.filters import threshold_local ImportError: cannot import name threshold_local

Code: Select all

# python --image images/page.jpg

# import the necessary packages
from transform import four_point_transform
from skimage.filters import threshold_local
import numpy as np
import argparse
import cv2
import imutils

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
	help = "Path to the image to be scanned")
args = vars(ap.parse_args())

# load the image and compute the ratio of the old height
# to the new height, clone it, and resize it
image = cv2.imread(args["image"])
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)

# convert the image to grayscale, blur it, and find edges
# in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)

# show the original image and the edge detected image
print("STEP 1: Edge Detection")
cv2.imshow("Image", image)
cv2.imshow("Edged", edged)

# find the contours in the edged image, keeping only the
# largest ones, and initialize the screen contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

# loop over the contours
for c in cnts:
	# approximate the contour
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, 0.02 * peri, True)

	# if our approximated contour has four points, then we
	# can assume that we have found our screen
	if len(approx) == 4:
		screenCnt = approx

# show the contour (outline) of the piece of paper
print("STEP 2: Find contours of paper")
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Outline", image)

# apply the four point transform to obtain a top-down
# view of the original image
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)

# convert the warped image to grayscale, then threshold it
# to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
T = threshold_local(warped, 11, offset = 10, method = "gaussian")
warped = (warped > T).astype("uint8") * 255

# show the original and scanned images
print("STEP 3: Apply perspective transform")
cv2.imshow("Original", imutils.resize(orig, height = 650))
cv2.imshow("Scanned", imutils.resize(warped, height = 650))
I only came across this issue whilst learning OpenCV following a tutorial. Whilst running the first script, which I have listed above, I encountered the error that threshold_local was not available. This led me to my research scikit-image.

The version of python-skimage installed appears to be 0.12.3-8. I have followed the installation guide in the documentation here:
I have run sudo apt-get install python-skimage on my Raspberry Pi but the version of python-skimage does not change?
Any help would be appreciated
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

Posts: 11
Joined: Mon Jun 22, 2015 6:51 pm

Re: Updating python-skimage

Thu May 31, 2018 10:49 am

You can always use pip to install modules, regardless of the availability of Raspian sources and version.

One suggestion if you want to keep an updated OS, but have different versions of modules available for development is to use virtualenv for python scripts. You can create an environment for each project and install versions of modules using pip for each instance of a virtual env depending on needs. That way the OS has access to versions in the stable OS repository, but scripts have access to versions of modules (installed with pip) that might not be included in OS updates. ... envwrapper

Return to “Python”