Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

OpenCV Introduction

OpenCV Image Read & Write

OpenCV Image Channels

OpenCV Color Conversion

OpenCV Rotation & Crop

OpenCV Text & Shape

OpenCV Video

OpenCV Arithmetics Operation

OpenCV Thresholding

OpenCV Blur & Blending

OpenCV Edge Detection

OpenCV Contours

OpenCV Hough Transformation Lines

OpenCV Moments & Convexhull

OpenCV Morphological Transformation

OpenCV Template Matching

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Image crop and rotation in opencv

How to rotate an image?

Input
import numpy as np
import cv2

path=r"E:\picture\prac_deep\dd.jpg"

img=cv2.imread(path)

height=img.shape[0]
width=img.shape[1]

rotate_matrix=cv2.getRotationMatrix2D((height/2,width/2),76,.2)
'''
By doing this we are just rotating the matrix. Here the first parameter is height, then width, then rotation degree, and the scale of the image. scale value range is between 0 to 1. You can see that here we divide the image height and width by 2. You can divide by any number.
'''
rotate_img=cv2.warpAffine(img,rotate_matrix,(width,height))
'''
Here we will rotate the image. The first parameter is an image then rotated matrix variable then the original height and width in the form of a tuple.
'''

cv2.imshow("my image",img)
cv2.imshow("rotate_img",rotate_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

How crop an image?

Input
import numpy as np
import cv2

path=r"E:\picture\prac_deep\dd.jpg"

img=cv2.imread(path)

height=img.shape[0]
width=img.shape[1]

cv2.imshow("my image",img)

start_row,star_col=int(height*.30),int(width*.40)
'''
We know that the images are in the form of matrix. Matrix has. rows and columns. We can crop the image by defining the start and ending row and column. Here height*.30 means that the position where image height is 30% from the original height will be the starting point of the row. Because .30 is a float value, that's why we put it in the int function. width*.40 means that position where image width is 40% from the original width will be the starting point of column.
'''

end_row,end_col=int(height*.75),int(width*.80)
'''
height*.75 means that position where image height is 75% from the original height will be the ending point of the row. width*.80 means that position where image width is 80% from the original width will be the ending point of the column.
'''

crop_img=img[start_row:end_row , star_col:end_col]

cv2.imshow("main_img",img)
cv2.imshow("crop_img",crop_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.