Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert fastai image from open_image() format to opencv?

I have image opened this way in fast.ai:

 img = open_image(img_f)

How do I show it with cv2.imshow ?

like image 936
Stepan Yakovenko Avatar asked May 07 '19 17:05

Stepan Yakovenko


People also ask

How to convert a Python image to an OpenCV image?

The first method is to use the urllib Python package to download the image, convert it to an array using NumPy, and finally reshape the array using OpenCV to construct our image. The second method is to use the io.imread function of scikit-image. So which method is better?

What is a fastai image?

The fastai Image classes The fastai library is built such that the pictures loaded are wrapped in an Image. This Image contains the array of pixels associated to the picture, but also has a lot of built-in functions that will help the fastai library to process transformations applied to the corresponding image.

How to read an image from an OpenCV sample?

Now, let's analyze the main code. As a first step, we read the image "starry_night.jpg" from the OpenCV samples. In order to do so, a call to the cv::imread function loads the image using the file path specified by the first argument. The second argument is optional and specifies the format in which we want the image.

How to convert PIL image to OpenCV image in transparent channel?

While convert PIL img to OpenCV img will able to keep transparent channel, although cv2.imshow not display it but save as png will gave result normally. You can call cv2.imwrite ("./"+"fileName.png", img); to export and check the transparent result of OpenCV img.


1 Answers

FastAi already uses this package to work with images. When you do open_image function check the doc string:

def open_image(fn):
    """ Opens an image using OpenCV given the file path.
like image 177
prosti Avatar answered Oct 19 '22 21:10

prosti