欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

Python-OpenCV 处理图像(二)(三):滤镜和图像运算 图像像素点操作

发布时间:2025/3/21 python 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Python-OpenCV 处理图像(二)(三):滤镜和图像运算 图像像素点操作 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

0x01. 滤镜

喜欢自拍的人肯定都知道滤镜了,下面代码尝试使用一些简单的滤镜,包括图片的平滑处理、灰度化、二值化等:

import cv2.cv as cvimage=cv.LoadImage('img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the image cv.ShowImage("Original", image)grey = cv.CreateImage((image.width ,image.height),8,1) #8depth, 1 channel so grayscale cv.CvtColor(image, grey, cv.CV_RGBA2GRAY) #Convert to gray so act as a filter cv.ShowImage('Greyed', grey)# 平滑变换 smoothed = cv.CloneImage(image) cv.Smooth(image,smoothed,cv.CV_MEDIAN) #Apply a smooth alogrithm with the specified algorithm cv.MEDIAN cv.ShowImage("Smoothed", smoothed)# 均衡处理 cv.EqualizeHist(grey, grey) #Work only on grayscaled pictures cv.ShowImage('Equalized', grey)# 二值化处理 threshold1 = cv.CloneImage(grey) cv.Threshold(threshold1,threshold1, 100, 255, cv.CV_THRESH_BINARY) cv.ShowImage("Threshold", threshold1)threshold2 = cv.CloneImage(grey) cv.Threshold(threshold2,threshold2, 100, 255, cv.CV_THRESH_OTSU) cv.ShowImage("Threshold 2", threshold2)element_shape = cv.CV_SHAPE_RECT pos=3 element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape) cv.Dilate(grey,grey,element,2) #Replace a pixel value with the maximum value of neighboors #There is others like Erode which replace take the lowest value of the neighborhood #Note: The Structuring element is optionnal cv.ShowImage("Dilated", grey)cv.WaitKey(0)

0x02. HighGUI

OpenCV 内建了一套简单的 GUI 工具,方便我们在处理界面上编写一些控件,动态的改变输出:

import cv2.cv as cvim = cv.LoadImage("img/lena.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) thresholded = cv.CreateImage(cv.GetSize(im), 8, 1)def onChange(val):cv.Threshold(im, thresholded, val, 255, cv.CV_THRESH_BINARY)cv.ShowImage("Image", thresholded)# 创建一个滑动条控件 onChange(100) #Call here otherwise at startup. Show nothing until we move the trackbar cv.CreateTrackbar("Thresh", "Image", 100, 255, onChange) #Threshold value arbitrarily set to 100cv.WaitKey(0)

0x03. 选区操作

有事希望对图像中某一块区域进行变换等操作,就可以使用如下方式:

import cv2.cv as cvim = cv.LoadImage("img/lena.jpg",3)# 选择一块区域 cv.SetImageROI(im, (50,50,150,150)) #Give the rectangle coordinate of the selected area# 变换操作 cv.Zero(im) #cv.Set(im, cv.RGB(100, 100, 100)) put the image to a given value# 解除选区 cv.ResetImageROI(im) # Reset the ROIcv.ShowImage("Image",im)cv.WaitKey(0)

0x04. 运算

对于多张图片,我们可以进行一些运算操作(包括算数运算和逻辑运算),下面的代码将演示一些基本的运算操作:

import cv2.cv as cv#or simply import cvim = cv.LoadImage("img/lena.jpg") im2 = cv.LoadImage("img/fruits-larger.jpg") cv.ShowImage("Image1", im) cv.ShowImage("Image2", im2)res = cv.CreateImage(cv.GetSize(im2), 8, 3)# 加 cv.Add(im, im2, res) #Add every pixels together (black is 0 so low change and white overload anyway) cv.ShowImage("Add", res)# 减 cv.AbsDiff(im, im2, res) # Like minus for each pixel im(i) - im2(i) cv.ShowImage("AbsDiff", res)# 乘 cv.Mul(im, im2, res) #Multiplie each pixels (almost white) cv.ShowImage("Mult", res)# 除 cv.Div(im, im2, res) #Values will be low so the image will likely to be almost black cv.ShowImage("Div", res)# 与 cv.And(im, im2, res) #Bit and for every pixels cv.ShowImage("And", res)对于多张图片,我们可以进行一些运算操作(包括算数运算和逻辑运算),下面的代码将演示一些基本的运算操作:# 或 cv.Or(im, im2, res) # Bit or for every pixels cv.ShowImage("Or", res)# 非 cv.Not(im, res) # Bit not of an image cv.ShowImage("Not", res)# 异或 cv.Xor(im, im2, res) #Bit Xor cv.ShowImage("Xor", res)# 乘方 cv.Pow(im, res, 2) #Pow the each pixel with the given value cv.ShowImage("Pow", res)# 最大值 cv.Max(im, im2, res) #Maximum between two pixels #Same form Min MinS cv.ShowImage("Max",res)

cv.WaitKey(0)

———————————————————————————————————————————分割线———————————————————————————————————————————————————

0x01. 像素

有两种直接操作图片像素点的方法:

第一种办法就是将一张图片看成一个多维的list,例如对于一张图片im,想要操作第四行第四列的像素点就直接 im[3,3] 就可以获取到这个点的RGB值。

第二种就是使用 OpenCV 提供的 Get1D、 Get2D 等函数。

推荐使用第一种办法吧,毕竟简单。

0x02. 获取行和列像素

有一下四个函数:

  • cv.GetCol(im, 0): 返回第一列的像素

  • cv GetCols(im, 0, 10): 返回前 10 列

  • cv.GetRow(im, 0): 返回第一行

  • cv.GetRows(im, 0, 10): 返回前 10 行

0x03. 批量处理

需要批量处理所有的像素点的时候,只需要使用for循环迭代处理就可以了:

import cv2.cv as cvim = cv.LoadImage("img/lena.jpg")for i in range(im.height):for j in range(im.width):im[i,j] # 这里可以处理每个像素点

还有一种迭代处理的方式是使用 LineIterator,不过在声明 LineIterator 的时候需要制定处理像素点的开始点和结束点。

import cv2.cv as cvim = cv.LoadImage("img/lena.jpg")li = cv.InitLineIterator(im, (0, 0), (im.rows, im.cols)) #So loop the entire matrixfor (r, g, b) in li:# 这里可以对每个像素点的 r g b 进行处理

娱乐一下, 随机获取 5000 个像素点,然后把颜色换成一个随机的值(salt):

import cv2.cv as cvimport random# 这里也可以使用 Get2D/Set2D 来加载图片 im = cv.LoadImage("img/lena.jpg") for k in range(5000): #Create 5000 noisy pixelsi = random.randint(0,im.height-1)j = random.randint(0,im.width-1)color = (random.randrange(256),random.randrange(256),random.randrange(256))im[i,j] = colorcv.ShowImage("Noize", im) cv.WaitKey(0)

效果图:



from: https://segmentfault.com/a/1190000003742433

https://segmentfault.com/a/1190000003742442

总结

以上是生活随笔为你收集整理的Python-OpenCV 处理图像(二)(三):滤镜和图像运算 图像像素点操作的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。