在学习数字图像处理时,书上介绍的都是理论,有时候光看理论有点儿枯燥,那就动手实现一下呗,自己以前也没有用过opencv,也是边学边练吧
源图像在本书官网下载 http://www.imageprocessingplace.com/DIP-3E/dip3e_book_images_downloads.htm
这个比较简单,实现了一下图像裁剪和图像反转
import cv2 import numpy as np img = cv2.imread("1.tif",0) ‘‘‘ edges = cv2.Canny(img,50,150) cv2.imshow(‘img‘,img)#这一行显示错误,可能是因为路径有英文 #放狗屁,图像名称弄错了 cv2.imshow(‘edges‘,edges)‘‘‘ #裁剪图像 m,n = np.shape(img) model = np.zeros((m,n)) model[int(m/4):int(3*m/4),int(n/4):int(3*n/4) ] = 1 img_cut = np.multiply(img,model) cv2.imshow(‘img_cut‘,img_cut.astype(np.uint8))#img_cut.astype(np.uint8)变换类型,保证图像正常显示 cv2.imshow(‘img‘,img) #cv2.imwrite(‘1_cut.tif‘,img_cut) #反转图像 img_reversion = 255-img cv2.imshow(‘img_reversion‘,img_reversion) #print(np.max(img)) cv2.waitKey(0) cv2.destroyAllWindows()
图像路径必须为英文
import cv2 import numpy as np img = cv2.imread("1.tif",0) cv2.imshow("img",img) #幂律变换,c = 1,r = 0.6,注意变换前需要将灰度变换到0—1,这样才能经过点(1,1),和图像中的(L-1,L-1)对应 img_max = np.max(img) img_0_1 = img/img_max #r = 0.6 r_0_6 = img_0_1**0.6 r_0_6 = r_0_6*img_max int_r_0_6 = r_0_6.astype(np.uint8)#把格式由浮点型转化为整型才能显示正确 cv2.imshow("r = 0.6",int_r_0_6) cv2.imwrite("r0.6.tif",int_r_0_6) #r = 0.4 r_0_4 = img_0_1**0.4 r_0_4 = r_0_4*img_max int_r_0_4 = r_0_4.astype(np.uint8)#把格式由浮点型转化为整型才能显示正确 cv2.imshow("r = 0.4",int_r_0_4) cv2.imwrite("r0.4.tif",int_r_0_4) #r = 0.3 r_0_3 = img_0_1**0.3 r_0_3 = r_0_3*img_max int_r_0_3 = r_0_3.astype(np.uint8)#把格式由浮点型转化为整型才能显示正确 cv2.imshow("r = 0.3",int_r_0_3) cv2.imwrite("r0.3.tif",int_r_0_3) cv2.waitKey(0) cv2.destroyAllWindows()
1、进行幂律变换前需要将灰度变换到0-1的范围内,
2、计算后的像素矩阵是浮点型,将其转化为整型才能正常显示
原文:https://www.cnblogs.com/jingxin-gewu/p/13300395.html