opencv(7)我一定一定坚持学完啊!

opencv:轮廓以及直方图

1.找轮廓 绘制轮廓
• 为了更加准确,要使用二值化图像。在寻找轮廓之前,要进行阈值化处理或者 Canny 边界检测。
• 查找轮廓的函数会修改原始图像。如果你在找到轮廓之后还想使用原始图像的话,你应该将原始图像存储到其他变量中。
• 在 OpenCV 中,查找轮廓就像在黑色背景中超白色物体。你应该记住,要找的物体应该是白色而背景应该是黑色。

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('D:/code/opencv/images/1.png')
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#对图像进行二值化处理
ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
#contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
#所有的边界点都会被存储
contours, hierarchy= cv.findContours(binary,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
#参数:输入的图像,轮廓的检索模式 ,轮廓的近似方法:知识端点
dst=cv.drawContours(img,contours,-1,(0,0,255),3)
#参数:原始图像  轮廓 -1:绘制所有轮廓  轮廓的颜色为红色  宽3
cv.imshow("d",dst)
cv.waitKey(0)
cv.destroyAllWindows()

在这里插入图片描述
2.

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('D:/code/opencv/images/approximate.png')
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#对图像进行二值化处理
ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
contours,hierarchy = cv.findContours(binary, 1, 2)
cnt = contours[0]
M = cv.moments(cnt)
print("矩",M)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print("重心cx,cy:",cx,cy)
area = cv.contourArea(cnt)
print("轮廓面积",area)
perimeter = cv.arcLength(cnt,True)
print("轮廓周长",perimeter)

epsilon = 0.01*cv.arcLength(cnt,True)
approx = cv.approxPolyDP(cnt,epsilon,True)
#多边形轮廓近似
cv.polylines(img, [approx], True, (0, 0, 255), 2)
#把近似的轮廓画出来
(x,y),radius = cv.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
img = cv.circle(img,center,radius,(0,255,0),2)
#轮廓的最小外接圆
cv.imshow("approx",img)
cv.waitKey(0)
cv.destroyAllWindows()

在这里插入图片描述

直方图

通过直方图你可以对整幅图像的灰度分布有一个整体的了解。直方图的 x 轴是灰度值(0 到 255),y 轴是图片中具有同一个灰度值的点的数目。

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('D:/code/opencv/images/02.jpg')
#plt.hist(img.ravel(),256,[0,256]);
hist = cv.calcHist([img],[0],None,[256],[0,256])
plt.plot(hist)
plt.show()

cv.calHist()参数表示:

  1. images: 原图像(图像格式为 uint8 或 float32)。当传入函数时应该用中括号 [] 括起来,例如:[img]。
  2. channels: 同样需要用中括号括起来,它会告诉函数我们要统计那幅图像的直方图。如果输入图像是灰度图,它的值就是 [0];如果是彩色图像的话,传入的参数可以是 [0],[1],[2] 它们分别对应着通道 B,G,R。
  3. mask: 掩模图像。要统计整幅图像的直方图就把它设为 None。但是如果你想统计图像某一部分的直方图的话,你就需要制作一个掩模图像,并使用它。(后边有例子)
  4. histSize:BIN 的数目。也应该用中括号括起来,例如:[256]。
  5. ranges: 像素值范围,通常为 [0,256]
#numpy的方式
#img.ravel() 将图像转成一维数组,这里没有中括号。
hist,bins = np.histogram(img.ravel(),256,[0,256])
#matplotlib的方式
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('home.jpg',0)
plt.hist(img.ravel(),256,[0,256]);
plt.show()
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('home.jpg')
color = ('b','g','r') # 对一个列表或数组既要遍历索引又要遍历元素时
# 使用内置 enumerrate 函数会有更加直接,优美的做法
#enumerate 会将数组或列表组成一个索引序列。
# 使我们再获取索引和索引内容的时候更加方便
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
plt.show()

直方图均衡化

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('D:/code/python/jupyter/date/image/7.jpg')
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
equ = cv.equalizeHist(gray)
#全局均衡化
cv.imshow('res',equ)
cv.waitKey(0)
cv.destroyAllWindows()

自适应均衡化

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('D:/code/python/jupyter/date/image/7.jpg')
g=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
c = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
#图像被分成8*8的小块  然后进行均衡化
cl1 = c.apply(g)

cv.imshow('res', cl1)
cv.waitKey(0)
cv.destroyAllWindows()

一起退学把!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值