[PYTHON] About Opencv ①

Notes about Opencv3 series

Basically, it is a thing while checking the official document.

environment

windows10 home 64bit Python 3.7.9 opencv 3.4.2.17 Jupyter Notebook used (Anaconda not used)


About the image used

For the image, I used the meat I took. Use .png as the format. .jpg is fine

opencv_test.ipynb


cap_dir = "Specify the folder where the images used are saved"

From now on, this variable is used.

① About image reading

  1. cv2.imread (1st argument, 2nd argument)

opencv_test.ipynb


img1 = cv2.imread(cap_dir,0)
img2 = cv2.imread(cap_dir,1)
img3 = cv2.imread(cap_dir,-1)
img4 = cv2.imread(cap_dir,cv2.IMREAD_GRAYSCALE)
img5 = cv2.imread(cap_dir,cv2.IMREAD_COLOR)
img6 = cv2.imread(cap_dir,cv2.IMREAD_UNCHANGED)

1st argument: Image you want to load Second argument: Flags for images

If you just use it normally, 0 or 1 is fine. I think it's okay to use it in color or gray because it is what to do in the processing after reading.

Rather, I've never seen an alpha channel. I do not understand even if I look at the image.

スクリーンショット (333).pngスクリーンショット(334).pngスクリーンショット(335).png


② About image display

  1. cv2.imshow (1st argument, 2nd argument)
  2. cv2.waitKey (first argument)
  3. cv2.destroyAllWindows (1st argument)
  4. cv2.namedWindow (1st argument, 2nd argument)

opencv_test.ipynb


img = cv2.imread(cap_dir,1)

cv2.namedWindow('niku',cv2.WINDOW_NORMAL) 
cv2.imshow('niku',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  1. cv2.imshow (1st argument, 2nd argument) 1st argument: Display window name (input as character type) Second argument: The image you want to display

  2. cv2.waitKey (first argument) First argument: Keyboard input wait time (ms unit) I thought it was a weight, but it seems to be the official input reception time If you set it to 0, you will be waiting for unlimited input.

  3. cv2.destroyAllWindows (1st argument) 1st argument: Close all open windows if not entered. If you enter the name of the window you want to close when multiple windows are open Close the window with the name you entered

  4. cv2.namedWindow (1st argument, 2nd argument) 1st argument: Window name (character type) Second argument: cv2.WINDOW_AUTOSIZE is set by default. Normally opened images have a fixed window size If you enter cv2.WINDOW_NORMAL in the second argument The size can be changed arbitrarily by operating the mouse.

However, as a caveat, if the first argument is different from the name set in the first argument of imshow Another window without an image will be launched and it will be meaningless.

The following is when the names do not match The size of the window can be changed on the left side スクリーンショット (338).png


③ About saving images

  1. cv2.imwrite (1st argument, 2nd argument)

opencv_test.ipynb


cv2.imwrite('test.png',img)

1st argument: Saved image name Second argument: Saved image

Now when you save the image, it will be saved in the same place where the script is.

opencv_test.ipynb


save_dir = "Directory where you want to save images"
save_name = "Image name"
save_ext = ".png "

cv2.imwrite((save_dir + save_name + save_ext),img)

Then you can save the image in the specified location.

Perhaps the above usage will be the default.


④ Summary

Other things will be put together one by one.

Recommended Posts

About Opencv ③
About Opencv ①
About LangID
About CAGR
About virtiofs
About python-apt
About Permission
About sklearn.preprocessing.Imputer
About gunicorn
About requirements.txt
About locale
About permissions
About axis = 0, axis = 1
About numpy
About pip
About numpy.newaxis
About endian
About Linux
About import
About Linux
About Linux
About Linux ①
About cv2.imread
About _ and __
About wxPython
Adjust OpenCV contrast
Notepad about TecoGAN
About python slices
Briefly about __name__
[OpenCV] Personal memo
About python comprehension
About Docker Volume
[Linux] About export
About reference type
About Twitter scraping
About the test
I tried'Beauty'with OpenCV
Learn about programming
About Flask customization
Python2.7 + CentOS7 + OpenCV3
About variable scope. .. ..
About Python tqdm.
Notes about with
About python, class
About Linear Models
About Go functions
OpenCV3 installation battle
About pandas describe
About Kivy root
About Firestore timeout
About python inheritance
About python, range ()
About Confusion Matrix
[Linux] About PATH
Linux (about groups)
OpenCV Samples (Python)
[Note] openCV + python
About python reference
About Bitnami Autostart
About Python decorators
About Milkcocoa SDK