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():
它以图像文件的绝对路径/相对路径作为参数,并返回其对应的图像矩阵。
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 the case of a Grayscale image, only one plane is needed. If the number of planes is 1 then shape attribute only return number of rows and columns.
在灰度图像的情况下,仅需要一个平面。 如果平面数为1,则shape属性仅返回行数和列数。
Also, here 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[ 1 : : 2])
Output:
[2, 4, 6, 9]
Python程序,无需使用任何内置函数即可调整灰度图像的大小 (Python program to resize a grayscale image without using any inbuilt functions )
# open-cv library is installed as cv2 in python
# import cv2 library into this program
import cv2
# give value by which you want to resize an image
# here we want to resize an image as one half of the original image
x,y= 2,2
# read an image using imread() function of cv2
# we have to pass only the path of the image
img = cv2.imread(r'C:/Users/user/Desktop/pic2.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)
# print shape of the image matrix
# using shape attribute
print("original image shape:",img.shape)
# here we take alternate row,column pixel.
# we take half pixel of rows and columns respectively
# so that it is one half of image matrix.
resize_img = img[::x,::y]
cv2.imshow('resize image',resize_img)
# print shape of the image matrix
# using shape attribute
print("resize image shape:",resize_img.shape)
Output
输出量
翻译自: https://www.includehelp.com/python/resize-a-grayscale-image-without-using-any-inbuilt-functions.aspx