In this program, we will be using two functions of OpenCV-python (cv2) module.. let's see their syntax and descriptions first
在此程序中,我们将使用OpenCV-python(cv2)模块的两个函数。.让我们首先查看它们的语法和描述。
1) imread():
It takes an absolute path/relative path of your image file as an argument and returns its corresponding image matrix.
1)imread():
它以图像文件的绝对路径/相对路径作为参数,并返回其对应的图像矩阵。
If flag value is:
如果标志值为:
1: Loads a color image.
1 :加载彩色图像。
0: Loads image in grayscale mode.
0 :以灰度模式加载图像。
-1: Loads image as such including alpha channel.
-1 :加载图像,包括alpha通道。
If the flag value is not given then show the original image, which path is given.
如果未给出标志值,则显示原始图像,并给出哪个路径。
2) imshow():
It takes window name and image matrix as an argument in order to display an image in a display window with a specified window name.
2)imshow():
它以窗口名称和图像矩阵为参数,以便在具有指定窗口名称的显示窗口中显示图像。
Also In this program, we will be using one attribute of an image matrix:
同样在此程序中,我们将使用图像矩阵的一个属性:
shape: This is the attribute of an image matrix which return shape of an image i.e. consisting of number of rows ,columns and number of planes.
形状:这是图像矩阵的属性,该属性返回图像的形状,即由行数,列数和平面数组成。
In case of Gray Scale image, only one plane is present. If the number of planes is 1 than shape attribute only return number of rows and columns.
如果是灰度图像,则仅存在一个平面。 如果平面数比shape属性多1,则仅返回行数和列数。
Also, in this program we are using the concept of array slicing
另外,在此程序中,我们使用数组切片的概念
Let, A is 1-d array:
A[start:stop:step]
设A为一维数组:
A [开始:停止:步骤]
start: Starting number of the sequence.
start:序列的起始编号。
stop: Generate numbers up to, but not including this number.
停止:生成不超过此数字的数字,但不包括此数字。
step: Difference between each number in the sequence.
步骤:序列中每个数字之间的差。
Example:
例:
A = [1,2,3,4,5,6,7,8,9,10]
print(A[ 10 : : -2])
Output:
[10, 8, 6,4,2]
Python程序无需使用任何内置功能即可将灰度图像旋转180度 (Python program to rotate a grayscale image by 180 degree without using any inbuilt functions )
# import numpy library as np
import numpy as np
# open-cv library is installed as cv2 in python
# import cv2 library into this program
import cv2
# read an image as grayscale using imread() function of cv2
# we have to pass the path of an image and flag value as 0
img = cv2.imread(r'C:/Users/user/Desktop/pic6.jpg',0)
# displaying the image using imshow() function of cv2
# In this : 1st argument is name of the frame
# 2nd argument is the image matrix
cv2.imshow('original image',img)
# shape attribute of img matrix return tuple
# which contains no. of ros and columns present in img.
row,col = img.shape
# here we take all rows in reverse order and columns as before.
img = img[row-1: :-1, :]
# Show the image formed
cv2.imshow("Rotate image", img);
Output
输出量