- Mastering OpenCV 4 with Python
- Alberto Fernández Villán
- 414字
- 2021-07-02 12:07:16
Reading and writing images in OpenCV
A common approach is to load an image, perform some kind of processing, and finally output this processed image (see Chapter 2, Image Basics in OpenCV, for a deeper explanation of these three steps). In this sense, the processed image can be saved to disk. In the following example, these three steps (load, processing, and save) are introduced. In this case, the processing step is very simple (convert the image into grayscale). This can be seen in the following example, argparse_load_processing_save_image.py:
# Import the required packages
import argparse
import cv2
# We first create the ArgumentParser object
# The created object 'parser' will have the necessary information
# to parse the command-line arguments into data types.
parser = argparse.ArgumentParser()
# Add 'path_image_input' argument using add_argument() including a help. The type is string (by default):
parser.add_argument("path_image_input", help="path to input image to be displayed")
# Add 'path_image_output' argument using add_argument() including a help. The type is string (by default):
parser.add_argument("path_image_output", help="path of the processed image to be saved")
# Parse the argument and store it in a dictionary:
args = vars(parser.parse_args())
# We can load the input image from disk:
image_input = cv2.imread(args["path_image_input"])
# Show the loaded image:
cv2.imshow("loaded image", image_input)
# Process the input image (convert it to grayscale):
gray_image = cv2.cvtColor(image_input, cv2.COLOR_BGR2GRAY)
# Show the processed image:
cv2.imshow("gray image", gray_image)
# Save the processed image to disk:
cv2.imwrite(args["path_image_output"], gray_image)
# Wait until a key is pressed:
cv2.waitKey(0)
# Destroy all windows:
cv2.destroyAllWindows()
In this previous example, there are two required arguments. The first one is path_image_input, which contains the path of the image we want to load. The path of the image is a string. Therefore, no type should be included in the positional argument because it is a string by default. The second one is path_image_output, which contains the path of the resulting image we want to save. In this example, the processing step consists of converting the loaded image into grayscale:
# Process the input image (convert it to grayscale)
gray_image = cv2.cvtColor(image_input, cv2.COLOR_BGR2GRAY)
It should be noted that the second argument, cv2.COLOR_BGR2GRAY, assumes that the loaded image is a BGR color image. If you have loaded an RGB color image and you want to convert it into grayscale, you should use cv2.COLOR_RGB2GRAY.
This is a very simple processing step, but it is included for the sake of simplicity. In future chapters, more elaborate processing algorithms will be shown.
- PHP 7底層設(shè)計與源碼實現(xiàn)
- 深入淺出DPDK
- 匯編語言程序設(shè)計(第3版)
- 深入理解Android:Wi-Fi、NFC和GPS卷
- Android程序設(shè)計基礎(chǔ)
- Linux C編程:一站式學(xué)習(xí)
- SQL Server實用教程(SQL Server 2008版)
- 詳解MATLAB圖形繪制技術(shù)
- Illustrator CC平面設(shè)計實戰(zhàn)從入門到精通(視頻自學(xué)全彩版)
- 進(jìn)入IT企業(yè)必讀的324個Java面試題
- Qt 4開發(fā)實踐
- JavaEE架構(gòu)與程序設(shè)計
- Scratch編程從入門到精通
- 微服務(wù)設(shè)計
- HTML 5與CSS 3權(quán)威指南(第4版·下冊)