
To implement this equation in Python OpenCV, you can use the addWeighted() method. If a is 1, there will be no contrast effect on the image.ī stands for beta.

If the value of a is between 0 and 1 (smaller than 1 but greater than 0), there would be lower contrast.

If a is greater than 1, there will be higher contrast. Here a is alpha which defines the contrast of the image. In the resize method, you can either specify the values of x and y axis or the number of rows and columns which tells the size of the image. To resize an image, you can use the resize() method of openCV. Now display the original and cropped image in the output: cv2.imshow('Original Image', img)Ĭv2.imshow('Cropped Image', croppedImage) Here we specified the range from starting to ending of rows and columns. Note that you have to cast the starting and ending values to integers because when mapping, the indexes are always integers. Now map these values to the original image. Similarly, to get the ending point of the cropped image, specify the percentage values as below: startRow = int(height*.15) You can get the starting point by specifying the percentage value of the total height and the total width. Similarly, start from column number 10 until column number 15 will give the width of the image. For example, start from row number 10 till row number 15 will give the height of the image. This will define the size of the newly created image. Now get the starting and ending index of the row and column. To show the image, use imshow() as below: cv2.imshow('Rotated Image', rotatedImage)Īfter running the above lines of code, you will have the following output:įirst, we need to import the cv2 module and read the image and extract the width and height of the image: import cv2 The rotated image is stored in the rotatedImage matrix. rotatedImage = cv2.warpAffine(img, rotationMatrix, (width, height)) To rotate the image, we have a cv2 method named wrapAffine which takes the original image, the rotation matrix of the image and the width and height of the image as arguments. The next step is to rotate our image with the help of the rotation matrix.
#RESIZE IMAGE OPENCV CODE#
To get the rotation matrix of our image, the code will be: rotationMatrix = cv2.getRotationMatrix2D((width/2, height/2), 90. Here the center is the center point of rotation, the angle is the angle in degrees and scale is the scale property which makes the image fit on the screen. The syntax of getRotationMatrix2D() is: cv2.getRotationMatrix2D(center, angle, scale) To get the rotation matrix, we use the getRotationMatrix2D() method of cv2. Okay, now we have our image matrix and we want to get the rotation matrix. The shape attribute returns the height and width of the image matrix. To rotate this image, you need the width and the height of the image because you will use them in the rotation process as you will see later. Here we set the time to zero to show the window forever until we close it manually. The waitkey functions take time as an argument in milliseconds as a delay for the window to close.

To display the image, you can use the imshow() method of cv2. All the time you are working with a NumPy array. It’s a NumPy array! That why image processing using OpenCV is so easy. The image is now treated as a matrix with rows and columns values stored in img.Īctually, if you check the type of the img, it will give you the following result: >print(type(img)) Now to read the image, use the imread() method of the cv2 module, specify the path to the image in the arguments and store the image in a variable as below: img = cv2.imread("pyimg.jpg") Let’s have some fun with some images!įirst of all, import the cv2 module. Now OpenCV is installed successfully and we are ready.
#RESIZE IMAGE OPENCV INSTALL#
To install OpenCV on your system, run the following pip command: pip install opencv-python
