Ways to convert URLs to OPENCV format using Python and OPENCV libraries

Source: Internet
Author: User

Today's blog is a direct source of my own personal tool function library.

In the past few months, some Pyimagesearch readers emailed me: "How to get a picture that the URL points to and convert it to OPENCV format (without having to write it to disk and then read back)". In this article I will show you how to implement this function.

In addition, we will also see how to use Scikit-image to download an image from a URL. Of course the road ahead will also have a common mistake, it may let you fall a somersault.

Read on to learn how to use Python and OpenCV to convert URLs to images


Methods 1:opencv, NumPy, Urllib

The first method: we use the OpenCV, NumPy, urllib libraries to get the image from the URL and convert it to an image. Open and create a new file, named url_to_image.py, let's get started:

# 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 = ur Llib.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 to do is to import the packages we need. We will use the NumPy conversion to download the byte order as an NumPy array, use Urllib to perform the actual network request, and use CV2 to bind the OpenCV interface.
In line 7th, we define our url_to_image function. This function takes a URL parameter, which is the image address we want to download.

Next, in line 10th, we use the Urllib library to open the image link. 11 lines convert the downloaded byte order to the NumPy array.

At this point, the NumPy array is also a 1-dimensional array (that is, a long, long list of pixels). In order to convert it to a 2-D format, assume that each pixel has 3 channels (meaning red, green, and blue channels, respectively), and in 12 rows we use the Cv.imdecode function. Finally, in line 15 we return the decoded image to the calling function.

It's time to make 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) C6/>cv2.imshow ("image", image)  Cv2.waitkey (0)

Line 3-5 defines the list of image addresses we will download and convert to OPENCV format.

Line 9th We iterate through the list, and 13 rows call the Url_to_image function, and then the images captured in rows 14 and 15 are displayed on the screen. In this case, we can use OPENCV to manipulate and manipulate the images as normal.

Seeing is believing, open the terminal and execute the following command:
Copy the Code code as follows:

$ python url_to_image.py


If all goes well, you'll see OpenCV's logo:

Figure 1: Download OpenCV logo from URL and convert to OPENCV format


Next is Google's logo:

Figure 2: Download Gooogle from URL and convert to OPENCV format


Here are also examples of verifying face detection in my book, "Practical Python and OpenCV":

Figure 3: Convert a URL image to OPENCV format

Now let's look at another way to get an image and convert it to the OPENCV format.


Method 2: Use Scikit-image

The second method assumes that you have installed the Scikit-image library on your computer. Let's see how to use Scikit-image to get an image from a 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 thing about the Scikit-image library is that the Imread function in the IO sub-Library distinguishes between whether the image path is on disk or a URL (line 9th).

In spite of this, there is a serious mistake here that could make you fall a somersault!

OpenCV expresses an image in BGR order, whereas Scikit-image is the RGB order. If you use Scikit-iamge's 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, then you may get the wrong result:

Figure 4: When using scikit-image, you need to pay special attention to converting RGB to BGR. The image on the left is an incorrect RGB order, and the right side is the RGB conversion to BGR, so it can be displayed normally.

Looking at Google's logo is even more obvious.

Figure 5: Order is important. Make sure to convert RGB to BGR, or leave a hard-to-find bug.

That's it, you got it! The two methods use Python, OpenCV, Urllib, and Scikit-image, respectively, to convert the picture that the URL points to to an image.


Summarize

In this article, we learned how to get images from URLs and convert them to OPENCV format using Python and OpenCV.

The first method uses the Urllib package to get the image, uses the NumPy to convert the array, and finally uses OPENCV to reconstruct the arrays to produce our images.

The second method uses the Io.imread function in Scikit-image.

So, which is better?

It all depends on your installation.

If you have installed Scikit-image, then I may use io.imread (just don't forget to convert RGB to BGR If you want to use the OPENCV function).

If you don't have Scikit-image installed, then Url_to_image is a handy tool. Refer to the beginning of this article for specific details.

I will soon add this function to the Imutils library on GitHub.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.