图像变换

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


img = cv.imread('1.png')    # 读入图像
cv.imshow('Source', img)    # 显示图像
# 图像改变大小和翻转
w, h = img.shape[0:2]   # 取图像的长和宽
resized = cv.resize(img, (int(w/4), int(h/2)))  # 重新设定图像大小
flipped = cv.flip(img, -1)  # 图像反转,1, 0, -1代表方向

# 距离变换:dis = cv.distance(src,distance, maskSize[,dst[,dstType]])
# distanceType:距离计算的方式,DIST_l1,DIST_l2或DIST_C
# maskSize:掩模尺寸,可取DIST_MASK_PRECISE或DIST_MASK_3,5等
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thr = cv.threshold(gray, 100, 255,cv.THRESH_OTSU)  # 图像二值化
dist = cv.distanceTransform(thr, cv.DIST_L2, cv.DIST_MASK_3)
dist_norm = cv.convertScaleAbs(dist)    # 显示图像


# Log-polar变换:dis = cv.logPolar(src, center,M, flags[,dst])
# center:变换中心点
# 幅度尺度参数
# flag: 标志。插值方法和下面选项的组合:CV_WARP_FILL_OUTLIERS填充目标图像中的所有像素;CV_WARP_INVERSE_MAP表示矩阵是从目标图像到原图像的反变换
center = (w/2, h/2)
maxRadius = 0.7*min(center)
M = w/(cv.log(maxRadius))
print(maxRadius, M[0])
log_ploar = cv.logPolar(img, center, M[0] * 0.8, cv.INTER_LINEAR + cv.WARP_FILL_OUTLIERS)



# 计算直方图:matplotlib.pyplot.hist(x, bin = None, range = None,...)
# bin:多少个柱,range:显示的范围
# 直方图均衡化:dst = cv.equalizeHist(sre,[,dst]):增强对比度
plt.hist(gray.ravel(), 256, [0, 256])
plt.show()
equa = cv.equalizeHist(gray)
cv.imshow("Equalized image", equa)



"""
标准 Hough 变换:检测直线
lines = cv.HoughLines(image,rho,theta,threshold[,srn[,stn[,min_theta[,max_theta]]]]])
image:输入的图像,应为二值图像,通常使用边缘检测的结果;
rho:线段以像素为单位的距离精度,double类型的,推荐1.0
theta:线段以弧度为单位的角度精度,推荐numpy.pi/180
threshod :累加平面的阈值参数,int类型,超过设定阈值才会被检测出线段,值越大,基本上意味着检出的线段越长,检测出的线段个数越少

累计概率 Hough 变换
lines = cv.HoughLinesP(image, rho,theta,threshold[,lines[,minLineLength[,maxLineGap]]])
"""
edges = cv.Canny(thr, 50, 150)
disp_edge = cv.cvtColor(edges, cv.COLOR_GRAY2BGR)
lines = cv.HoughLinesP(edges, 1, 1 * np.pi/180, 10)
for line in lines:
    for x1, y1, x2, y2 in line:
        # 画出直线
        cv.line(disp_edge, (x1, y1), (x2, y2), (0, 255, 0), 1)
    pass

print("line count : ", len(lines))

cv.waitKey()
cv.destroyAllWindows()

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值