YouTip LogoYouTip

Opencv First Example

Next, let's look at a simple OpenCV example, which will implement the following features: 1. Read an image. 2. Display the image. 3. Save the image. 4. Add simple user interaction. You can use the following image as an example: !(https://static.jyshare.com/images/mix/bird.jpg) Save the following code in display_image.py file: ## Example (Python) # Import OpenCV library import cv2 # 1. Read image # Replace with the actual image path, here it's "bird.jpg" in the current directory image_path ="./bird.jpg" image = cv2.imread(image_path) # Check if image was loaded successfully if image is None: print("Error: Failed to load image, please check if the path is correct.") exit() # 2. Display image # Create a window named "Display Image" and display the image in it cv2.imshow("Display Image", image) # 3. Wait for user key press # Parameter 0 means wait indefinitely until user presses any key key = cv2.waitKey(0) # 4. Perform operation based on user key press if key ==ord('s'): # If 's' key is pressed # Save image output_path ="saved_image.jpg" cv2.imwrite(output_path, image) print(f"Image saved as {output_path}") else: # If other key is pressed print("Image not saved.") # 5. Close all windows cv2.destroyAllWindows() Execute the following command: python display_image.py The popup window displays as follows: !(#) In the displayed image window, press the 's' key to save the image, or press other key to exit. The C++ code for the above example is as follows: ## Example (C++) #include #include using namespace cv; using namespace std; int main(){ // 1. Read image // Replace with the actual image path, here it's "bird.jpg" in the current directory string image_path ="./bird.jpg"; Mat image = imread(image_path); // Check if image was loaded successfully if(image.empty()){ cout<<"Error: Failed to load image, please check if the path is correct."<< endl; return-1; } // 2. Display image // Create a window named "Display Image" and display the image in it namedWindow("Display Image", WINDOW_AUTOSIZE); imshow("Display Image", image); // 3. Wait for user key press // Parameter 0 means wait indefinitely until user presses any key int key = waitKey(0); // 4. Perform operation based on user key press if(key =='s'){// If 's' key is pressed // Save image string output_path ="saved_image.jpg"; imwrite(output_path, image); cout<<"Image saved as "<< output_path << endl; }else{// If other key is pressed cout<<"Image not saved."<< endl; } // 5. Close all windows destroyAllWindows(); return 0; } ### Extended Exercises Next, we can try making some code modifications. **1. Modify window name:** Change "Display Image" to another name, for example "My Image". cv2.imshow("My Image", image) **2. Save as different format:** Change "saved_image.jpg" to "saved_image.png" and observe the saved result. # 4. Perform operation based on user key press if key == ord('s'): # If 's' key is pressed # Save image output_path = "saved_image.png" cv2.imwrite(output_path, image) print(f"Image saved as {output_path}") **3. Add more interaction:** For example, press 'q' key to exit the program directly. if key == ord('s'): # If 's' key is pressed # Save image output_path = "saved_image.jpg" cv2.imwrite(output_path, image) print(f"Image saved as {output_path}") elif key == ord('q'): # If 'q' key is pressed print("Program exited.") else: # If other key is pressed print("Image not saved, program exited.") * `ord('q')` returns the ASCII code value of character `'q'`. * If user presses `'q'` key, print the prompt message `"Program exited."`, and then the program will continue to execute `cv2.destroyAllWindows()` to close the window and exit.
← Opencv BasicOpencv Install β†’