This article mainly introduces how to use Python and OpenCV libraries to convert URLs into OpenCV formats. at the same time, NumPy and urllib are used. For more information, see
Today's blog is directly from my own tool library.
In the past few months, some PyImageSearch readers emailed me: "How to get the image pointed to by the URL and convert it to OpenCV format (you do not need to write it to the disk and then read it back )". This article will show you how to implement this function.
Additionally, we will also see how to use scikit-image to download an image from a URL. Of course, there will also be a common mistake on the way forward, which may lead you down.
Continue reading and learn how to use Python and OpenCV to convert URLs to images
Method 1: OpenCV, NumPy, urllib
First method: We use OpenCV, NumPy, and urllib libraries to obtain images from URLs and convert them to images. Open and create a new file named url_to_image.py. let's start:
# import the necessary packagesimport numpy as npimport urllibimport cv2 # METHOD #1: OpenCV, NumPy, and urllibdef url_to_image(url): # download the image, convert it to a NumPy array, and then read # it into OpenCV format resp = urllib.urlopen(url) image = np.asarray(bytearray(resp.read()), dtype="uint8") image = cv2.imdecode(image, cv2.IMREAD_COLOR) # return the image return image
The first thing we need to do is import the necessary packages. We will use NumPy to convert the downloaded byte order to the NumPy array, use urllib to execute the actual network request, and use cv2 to bind the OpenCV interface.
In row 3, we define our url_to_image function. This function includes a url parameter, that is, the image address we want to download.
Next, we will use the urllib library to open the image link in row 10th. Line 11 converts the downloaded byte order to the NumPy array.
At this point, the NumPy array is still a one-dimensional array (that is, a long pixel linked list ). To convert it to a 2-dimensional format, we use the cv. imdecode function in 12 rows, assuming three channels per pixel (I .e. red, green, and blue channels respectively. Finally, in the 15 lines, we return the decoded image to the call function.
Everything is ready. it's time to let it work:
# initialize the list of image URLs to downloadurls = [ "http://www.pyimagesearch.com/wp-content/uploads/2015/01/opencv_logo.png", "http://www.pyimagesearch.com/wp-content/uploads/2015/01/google_logo.png", "http://www.pyimagesearch.com/wp-content/uploads/2014/12/adrian_face_detection_sidebar.png",] # loop over the image URLsfor url in urls: # download the image URL and display it print "downloading %s" % (url) image = url_to_image(url) cv2.imshow("Image", image) cv2.waitKey(0)
Lines 3-5 define the list of image addresses to be downloaded and converted to OpenCV format.
Row 3 traverses this list, and row 13 calls the url_to_image function, and then the images obtained from lines 14 and 15 are displayed on the screen. Now, we can use OpenCV to operate and process these images, just as we normally do.
When you open the terminal, run the following command:
The code is as follows:
$ Python url_to_image.py
If everything goes well, you will see the OpenCV logo:
: Download the OpenCV logo from the URL and convert it to the OpenCV format
Next is Google's logo:
: Download Gooogle from URL and convert it to OpenCV format
Here is also an example of verifying face detection in my book, Practical Python and OpenCV:
: Convert a URL image to OpenCV format
Now, let's look at another method to get the image and convert it to OpenCV format.
Method 2: Use scikit-image
The second method assumes that you have installed the scikit-image library on your computer. Let's take a look at how to use scikit-image to get an image from the URL and convert it to the OpenCV format:
# METHOD #2: scikit-imagefrom skimage import io # loop over the image URLsfor url in urls: # download the image using scikit-image print "downloading %s" % (url) image = io.imread(url) cv2.imshow("Incorrect", image) cv2.imshow("Correct", cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) cv2.waitKey(0)
The nice part of the scikit-image library is that the imread function in the io sub-Library can distinguish whether the image path is on a disk or a URL (line 1 ).
Despite this, there is a very serious error that may lead you down!
OpenCV expresses an image in BGR order, while scikit-image is in RGB order. If you use the scikit-iamge imread function and want to use the OpenCV function after the download is complete, be careful. As described in line 41, you need to convert the image from RBG to BGR.
If you do not have this step, you may get the wrong result:
: When using scikit-image, pay special attention to converting RGB to BGR. The image on the left is an incorrect RGB sequence, and the right is converted to BGR, so it can be displayed normally.
Look at Google's logo.
: Order is important. Make sure to convert RGB to BGR; otherwise, a bug is hard to be found.
So far, you understand it! These two methods use Python, OpenCV, urllib, and scikit-image to convert the image pointed to by the URL to an image.
Summary
In this article, we learned how to get images from URLs and convert them to OpenCV formats using Python and OpenCV.
The first method is to use the urllib package to obtain the image, use Numpy to convert it to an array, and then use OpenCV to recreate the array to generate our image.
The second method is to use the io. imread function in scikit-image.
So what is better?
It depends entirely on your installation.
If you have installed scikit-image, I may use io. imread (just don't forget to convert RGB to BGR if you want to use the OpenCV function ).
If you have not installed scikit-image, url_to_image is a ready-to-use tool. For details, refer to the beginning of this article.
I will soon add this function to the imutils Library on Github.