Welcome to DOFBOT-Ultra repository
2. Image Reading and Display
Image Reading and Display
1. Image Reading:
img = cv2.imread('yahboom.jpg', 0) The first parameter is the path to the image, and the second parameter specifies how to read the image.
cv2.IMREAD_UNCHANGED: Keep the original format unchanged, -1;
cv2.IMREAD_GRAYSCALE: Read the image in grayscale mode, can be represented by 0;
cv2.IMREAD_COLOR: Read a color image, can be represented by 1; default value
cv2.IMREAD_UNCHANGED: Read an image including its alpha channel, can be represented by 2.
2. Image Display
cv.imshow('frame', frame): Opens a window named 'frame' and displays the frame data (image/video data)
Parameter meanings:
The first parameter specifies the name of the window to be created and opened
The second parameter specifies the image to be displayed
2.1. Code and Actual Effect Display
Code path:
~/dofbot_opencv/scripts/1.OpenCV_basic/01_read_pic.ipynbMain code:
xxxxxxxxxximport cv2img = cv2.imread('yahboom.jpg', 1)# cv2.imshow('image', img) # This line can only be executed from a py file in command line, it will pop up a video window# cv2.waitKey (0)xxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])xxxxxxxxxx# Display the read image using image widgets in JupyterLabimport ipywidgets.widgets as widgetsimage_widget = widgets.Image(format='jpg', width=800, height=800)display(image_widget)image_widget.value = bgr8_to_jpeg(img)After running the code block, you can see the interface below showing the image has been read.
