Python3与OpenCV图像处理(二)——NumPy数组操作
2019-07-27 22:29
791 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41874898/article/details/97565119
NumPy数组操作
【参考视频网址:】https://www.bilibili.com/video/av24998616
- 1 获取图像像素特征
- 2 创建图像
- 注意:需要在当前目录下创建一个pic文件夹,放0.jpg图片
import cv2 import numpy as np # 图像像素取反(速度慢) def access_pixels(image): print(image.shape) height = image.shape[0] width = image.shape[1] channels = image.shape[2] print("width :%s, height: %s,channels: %s" % (height, width, channels)) # 将图像的值取反 for row in range(height): for col in range(width): for c in range(channels): pv = image[row, col, c] image[row, col, c] = 255 - pv cv2.imshow("pixels_demo", image) # 图像像素取反(速度快) def inverse(image): dst = cv2.bitwise_not(image) cv2.imshow("inverse_demo", dst) # Required argument 'mat' (pos 2) not found即,少了一个参数,显示窗口没有定义 # 创建图像 def create_image(): # 创建纯黑图像 img = np.zeros([400, 400, 3]) img[:, :, 1] = np.ones([400, 400]) * 255 # 这里的1对应的是BGR里面的G,第二个通道,所以出来的图是绿色的 cv2.imshow("new_image", img) # 上述为三通道,现创建一个单通道的(灰度图像)。 img = np.zeros([400, 400, 1], np.uint8) img[:, :, 0] = np.ones([400, 400]) * 127 cv2.imshow("my_gray_image", img) cv2.imwrite("pic/my_image.jpg", img) # 上述实现的另一种方法 img = np.ones([400, 400, 1], np.uint8) img = img * 127 cv2.imshow("my_gray_image", img) n1 = np.ones([3, 3], np.float32) # np.uint8不太好,大于128会被自动截断。 n1.fill(122.333) # [[122.33, 122.33, 122.33][122.33, 122.33, 122.33][122.33, 122.33, 122.33]] n2 = np.ones([3, 3, 3]) n22 = n2.reshape([1, 27]) # 把矩阵变成1行27列,仍然是一个二维矩阵 def main(): path = "pic/0.jpg" src = cv2.imread(path) # BGR顺序 cv2.namedWindow("input_image", cv2.WINDOW_AUTOSIZE) cv2.imshow("input_image", src) # 1获取图像的像素特征 t1 = cv2.getTickCount() access_pixels(src) t2 = cv2.getTickCount() time = (t2 - t1) / cv2.getTickFrequency() # time出来的是毫秒 print("time: %s ms"%(time * 1000)) # 乘以1000,结果是毫秒 # 2像素值快速取反 inverse(src) # python解释执行,for循环很慢。而cv2.调用C++速度很快。 # 3创建新的图像 create_image() cv2.waitKey(0) # cv2.waitKey顾名思义等待键盘输入,单位为毫秒,即等待指定的毫秒数看是否有键盘输入, # 若在等待时间内按下任意键则返回按键的ASCII码,程序继续运行。 # 若没有按下任何键,超时后返回-1。参数为0表示无限等待。 # 不调用waitKey的话,窗口会一闪而逝,看不到显示的图片。 cv2.destroyAllWindows() # cv2.destroyWindow(name)销毁指定窗口 print("Hi,Python!") if __name__ == "__main__": main()
相关文章推荐
- OpenCV+Python——Numpy数组操作
- Python-OpenCV 处理图像(一):基本操作
- Python3与OpenCV3.3 图像处理(二)--图像基本操作
- Python3与OpenCV3.3 图像处理(二十四)--开闭操作
- 基本的图像操作和处理(python+OpenCV)
- Python-OpenCV 处理图像(一):基本操作
- Python-OpenCV 处理图像(一):基本操作 cv2
- Python-OpenCV 处理图像(三):图像像素点操作
- python OpenCV学习笔记(四):Numpy数组操作
- Python3与OpenCV3.3 图像处理(二十五)--开闭操作(补充)
- python3-opencv库(2)--遍历图像像素,利用Numpy创建图像,Numpy基本操作
- Python-OpenCV 处理图像(三):图像像素点操作
- Python-Opencv 图像处理基本操作(一)
- Python使用scipy和numpy操作处理图像
- Python用OpenCV 处理图像的基本操作
- OpenCV-Python图像处理教程:图像基本操作
- Python-OpenCV 处理图像(三):图像像素点操作
- Python-OpenCV 处理图像基本操作
- 图像处理之PIL.Image与numpy.array之间的相互转换 (Python-OpenCV)
- OpenCv-Python 图像处理基本操作