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)
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 ...)
・ 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
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.
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 the image on Jupyter Notebook. This time, the image of chipmunk.
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
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!
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.
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)
-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