第3章作业

  1. 旋转45°
import cv2
import numpy as np

img = cv2.imread("demo.png",0)
# 原图的高、宽
h, w = img.shape[:2]


A3 = cv2.getRotationMatrix2D((w / 2.0, h / 2.0), -45, 1)
Image = cv2.warpAffine(img, A3, (w, h), borderValue=0,flags=cv2.INTER_AREA)

ImageWARP_INVERSE_MAP  = cv2.warpAffine(img, A3, (w, h), borderValue=0,flags=cv2.WARP_INVERSE_MAP  )
ImageINTER_LINEAR = cv2.warpAffine(img, A3, (w, h), borderValue=0,flags=cv2.INTER_LINEAR )
ImageINTER_CUBIC  = cv2.warpAffine(img, A3, (w, h), borderValue=0,flags=cv2.INTER_CUBIC  )
ImageINTER_AREA = cv2.warpAffine(img, A3, (w, h), borderValue=0,flags=cv2.INTER_AREA )

# 如果要选择插值的方法可以通过参数flags设置,如flags=cv.INTER_CUBIC

cv2.namedWindow('imgRotation', cv2.WINDOW_NORMAL)

cv2.imshow('imgRotation',Image)

cv2.waitKey(0)
cv2.destroyAllWindows()

| 原图
在这里插入图片描述

|旋转插值再处理在这里插入图片描述
|
2. 错切45°

import math
import cv2
import numpy as np


img = cv2.imread("demo.png",0)
cv2.imshow("origin", img)
h, w = img.shape[0], img.shape[1]
origin_coord = np.array([[0, 0, 1], [h, 0, 1], [h, w, 1], [0, w, 1]])

theta = 45  # shear角度
tan = math.tan(math.radians(theta))

# x方向错切
m = np.eye(3)
m[0,1] = tan
shear_coord = (m @ origin_coord.T).T.astype(np.int)
shear_img = cv2.warpAffine(src=img, M=m[:2],
                           dsize=(np.max(shear_coord[:, 0]), np.max(shear_coord[:, 1])),
                           borderValue=0,
                           flags=cv2.INTER_CUBIC)

cv2.namedWindow('shear_x', cv2.WINDOW_NORMAL)

cv2.imshow('shear_x',shear_img)

cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(0)

在这里插入图片描述
在这里插入图片描述

**

习题-2

**
若希望通过非线性点运算降低较亮或较暗物体的 对比度,从而加强灰度级处于中间范围物体的对比 度:

  • 相应的非线性点运算函数应如何构成?
  • 试编写达到上述要求的函数并给出其输入输出曲线;
  • 编写python程序,对实际图像进行处理观看效果。

1、构建的非线性点运算函数应包括一下的特点:
中间部分斜率大于1.从而加强灰度级处于中间范围物体的对比度
两端处斜率小于1,以降低较暗或较l亮物体的对比度
下面的函数就可以胜任这个任务

2、构建的函数 及输入输出如图所示:在这里插入图片描述
3、python

import cv2

import numpy as np



image = cv2.imread('im.png')
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 在灰度图进行分段线性对比度拉伸
# 此种方式变换函数把灰度级由原来的线性拉伸到整个范围[0, 255]
r_min, r_max = 255, 0
for i in range(gray_img.shape[0]):
    for j in range(gray_img.shape[1]):
        if gray_img[i, j] > r_max:
            r_max = gray_img[i, j]
        if gray_img[i, j] < r_min:
            r_min = gray_img[i, j]
r1, s1 = r_min+40, 10
r2, s2 = r_max-40, 255-10

precewise_img = np.zeros((gray_img.shape[0], gray_img.shape[1]), dtype=np.uint8)
k1 = s1 / r1
k3 = (255 - s2) / (255 - r2)
k2 = (s2 - s1) / (r2 - r1)
for i in range(gray_img.shape[0]):
    for j in range(gray_img.shape[1]):
        if r1 <= gray_img[i, j] <= r2:
            precewise_img[i, j] = k2 * (gray_img[i, j] - r1)
        elif gray_img[i, j] < r1:
            precewise_img[i, j] = k1 * gray_img[i, j]
        elif gray_img[i, j] > r2:
            precewise_img[i, j] = k3 * (gray_img[i, j] - r2)

cv2.imshow('origin image', image)
cv2.imshow('gray image', gray_img)
cv2.imshow('precewise image', precewise_img)
if cv2.waitKey(0) == 27:
    cv2.destroyAllWindows()



效果图
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值