Skip to main content

OpenCV Crop an image in Python

 Crop operation in OpenCV is very simple. You should first read the image. Then the values x, y, h, w must be determined to specify the region to be cropped. You can do this using the code below.

import cv2

image = cv2.imread('image.jpg')
crop = image[y:y+h, x:x+w]
cv2.imshow('Image', crop)


Popular posts from this blog

OpenCV Capture Image From Camera

Capture Image From Camera Using OpenCV, you can capture video from your computer's webcam and use these videos for many purposes. OpenCV provides a very simple interface for video capture process. It takes preparation to capture video. To capture a video, you must create a VideoCapture object. Its argument may be the device index. capture = cv2.VideoCapture( 0 )   The device index is just the number that determines which camera to use. Normally 0 is selected if a camera will be connected. You can select the second camera by giving a value of 1. After that, you can shoot frame by frame. ret , frame = () returns a bool value. If the frame reads correctly, the value will be True. With the Cap.isOpened () method, you can check whether the video capture process is started. If it's true, it's okay. If not, you need to open it using (). The codes are all as follows: import cv2 capture = cv2.VideoCapture( 0 ) while ( True ):   # C

Opencv Read-Write-Display Image Python

Read-Write-Display Image In this lesson, you will learn how to read, how to view and save. The functions cv2.imread (), cv2.imshow (), cv2.imwrite () will be used for these operations. To read an image, the function cv2.imread () is used. The image to be read must be in the working directory or a full visual path must be given. The second argument is a sign that indicates how the image should be read. cv2.IMREAD_COLOR: Indicates that a color image will be loaded. It is the default flag. The integer value for this flag can be assigned to 1. cv2.IMREAD_GRAYSCALE: Loads the image in grayscale mode. cv2.IMREAD_UNCHANGED: Loads the image as included in the alpha channel. import cv2 # The code below reads the image and assigns it to the image variable. image = cv2.imread( 'bird' ) The function cv2.imshow () is used to display an image. The window opens automatically in image size. The first argument is the name of the window to open. The second argument is the image we re

Show OpenCV Image With Matplotlib

Show Image With Matplotlib Matplotlib is a drawing library that offers a wide variety of drawing methods for Python. Matplotlib; The basic python library we use in data visualization. It allows us to make 2 and 3 dimensional drawings. In order to use Matplotlib, we need to add it to our project as follows: from matplotlib import pyplot as plt The image is read as follows: img = cv2.imread( 'bird.jpg' )   Finally, the captured image is displayed on the screen with the following code: plt.imshow(img) plt.xticks([]) , plt.yticks([]) Code and output are as follows: import cv2 from matplotlib import pyplot as plt img = cv2.imread( 'bird.jpg' ) plt.imshow(img) plt.xticks([]) , plt.yticks([]) Output: