c# OpenCvSharp reads, displays and writes images (2)

        Reading, displaying, and writing images are fundamental to image processing and computer vision. Even when you're cropping, resizing, rotating, or applying different filters to an image, you need to read the image first. Therefore, it is very important to master these basic operations.

  1. imread() reads images
  2. imshow() displays the image in the window
  3. imwrite() saves the image to the file directory

We'll use the image below to demonstrate all the functionality here.

1. imread() reads images

cv.imread(filename[, flags])

ImreadModes.Color: Always convert the image to a 3-channel BGR color image, the default way

ImreadModes.Grayscale: Always convert the image to a single channel grayscale image

ImreadModes.Unchanged: Returns the loaded image as-is (with alpha channel)

ImreadModes.AnyDepth: Returns a 16-bit/32-bit image when the input has corresponding depth, otherwise converts it to 8-bit

ImreadModes.AnyColor: Read images in any possible color format

Mat img1 = new Mat("1.jpg", ImreadModes.Color);
Cv2.ImShow("Color", img1);
Mat img2 = new Mat("1.jpg", ImreadModes.Grayscale);
Cv2.ImShow("Grayscale", img2);
Mat img3 = new Mat("1.jpg", ImreadModes.Unchanged);
Cv2.ImShow("Unchanged", img3);
Mat img4 = new Mat("1.jpg", ImreadModes.AnyColor);
Cv2.ImShow("AnyColor", img4);

2. imshow() displays the image in the window

Cv2.waitKey()is a function in OpenCV that waits for the specified number of milliseconds on the window and returns -1 if no key is pressed during the period. If any key is pressed, the ASCII code value of the key is returned. This function is typically cv2.imshow()used with a window to display an image and wait for the user to press a keyboard.

img = cv2.imread('image.jpg')
cv2.imshow('image', img)
k = cv2.waitKey(0) & 0xFF # 等待按键按下
if k == 27: # 如果按下ESC键
    cv2.destroyAllWindows() # 关闭所有窗口
elif k == ord('s'): # 如果按下's'键
    cv2.imwrite('image_copy.jpg', img) # 保存图像
    cv2.destroyAllWindows() # 关闭所有窗口

 In the above example, cv2.waitKey(0)it will wait until the user presses any key. If the user presses the ESC key, all windows will be closed. If the user presses the 's' key, the image will be saved as 'image_copy.jpg' and all windows will be closed.

3. ImWrite() writes the image to the file directory

imwrite(filename, image).

  1. The first parameter is the filename, which must include the file extension (e.g. .png, .jpg, etc.). OpenCV uses this file extension to specify the format of the file.
  2. The second parameter is the image to save. This function will return if the image is saved successfully.True
Mat src = Cv2.ImRead("lenna.png", ImreadModes.AnyColor);
Mat output_image = new Mat();
Cv2.CvtColor(src, output_image, ColorConversionCodes.BGR2GRAY);
Cv2.ImWrite("output_image.png", output_image);

4. Summary

  • imread() imshow() imwrite() 图像读取,图像显示,图像保存;
  • waitKey()and functions, as well as display functionsdestroyAllWindows()
    • Close image window on key press
    • and clear any open image windows from memory

 c# OpenCV article directory

c# OpenCV detection (spot detection, edge detection, contour detection) (5)

c# OpenCV basic drawing (line, ellipse, rectangle, circle, polygon, text) (4)
c# OpenCV image cropping, resizing, rotation, perspective (3)

c#OpenCV reads, displays and writes images (2)

c# OpenCV installation (1)

Guess you like

Origin blog.csdn.net/hb_ljj/article/details/134768769