python转换rgb图像成灰阶图

>>> import sys
>>> import os
>>> import math
>>> import numpy as np
>>> from scipy import ndimage as ndi
>>> from skimage.transform import hough_line, hough_line_peaks
>>> from scipy.ndimage import gaussian_filter
>>> from PIL import Image
>>> from matplotlib import pyplot as plt
>>> def get_size( my_image ):
	img = Image.open(my_image)
	x,y = img.size
	return (x,y)
>>> def create_grayscale( my_image ):
	grayscale = np.array(Image.open( my_image ).convert('L'))
	return grayscale
>>> def show_grayscale( my_image ):
	plt.figure(figsize = (10,10))
	plt.imshow(my_image, cmap = plt.get_cmap(name = 'gray'))
	plt.show()
>>> def LoG (my_image, sigma):
	A = gaussian_filter(grayscale, sigma=math.sqrt(sigma)/math.sqrt(2))
	B = gaussian_filter(grayscale, sigma=math.sqrt(sigma)*math.sqrt(2))
	return np.subtract(A,B)
>>> def Gradient( pixel_arr ):
	padded_arr = np.pad(pixel_arr,1)
	Sx_prewitt = [[-1,-1,-1],[0,0,0],[1,1,1]]
	Sy_prewitt = [[-1,0,1],[-1,0,1],[-1,0,1]]
	Gx = []
	Gy = []
	for i in range(len(pixel_arr)):
		row_x = []
		row_y = []
		for j in range(len(pixel_arr[0])):
			neigh_matrix = padded_arr[i:i+3,j:j+3]
			g_x = np.multiply(neigh_matrix,Sx_prewitt)
>>> def Gradient( pixel_arr ):
	padded_arr = np.pad(pixel_arr,1)
	Sx_prewitt = [[-1,-1,-1],[0,0,0],[1,1,1]]
	Sy_prewitt = [[-1,0,1],[-1,0,1],[-1,0,1]]
	Gx = []
	Gy = []
	for i in range(len(pixel_arr)):
		row_x = []
		row_y = []
		for j in range(len(pixel_arr[0])):
			neigh_matrix = padded_arr[i:i+3,j:j+3]
			g_x = np.multiply(neigh_matrix,Sx_prewitt)
			g_y = np.multiply(neigh_matrix,Sy_prewitt)
			sum_x = np.sum(g_x)
			sum_y = np.sum(g_y)
			row_x.append(sum_x)
			row_y.append(sum_y)
	Gx.append(row_x)
	Gy.append(row_y)
	Gx2 = np.multiply(Gx,Gx)
	Gy2 = np.multiply(Gy,Gy)
	sum_Gx2 = Gx2 + Gy2
	gradient_mag = np.sqrt(sum_Gx2)
	print(gradient_mag)
	gradient_mag[gradient_mag < 60] = 0
	edge_img = Image.fromarray(gradient_mag)
	show_grayscale(edge_img)
	return edge_img
>>> def generate_Hough_Transform(my_image):
	h, theta, d = hough_line(my_image)
	fig, axes = plt.subplots(1, 3, figsize=(15, 6))
	ax = axes.ravel()
	ax[0].imshow(my_image, cmap = plt.get_cmap(name = 'gray'))
	ax[0].set_title('Input image')
	ax[0].set_axis_off()
	ax[1].imshow(np.log(1 + h),
		     extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]), d[-1], d[0]],
		     cmap = plt.get_cmap(name = 'gray'), aspect=1/1.5)
	ax[1].set_title('Hough transform')
	ax[1].set_xlabel('Angles (degrees)')
	ax[1].set_ylabel('Distance (pixels)')
	ax[1].axis('on')
	ax[2].imshow(my_image, cmap = plt.get_cmap(name = 'gray'))
	origin = np.array((0, grayscale.shape[1]))
	for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
		y0, y1 = (dist - origin * np.cos(angle)) / np.sin(angle)
		ax[2].plot(origin, (y0, y1), '-r')
	ax[2].set_xlim(origin)
	ax[2].set_ylim((my_image.shape[0], 0))
	ax[2].set_axis_off()
	ax[2].set_title('Detected lines')
	plt.tight_layout()
	plt.show()
>>> grayscale = create_grayscale('/Users/spurs/Desktop/IMG_2211.jpeg')
>>> show_grayscale(grayscale)
>>> show_grayscale(grayscale)
>>> (size_x, size_y) =  get_size('/Users/spurs/Desktop/IMG_2211.jpeg')
>>> (size_x, size_y) =  get_size('/Users/spurs/Desktop/myselfgray.png')
>>> LoG_Image = LoG( grayscale, sigma=5)
>>> generate_Hough_Transform(LoG_Image)
>>> grayscale = create_grayscale('/Users/spurs/Desktop/IMG_2211.jpeg')
>>> show_grayscale(grayscale)
>>> (size_x, size_y) =  get_size('corner_window.jpg')

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GOD FOR JAVA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值