[PYTHON] I want to display an image on Jupyter Notebook using OpenCV (mac)

Image analysis for python beginners

Record what you failed and what you could improve from a beginner's point of view.

This time, on the Jupyter Notebook launched from Anaconda Contents that caused an error when displaying an image using OpenCV. (The image below is a successful example) jupyternotebook_shimarisu.png

Also, when the image quality was displayed, the resolution had dropped, so I'm looking for ways to improve it. (If anyone knows, please teach me ...)

conditions

・ Using macOS ・ I want to display it on Jupyter Notebook on Anaconda -Use Python 3.7 -Use OpenCV 4.1.1 -Use matplotlib -Images (shimarisu.jpg) are stored in a folder on the desktop of mac.  /Desktop/detect_ai/data/shimarisu.jpg

OpenCV installation

Since only OpenCV was not installed in Anaconda https://rightcode.co.jp/blog/information-technology/opencv-jupyternotebook-face-recognition I tried it with reference to, but for some reason I couldn't. You can select OpenCV and press the Apply button to start the installation I cannot press the Apply button after the installation is complete.

So, enter `$ pip install opencv-python` in the terminal and execute. スクリーンショット 2019-11-17 23.46.58.png In yellow letters, it is displayed as WARNING, Since it is "Successfully installed opencv-python-4.1.1.26" The installation seems to be successful.

Display images using OpenCV

Display the image on Jupyter Notebook. This time, the image of chipmunk.

Source code

I referred to this. https://teratail.com/questions/169009

opencv.ipynb


import matplotlib.pyplot as plt
import numpy as np
import cv2

#Inline display
%matplotlib inline

#image(shimarisu.jpg)Loading
img = cv2.imread("shimarisu.jpg ")

#Image display
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) #OpenCV colors are in GBR order, so sort them in RGB order
plt.show()

However, I got the following error.

opencv.ipynb


error                                     Traceback (most recent call last)
<ipython-input-33-7c8a2c33eec7> in <module>
     10 
     11 #Image display
---> 12 plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) #OpenCV colors are in GBR order, so sort them in RGB order
     13 plt.show()

error: OpenCV(4.1.1) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

◆ After investigating the cause, the location of the python file that is currently up (on Anaconda) and The location (on Desktop) of the image (shimarisu.jpg) you want to display is different. https://ja.stackoverflow.com/questions/49260/python3-6-6%E3%81%AB%E3%81%A6cv2%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%8C%E7%99%BA%E7%94%9F%E3%81%97%E3%81%BE%E3%81%99

The image I wanted to display (shimarisu.jpg) was stored in a folder on the desktop of the mac. /Desktop/detect_ai/data/shimarisu.jpg

What I tried

In the 5th line of the 1st image, the reading part of the image (shimarisu.jpg)

img = cv2.imread ("shimarisu.jpg ") img = cv2.imread("/Users/name/Desktop/detect_ai/data/shimarisu.jpg ")

After replacing it with, the image was displayed!

Points to note

It is necessary to align the location of the python file that is up and the hierarchy of the storage location of the image. It is necessary to describe "The image (shimarisu.jpg) is stored in the folder on the desktop of mac". In this case /Users/(name displayed in the terminal) /Desktop/detect_ai/data/shimarisu.jpg

(The name displayed in the terminal) varies from person to person. The image below is just described as "name" as an example, so please change it at any time.

jupyternotebook_shimarisu.png

problem

However, the resolution of the image quality will be reduced and displayed, so keep the resolution of the original image. We are currently verifying whether the image can be displayed. (I will update if there is progress)

Other reference sites

-Source code for displaying images with OpenCV http://peaceandhilightandpython.hatenablog.com/entry/2015/12/23/214840 http://rasp.hateblo.jp/entry/2016/01/22/201534

Recommended Posts

I want to display an image on Jupyter Notebook using OpenCV (mac)
I want to use Linux on mac
[Pythonocc] I tried using CAD on jupyter notebook
Library for "I want to do that" of data science on Jupyter Notebook
I want to convert an image to WebP with lollipop
I tried to become an Ann Man using OpenCV
I want to AWS Lambda with Python on Mac!
[Memo] Display Jupyter Notebook on PC in monospaced font (Mac)
I want to announce my graduation thesis on IPython Notebook
I want to use a virtual environment with jupyter notebook!
Unable to display tensorboard in jupyter notebook on docker (solved)
How to install OpenCV on Mac
Try running Jupyter Notebook on Mac
Somehow I tried using jupyter notebook
I want to know if you install Python on Mac ・ Iroha
I tried to digitize the stamp stamped on paper using OpenCV
I tried to visualize BigQuery data using Jupyter Lab on GCP
I tried to display GUI on Mac with X Window System
Install Anaconda on Mac and upload Jupyter (IPython) notebook to Anaconda Cloud
I tried to make an image similarity function with Python + OpenCV
Install opencv on Mac using Anaconda Navigator
Display histogram / scatter plot on Jupyter Notebook
I want to display the progress bar
I want to make an automation program!
Video processing using Python + OpenCV on Mac
I want to develop Android apps on Android
I want to operate DB using Django's ORM from an external application
Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)
For the time being using FastAPI, I want to display how to use API like that on swagger
Install matplotlib and display graph on Jupyter Notebook
[Jupyter Notebook / Lab] 3 ways to debug on Jupyter [Pdb]
I tried to get an image by scraping
How to build an environment for using multiple versions of Python on Mac
I want to email from Gmail using Python.
I want to display multiple images with matplotlib.
I want to visualize csv files using Vega-Lite!
How to crop an image with Python + OpenCV
I've built an environment to try using TensorFlow on mac, so my memo
I want to be an OREMO with setParam!
Simply display a line graph on Jupyter Notebook
I made an image classification model and tried to move it on mobile
I want to crop the image along the contour instead of the rectangle [python OpenCV]
I want to do pyenv + pipenv on Windows
[I want to classify images using Tensorflow] (2) Let's classify images
When I try to use Jupiter notebook on Mac, I can only select python2
I want to log file I / O on Linux
I tried using the image filter of OpenCV
I want to display the progress in Python!
jupyter notebook does not start on mac fish
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
I want to collect a lot of images, so I tried using "google image download"
I want to take a screenshot of the site on Docker using any font
I get an error when I try to raise Python to 3 series using pyenv on Catalina
I want to use Mac / emacs key bindings (keyboard shortcuts) on Ubuntu xkeysnail recommendations
Build an OpenCV4 environment on Raspberry Pi using Poetry
100 image processing knocks !! (021-030) I want to take a break ...
I want to find a popular package on PyPi
Mac application for double-clicking to open Jupyter Notebook (* .ipynb)
I tried to get an AMI using AWS Lambda
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
I tried using PySpark from Jupyter 4.x on EMR