图像基本处理

给图像添加噪声

img = cv2.imread('timg.jpg', cv2.IMREAD_UNCHANGED)
img_noise = img
cv2.imshow("src", img)
rows, cols, chn = img_noise.shape
for i in range(5000):
    x = np.random.randint(0, rows)
    y = np.random.randint(0, cols)
    img_noise[x, y, :] = 255

cv2.imshow("noise", img_noise)

# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存含噪声图像
cv2.imwrite("zxp_noise.jpg", img_noise)

均值滤波

均值滤波是指任意一点的像素值,都是周围 N \times M 个像素值的均值。

模板匹配

method=['cv2.TM_CCOEFF','cv2.TM_CCORR','cv2.TM_CCOEFF_NORMED','cv2.TM_CCORR_NORMED','cv2.TM_SQDIFF_NORMED']
img=cv2.imread('timg.jpg')
img0=cv2.imread('ww.png')
for meth in method:
    method1=eval(meth)
    print(method1)
    res=cv2.matchTemplate(img,img0,method1)
    h,w = img0.shape[:2]
    minral,maxral,minloc,maxloc=cv2.minMaxLoc(res)
    if meth in ['cv2.TM_CCOEFF','cv2.TM_SQDIFF_NORMED']:
        top_left=minloc
    else:
        top_left=maxloc
    bottom_right=(top_left[0]+w,top_left[1]+h)
    cv2.rectangle(img,top_left,bottom_right,255,2)
    plt.subplot(121)
    plt.imshow(res,cmap='gray')
    plt.xticks([])
    plt.yticks([])
    plt.subplot(122)
    plt.imshow(img, cmap='gray')
    plt.xticks([])
    plt.yticks([])
    plt.suptitle(meth)
    plt.show()
method=['cv2.TM_CCOEFF','cv2.TM_CCORR','cv2.TM_CCOEFF_NORMED','cv2.TM_CCORR_NORMED','cv2.TM_SQDIFF_NORMED']
img=cv2.imread('timg.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img0=cv2.imread('ww.png')
h,w=img0.shape[:2]
# for meth in method:
#     method1=eval(meth)
#     print(method1)
res=cv2.matchTemplate(gray,img0,cv2.TM_SQDIFF_NORMED)
print(res)  #【A-a+1】 【B-b+1】
thresh=0.8
loc=np.where(res>=thresh)
for ph in zip(*loc[::-1]):
    bottom_right=(ph[0]+w,ph[1]+h)
    cv2.rectangle(img,ph, bottom_right, (0,0,255), 2)
cv2.imshow('qw',img)
cv2.waitKey(0)

直方图

img=cv2.imread('timg.jpg')
color=['b','g','r']
print('haha')
for i,col in enumerate(color):
   hist=cv2.calcHist([img],[i],None,[256],[0,256])
   print(hist)
   plt.plot(hist,color=col)
   plt.xlim([0,256])
plt.show()
img=cv2.imread('23.png')
gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_float=np.float32(gray)
dft = cv2.dft(img_float, flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift=np.fft.fftshift(dft)#将低频移动到图像中心
magnitude=20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
#将实部和虚部转化为实部
plt.subplot(121)
plt.imshow(img,'gray')
plt.title('img')
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(magnitude,'gray')
plt.title('ma')
plt.xticks([])
plt.yticks([])
plt.show()

只保留低频

1.读取图片
2.np.float32进行类型转换
3.使用cv2.dft进行傅里叶变换
4.使用np.fft.ffshift将低频部分转换到图像的中心
5.构造掩模,使得淹模的中心位置为1,边缘位置为0
6.将掩模与傅里叶变换后的图像结合,只保留中心部分的低频位置
7.使用np.fft.iffshift将低频部分转移回到原先的位置
8.使用cv2.idft进行傅里叶的反转换
9.使用cv2.magnitude将图像的实部和虚部转换为空间域内
10.进行作图操作

img=cv2.imread('23.png',0)
img_float=np.float32(img)
dft_image=cv2.dft(img_float,flags=cv2.DFT_COMPLEX_OUTPUT)
dft_center=np.fft.fftshift(dft_image)
crow, ccol = int(img.shape[0] / 2), int(img.shape[1] / 2) # 求得图像的中心点位置
mask = np.zeros((img.shape[0], img.shape[1], 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
newimage = dft_center * mask
img_idf=np.fft.ifftshift(newimage)
ing=cv2.idft(img_idf)
new=cv2.magnitude(ing[:,:,0],ing[:,:,1])
plt.subplot(121)
plt.imshow(img,'gray')
plt.subplot(122)
plt.imshow(new,'gray')
plt.show()

保留高频部分

# 第一步读入图片
img = cv2.imread('lena.jpg', 0)
# 第二步:进行数据类型转换
img_float = np.float32(img)
# 第三步:使用cv2.dft进行傅里叶变化
dft = cv2.dft(img_float, flags=cv2.DFT_COMPLEX_OUTPUT)
# 第四步:使用np.fft.fftshift将低频转移到图像中心
dft_center = np.fft.fftshift(dft)
# 第五步:定义掩模:生成的掩模中间为0周围为1
crow, ccol = int(img.shape[0] / 2), int(img.shape[1] / 2) # 求得图像的中心点位置
mask = np.ones((img.shape[0], img.shape[1], 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0

# 第六步:将掩模与傅里叶变化后图像相乘,保留中间部分
mask_img = dft_center * mask

# 第七步:使用np.fft.ifftshift(将低频移动到原来的位置
img_idf = np.fft.ifftshift(mask_img)

# 第八步:使用cv2.idft进行傅里叶的反变化
img_idf = cv2.idft(img_idf)

# 第九步:使用cv2.magnitude转化为空间域内
img_idf = cv2.magnitude(img_idf[:, :, 0], img_idf[:, :, 1])

# 第十步:进行绘图操作
plt.subplot(121)
plt.imshow(img, cmap='gray')
plt.subplot(122)
plt.imshow(img_idf, cmap='gray')
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值