函数:ret, dst = cv2.threshold(src, thresh, maxval, type)
功能:固定阈值二值化图像,大于阈值的是255,否则就是0
参数:
src: 输入图,只能输入单通道图像,通常来说为灰度图。
dst: 输出图。
thresh: 阈值。
maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值。
type:二值化操作的类型,包含以下5种类型: cv2.THRESH_BINARY;cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV。
函数: dst = cv2.adaptiveThreshold(src, maxval, thresh_type, type, Block Size, C)
功能:自适应阈值二值化图像,根据图片一小块区域的值来计算对应区域的阈值,从而得到也许更为合适的图片。
参数:
src: 输入图,只能输入单通道图像,通常来说为灰度图。
dst: 输出图。
maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值。
thresh_type: 阈值的计算方法,包含以下2种类型:cv2.ADAPTIVE_THRESH_MEAN_C; cv2.ADAPTIVE_THRESH_GAUSSIAN_C。
type:二值化操作的类型,与固定阈值函数相同,包含以下5种类型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV。
Block Size: 图片中分块的大小。
C :阈值计算方法中的常数项。
函数:connectedComponentsWithStats(img, 8, cv2.CV_32S)
功能:求得最大连通域。
返回:假如存在一个375494大小的图片有,有39个连通域。
num_labels:连通域个数39;
stats:395,5个数分别对应各个轮廓的x,y,width,height和面积。注意0的区域标识的是background;
labels:大小375494,标记图,图中不同连通域使用不同的标记(当前像素是第几个轮廓),和原图宽高一致; centroids:392,各个轮廓的中心点。
下图为stats的数据形式:
code:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
im=cv2.imread('C:\\Users\\Desktop\\**.bmp')
w,h,n= im.shape
im_gray = cv2.cvtColor(im,cv2.COLOR_RGB2GRAY)
_,thresh = cv2.threshold(im_gray,128,255,cv2.THRESH_BINARY)
cv2.imshow('th',thresh)
nccomps = cv2.connectedComponentsWithStats(thresh)
_ = nccomps[0]
labels = nccomps[1]
status = nccomps[2]
centroids = nccomps[3]
for row in range(status.shape[0]):
if status[row,:][0] == 0 and status[row,:][1] == 0:
background = row
else:
continue
status_no_background = np.delete(status,background,axis=0)
rec_value_max = np.asarray(status_no_background[:,4].max())
re_value_max_position = np.asarray(status_no_background[:,4].argmax())
h = np.asarray(labels,'uint8')
h[h==(re_value_max_position+1)]=255
for single in range(centroids.shape[0]):
print(tuple(map(int,centroids[single])))
#position = tuple(map(int,centroids[single]))
#cv2.circle(h, position, 1, (255,255,255), thickness=0,lineType=8)
cv2.imshow('h',h)
cv2.imshow('im_bw',thresh)
cv2.imshow('im_origin',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
函数:cv2.calcHist([image],[channels],mask,histSize,ranges)
直方图就是对图像的中的这些像素点的值进行统计,就可以清晰了解图像的整体灰度分布。一般情况下直方图都是灰度图像,直方图x轴是灰度值(一般0~255),y轴就是图像中每一个灰度级对应的像素点的个数。
功能:
返回:
image:输入图像,传入时应该用中括号[]括起来
channels:传入图像的通道,如果是灰度图像,那就不用说了,只有一个通道,值为0,如果是彩色图像(有3个通道),那么值为0,1,2,中选择一个,对应着BGR各个通道。这个值也得用[]传入。
mask:掩膜图像。如果统计整幅图,那么为none。主要是如果要统计部分图的直方图,就得构造相应的炎掩膜来计算。
histSize:灰度级的个数,需要中括号,比如[256]
ranges:像素值的范围,通常[0,256],有的图像如果不是0-256,比如说你来回各种变换导致像素值负值、很大,则需要调整后才可以。
code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('myid.jpg',0) #直接读为灰度图像
#opencv方法读取-cv2.calcHist(速度最快)
#图像,通道[0]-灰度图,掩膜-无,灰度级,像素范围
hist_cv = cv2.calcHist([img],[0],None,[256],[0,256])
#numpy方法读取-np.histogram()
hist_np,bins = np.histogram(img.ravel(),256,[0,256])
#numpy的另一种方法读取-np.bincount()(速度=10倍法2)
hist_np2 = np.bincount(img.ravel(),minlength=256)
plt.subplot(221),plt.imshow(img,'gray')
plt.subplot(222),plt.plot(hist_cv)
plt.subplot(223),plt.plot(hist_np)
plt.subplot(224),plt.plot(hist_np2)
参考:https://blog.csdn.net/sinat_21258931/article/details/61418681
https://blog.csdn.net/weixin_42222152/article/details/90172715
https://blog.csdn.net/YZXnuaa/article/details/79231817