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

0x01. 滤镜

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

 
  1. import cv2.cv as cv

  2.  
  3. image=cv.LoadImage('img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the image

  4. cv.ShowImage("Original", image)

  5.  
  6. grey = cv.CreateImage((image.width ,image.height),8,1) #8depth, 1 channel so grayscale

  7. cv.CvtColor(image, grey, cv.CV_RGBA2GRAY) #Convert to gray so act as a filter

  8. cv.ShowImage('Greyed', grey)

  9.  
  10. # 平滑变换

  11. smoothed = cv.CloneImage(image)

  12. cv.Smooth(image,smoothed,cv.CV_MEDIAN) #Apply a smooth alogrithm with the specified algorithm cv.MEDIAN

  13. cv.ShowImage("Smoothed", smoothed)

  14.  
  15. # 均衡处理

  16. cv.EqualizeHist(grey, grey) #Work only on grayscaled pictures

  17. cv.ShowImage('Equalized', grey)

  18.  
  19. # 二值化处理

  20. threshold1 = cv.CloneImage(grey)

  21. cv.Threshold(threshold1,threshold1, 100, 255, cv.CV_THRESH_BINARY)

  22. cv.ShowImage("Threshold", threshold1)

  23.  
  24. threshold2 = cv.CloneImage(grey)

  25. cv.Threshold(threshold2,threshold2, 100, 255, cv.CV_THRESH_OTSU)

  26. cv.ShowImage("Threshold 2", threshold2)

  27.  
  28. element_shape = cv.CV_SHAPE_RECT

  29. pos=3

  30. element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape)

  31. cv.Dilate(grey,grey,element,2) #Replace a pixel value with the maximum value of neighboors

  32. #There is others like Erode which replace take the lowest value of the neighborhood

  33. #Note: The Structuring element is optionnal

  34. cv.ShowImage("Dilated", grey)

  35.  
  36. cv.WaitKey(0)

0x02. HighGUI

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

 
  1. import cv2.cv as cv

  2.  
  3. im = cv.LoadImage("img/lena.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)

  4. thresholded = cv.CreateImage(cv.GetSize(im), 8, 1)

  5.  
  6. def onChange(val):

  7. cv.Threshold(im, thresholded, val, 255, cv.CV_THRESH_BINARY)

  8. cv.ShowImage("Image", thresholded)

  9.  
  10. # 创建一个滑动条控件

  11. onChange(100) #Call here otherwise at startup. Show nothing until we move the trackbar

  12. cv.CreateTrackbar("Thresh", "Image", 100, 255, onChange) #Threshold value arbitrarily set to 100

  13.  
  14. cv.WaitKey(0)

0x03. 选区操作

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

 
  1. import cv2.cv as cv

  2.  
  3. im = cv.LoadImage("img/lena.jpg",3)

  4.  
  5. # 选择一块区域

  6. cv.SetImageROI(im, (50,50,150,150)) #Give the rectangle coordinate of the selected area

  7.  
  8. # 变换操作

  9. cv.Zero(im)

  10. #cv.Set(im, cv.RGB(100, 100, 100)) put the image to a given value

  11.  
  12. # 解除选区

  13. cv.ResetImageROI(im) # Reset the ROI

  14.  
  15. cv.ShowImage("Image",im)

  16.  
  17. cv.WaitKey(0)

0x04. 运算

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

 
  1. import cv2.cv as cv#or simply import cv

  2.  
  3. im = cv.LoadImage("img/lena.jpg")

  4. im2 = cv.LoadImage("img/fruits-larger.jpg")

  5. cv.ShowImage("Image1", im)

  6. cv.ShowImage("Image2", im2)

  7.  
  8. res = cv.CreateImage(cv.GetSize(im2), 8, 3)

  9.  
  10. # 加

  11. cv.Add(im, im2, res) #Add every pixels together (black is 0 so low change and white overload anyway)

  12. cv.ShowImage("Add", res)

  13.  
  14. # 减

  15. cv.AbsDiff(im, im2, res) # Like minus for each pixel im(i) - im2(i)

  16. cv.ShowImage("AbsDiff", res)

  17.  
  18. # 乘

  19. cv.Mul(im, im2, res) #Multiplie each pixels (almost white)

  20. cv.ShowImage("Mult", res)

  21.  
  22. # 除

  23. cv.Div(im, im2, res) #Values will be low so the image will likely to be almost black

  24. cv.ShowImage("Div", res)

  25.  
  26. # 与

  27. cv.And(im, im2, res) #Bit and for every pixels

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

  29.  
  30. # 或

  31. cv.Or(im, im2, res) # Bit or for every pixels

  32. cv.ShowImage("Or", res)

  33.  
  34. # 非

  35. cv.Not(im, res) # Bit not of an image

  36. cv.ShowImage("Not", res)

  37.  
  38. # 异或

  39. cv.Xor(im, im2, res) #Bit Xor

  40. cv.ShowImage("Xor", res)

  41.  
  42. # 乘方

  43. cv.Pow(im, res, 2) #Pow the each pixel with the given value

  44. cv.ShowImage("Pow", res)

  45.  
  46. # 最大值

  47. cv.Max(im, im2, res) #Maximum between two pixels

  48. #Same form Min MinS

  49. cv.ShowImage("Max",res)

  50.  

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循环迭代处理就可以了:

 
  1. import cv2.cv as cv

  2.  
  3. im = cv.LoadImage("img/lena.jpg")

  4.  
  5. for i in range(im.height):

  6. for j in range(im.width):

  7. im[i,j] # 这里可以处理每个像素点

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

 
  1. import cv2.cv as cv

  2.  
  3. im = cv.LoadImage("img/lena.jpg")

  4.  
  5. li = cv.InitLineIterator(im, (0, 0), (im.rows, im.cols)) #So loop the entire matrix

  6.  
  7. for (r, g, b) in li:

  8. # 这里可以对每个像素点的 r g b 进行处理

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

 
  1. import cv2.cv as cv

  2.  
  3. import random

  4.  
  5. # 这里也可以使用 Get2D/Set2D 来加载图片

  6. im = cv.LoadImage("img/lena.jpg")

  7.  
  8. for k in range(5000): #Create 5000 noisy pixels

  9. i = random.randint(0,im.height-1)

  10. j = random.randint(0,im.width-1)

  11. color = (random.randrange(256),random.randrange(256),random.randrange(256))

  12. im[i,j] = color

  13.  
  14. cv.ShowImage("Noize", im)

  15. cv.WaitKey(0)

效果图:

 

 

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

https://segmentfault.com/a/1190000003742442

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值