Python与FPGA——图像锐化

news/2024/7/27 8:42:40/文章来源:https://blog.csdn.net/stark_cc/article/details/136512781

文章目录

  • 前言
  • 一、图像锐化
  • 二、Python robert锐化
  • 三、Python sobel锐化
  • 四、Python laplacian锐化
  • 五、FPGA sobel锐化
  • 总结


前言

  在增强图像之前一般会先对图像进行平滑处理以减少或消除噪声,图像的能量主要集中在低频部分,而噪声和图像边缘信息的能量主要集中在高频部分。因此,平滑处理会使原始的图像边缘和轮廓变得模糊。为了减少不利效果的影响,需要利用图像锐化技术。


一、图像锐化

  图像锐化其实就是使用robert,sobel,laplacian这些人发明的窗口,进行图像的处理。图像锐化过程和sobel边缘检测的过程类似,可以移步至《Python与FPGA——sobel边缘检测》课程,一探究竟。

一阶微分的边缘检测
  图像f(x, y)在像素(x, y)梯度的定义为
G = ∂ f ∂ x + ∂ f ∂ y G = \frac{\partial f}{\partial x} + \frac{\partial f}{\partial y} G=xf+yf
也可以用差分来替代微分,即
∂ f ∂ x = f ( i + 1 , j ) − f ( i , j ) \frac{\partial f}{\partial x} = f(i + 1, j) - f(i, j) xf=f(i+1,j)f(i,j)
∂ f ∂ y = f ( i , j + 1 ) − f ( i , j ) \frac{\partial f}{\partial y} = f(i, j + 1) - f(i, j) yf=f(i,j+1)f(i,j)
梯度的幅值即模值,为
∣ G ∣ = ( ∂ f ∂ x ) 2 + ( ∂ f ∂ y ) 2 = [ f ( i + 1 , j ) − f ( i , j ) ] 2 + [ f ( i , j ) − f ( i , j ) ] 2 |G| = \sqrt{(\frac{\partial f}{\partial x})^2 + (\frac{\partial f}{\partial y})^2} = \sqrt{[f(i + 1, j) - f(i, j)]^2 + [f(i, j ) - f(i, j)]^2} G=(xf)2+(yf)2 =[f(i+1,j)f(i,j)]2+[f(i,j)f(i,j)]2
梯度方向为
θ = a r c t a n ( ∂ f ∂ y / ∂ f ∂ x ) = a r c t a n [ f ( i , j + 1 ) − f ( i , j ) f ( i + 1 , j ) − f ( i , j ) ] \theta = arctan(\frac{\partial f}{\partial y}/\frac{\partial f}{\partial x}) = arctan[\frac{f(i, j + 1) - f(i, j)}{f(i + 1, j) - f(i, j)}] θ=arctan(yf/xf)=arctan[f(i+1,j)f(i,j)f(i,j+1)f(i,j)]
图像f(i, j)处的梯度g为
g ( i , j ) = G [ f ( i , j ) ] g(i, j) = G[f(i, j)] g(i,j)=G[f(i,j)]
使用 g ( i , j ) g(i, j) g(i,j)去替代原来的像素。
  一阶导算子有robert算子,perwitt算子,sobel算子。
1. Roberts算子
G x = [ 1 0 0 − 1 ] G y = [ 0 − 1 1 0 ] G_x = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \quad\quad\quad G_y = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} Gx=[1001]Gy=[0110]
2. Prewitt算子
G x = [ − 1 0 1 − 1 0 1 − 1 0 1 ] G y = [ − 1 − 1 − 1 0 0 0 1 1 1 ] G_x = \begin{bmatrix} -1 & 0 & 1\\ -1 & 0 & 1\\ -1 & 0 & 1 \end{bmatrix} \quad\quad\quad G_y = \begin{bmatrix} -1 & -1 & -1\\ 0 & 0 & 0\\ 1 & 1 & 1 \end{bmatrix} Gx=111000111Gy=101101101
3. Sobel算子
G x = [ − 1 0 + 1 − 2 0 + 2 − 1 0 + 1 ] G y = [ + 1 + 2 + 1 0 0 0 − 1 − 2 1 ] G_x = \begin{bmatrix} -1 & 0 & +1\\ -2 & 0 & +2\\ -1 & 0 & +1 \end{bmatrix} \quad\quad\quad G_y = \begin{bmatrix} +1 & +2 & +1\\ 0 & 0 & 0\\ -1 & -2 & 1 \end{bmatrix} Gx=121000+1+2+1Gy=+101+202+101

二阶微分的边缘检测
  二阶微分公式用差分法,推理如下
∂ 2 f ∂ x 2 = 2 f ( x , y ) − f ( x − 1 , y ) − f ( x + 1 , y ) \frac{\partial^2 f}{\partial x^2}=2f(x,y)-f(x-1,y)-f(x+1, y) x22f=2f(x,y)f(x1,y)f(x+1,y)
∂ 2 f ∂ y 2 = 2 f ( x , y ) − f ( x , y − 1 ) − f ( x , y + 1 ) \frac{\partial^2 f}{\partial y^2}=2f(x,y)-f(x,y-1)-f(x, y+1) y22f=2f(x,y)f(x,y1)f(x,y+1)
▽ 2 f = 4 f ( x , y ) − [ f ( x − 1 , y ) + f ( x , y − 1 ) + f ( x , y + 1 ) + f ( x + 1 , y ) ] \triangledown^2f=4f(x,y)-[f(x-1,y)+f(x,y-1)+f(x,y+1)+f(x+1,y)] 2f=4f(x,y)[f(x1,y)+f(x,y1)+f(x,y+1)+f(x+1,y)]
符合二阶微分的算子是laplacian。

G x = [ 0 − 1 0 − 1 4 − 1 0 − 1 0 ] G y = [ − 1 − 1 − 1 − 1 8 − 1 − 1 − 1 − 1 ] G_x = \begin{bmatrix} 0 & -1 & 0\\ -1 & 4 & -1\\ 0 & -1 & 0 \end{bmatrix} \quad\quad\quad G_y = \begin{bmatrix} -1 & -1 & -1\\ -1 & 8 & -1\\ -1 & -1 & -1 \end{bmatrix} Gx=010141010Gy=111181111

二、Python robert锐化

import numpy as np
import matplotlib.pyplot as plt
def image_gray(image):gray = np.dot(image[:, :, ...], [0.299, 0.587, 0.114])#等同0.299 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.114 * image[:, :, 2]return gray.astype(np.uint8)def robert_sharpen(image, gx, gy):h, w = image.shapen, n = gx.shapefiltered_image = np.zeros((h, w))m = int(n / 2)for i in range(m, h - m):for j in range(m, w - m):   gx_value = np.sum(np.multiply(gx, image[i - m: i + m, j - m: j + m]))gy_value = np.sum(np.multiply(gy, image[i - m: i + m, j - m: j + m]))gxy_value = np.sqrt(gx_value ** 2 + gy_value ** 2)filtered_image[i, j] = gxy_valuereturn filtered_image.astype(np.uint8)img = plt.imread("lenna.png")
img = img * 255#图像是[0-1]--->[0-255],确认一下自己的图像是[0-1]还是[0-255]
img = img.astype(np.uint8)
gx = np.array([[1, 0],[0, -1]])
gy = np.array([[0, 1],[-1, 0]])
gray = image_gray(img)
robert_image = robert_sharpen(gray, gx, gy)
fig = plt.figure(figsize=(10, 6))
ax = plt.subplot(1, 2, 1)
ax.set_title("raw image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(gray, cmap="gray")
ax = plt.subplot(1, 2, 2)
ax.set_title("robert image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(robert_image, cmap="gray")

在这里插入图片描述

三、Python sobel锐化

import numpy as np
import matplotlib.pyplot as plt
def image_gray(image):gray = np.dot(image[:, :, ...], [0.299, 0.587, 0.114])#等同0.299 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.114 * image[:, :, 2]return gray.astype(np.uint8)def sobel_sharpen(image, gx, gy):h, w = image.shapen, n = gx.shapefiltered_image = np.zeros((h, w))m = int((n-1) / 2)for i in range(m, h - m):for j in range(m, w - m):   gx_value = np.sum(np.multiply(gx, image[i - m: i + m + 1, j - m: j + m + 1]))gy_value = np.sum(np.multiply(gy, image[i - m: i + m + 1, j - m: j + m + 1]))gxy_value = np.sqrt(gx_value ** 2 + gy_value ** 2)filtered_image[i, j] = gxy_valuereturn filtered_image.astype(np.uint8)img = plt.imread("lenna.png")
img = img * 255#图像是[0-1]--->[0-255],确认一下自己的图像是[0-1]还是[0-255]
img = img.astype(np.uint8)
gx = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])
gy = np.array([[-1, -2, -1],[0, 0, 0],[1, 2, 1]])
gray = image_gray(img)
sobel_image = sobel_sharpen(gray, gx, gy)
fig = plt.figure(figsize=(10, 6))
ax = plt.subplot(1, 2, 1)
ax.set_title("raw image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(gray, cmap="gray")
ax = plt.subplot(1, 2, 2)
ax.set_title("sobel image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(sobel_image, cmap="gray")

在这里插入图片描述

四、Python laplacian锐化

import numpy as np
import matplotlib.pyplot as plt
def image_gray(image):gray = np.dot(image[:, :, ...], [0.299, 0.587, 0.114])#等同0.299 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.114 * image[:, :, 2]return gray.astype(np.uint8)def laplacian_sharpen(image, gx, gy):h, w = image.shapen, n = gx.shapefiltered_image = np.zeros((h, w))m = int((n-1) / 2)for i in range(m, h - m):for j in range(m, w - m):   gx_value = np.sum(np.multiply(gx, image[i - m: i + m + 1, j - m: j + m + 1]))gy_value = np.sum(np.multiply(gy, image[i - m: i + m + 1, j - m: j + m + 1]))gxy_value = np.sqrt(gx_value ** 2 + gy_value ** 2)filtered_image[i, j] = gxy_valuereturn filtered_image.astype(np.uint8)img = plt.imread("lenna.png")
img = img * 255#图像是[0-1]--->[0-255],确认一下自己的图像是[0-1]还是[0-255]
img = img.astype(np.uint8)
gx = np.array([[0, -1, 0],[-1, 4, -1],[0, -1, 0]])
gy = np.array([[-1, -1, -1],[-1, 8, -1],[-1, -1, -1]])
gray = image_gray(img)
sobel_image = sobel_sharpen(gray, gx, gy)
fig = plt.figure(figsize=(10, 6))
ax = plt.subplot(1, 2, 1)
ax.set_title("raw image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(gray, cmap="gray")
ax = plt.subplot(1, 2, 2)
ax.set_title("sobel image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(sobel_image, cmap="gray")

在这里插入图片描述

五、FPGA sobel锐化

//3*3图像
//P11   P12   P13
//P21   P22   P23
//P31   P32   P33//Gx算子
//-1     0     1
//-2     0     2
//-1     0     1
//Gx = -P11 + P13 - 2*P21 + 2*P23 - P31 + P33
//Gx = (P13 - P11) + 2*(P23 - P21) + (P33 - P31)//Gy算子
//1      2     1
//0      0     0
//-1     -2    -1
//Gy = P11 + 2*P12 + P13 - P31 - 2*P32 - P33
//Gy = (P11 - P31) + 2*(P12 - P32) + (P13 - P33)
module  ycbcr_sobel_sharpen
(input	wire			sys_clk		,	//系统时钟,频率为50MHZinput	wire			sys_rst_n	,	//系统复位,低电平有效input	wire			rgb_valid	,	//RGB565图像显示有效信号input	wire	[7:0]	y_data		,	//Y分量input	wire	[11:0]	pixel_x		,	//有效显示区域横坐标input	wire	[11:0]	pixel_y		,	//有效显示区域纵坐标output	reg		[15:0]	sobel_data		//Sobel算法处理后的图像数据
);reg				y_valid		;	//Y分量有效信号
//shift ram
wire	[7:0]	data_row1	;	//移位寄存器第一行数据
wire	[7:0]	data_row2	;	//移位寄存器第二行数据
wire	[7:0]	data_row3	;	//移位寄存器第三行数据
//3*3像素数据,左上角至右下角共9个数据
reg		[7:0]	p11			;	//3*3第1个像素数据
reg		[7:0]	p12			;	//3*3第2个像素数据
reg		[7:0]	p13			;	//3*3第3个像素数据
reg		[7:0]	p21			;	//3*3第4个像素数据
reg		[7:0]	p22			;	//3*3第5个像素数据
reg		[7:0]	p23			;	//3*3第6个像素数据
reg		[7:0]	p31			;	//3*3第7个像素数据
reg		[7:0]	p32			;	//3*3第8个像素数据
reg		[7:0]	p33			;	//3*3第9个像素数据
//Sobel算子
wire	[15:0]	Gx			;	//水平梯度值
wire	[15:0]	Gy			;	//数值梯度值
wire	[7:0]	Gxy			;	//总体梯度值assign  data_row3 = y_data  ;
assign  Gx = (p13 - p11) + 2*(p23 - p21) + (p33 - p31)  ;
assign  Gy = (p11 - p31) + 2*(p12 - p32) + (p13 - p33)  ;//设定第一行、第二行,第一列、第二列显示全白色
always@(*)if((pixel_y == 12'd0)||(pixel_y == 12'd1)||(pixel_x == 12'd2)||(pixel_x == 12'd3))sobel_data = 16'hffff  ;elsesobel_data = {Gxy[7:3],Gxy[7:2],Gxy[7:3]}  ;//锐化核心代码always@(posedge sys_clk or negedge sys_rst_n)if(sys_rst_n == 1'b0)y_valid  <=  1'b0  ;elsey_valid  <=  rgb_valid  ;always@(posedge sys_clk or negedge sys_rst_n)if(sys_rst_n == 1'b0)begin{p11,p12,p13}  <=  24'd0  ;{p21,p22,p23}  <=  24'd0  ;{p31,p32,p33}  <=  24'd0  ;endelse  if(y_valid == 1'b1)begin{p11,p12,p13}  <= {p12,p13,data_row1}  ;{p21,p22,p23}  <= {p22,p23,data_row2}  ;{p31,p32,p33}  <= {p32,p33,data_row3}  ;end	elsebegin{p11,p12,p13}  <=  24'd0  ;{p21,p22,p23}  <=  24'd0  ;{p31,p32,p33}  <=  24'd0  ;end		shift_ram_gen  shift_ram_gen_inst
(.clock 		(sys_clk	),.shiftin	(data_row3	),.shiftout 	(			),.taps0x 	(data_row2	),.taps1x 	(data_row1	)
);sqrt_gen  sqrt_gen_inst 
(.radical	(Gx*Gx + Gy*Gy),.q 			(Gxy	),.remainder 	()
);endmodule

在这里插入图片描述


总结

  图像锐化就到此结束,剩下的交给小伙伴自行实现。Python的prewitt实现;FPGA的robert、prewitt、laplacian算子实现,你都可以尝试。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_997474.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

排序算法——梳理总结

✨冒泡 ✨选择 ✨插入  ✨标准写法  &#x1f3ad;不同写法 ✨希尔排序——标准写法 ✨快排 ✨归并 ✨堆排 ✨冒泡 void Bubble(vector<int>& nums) {// 冒泡排序只能先确定最右边的结果&#xff0c;不能先确定最左边的结果for (int i 0; i < nums.size(); i){…

Spark Core

Spark Core 一、Spark RDD RDD概述 1.RDD基础 2.RDD源代码描述 3.RDD特性 4.Spark宽窄依赖 RDD创建 在驱动器中创建RDD 1.parallelize 读取外部数据集创建RDD 2.textFile RDD操作 缓存rdd到内存 1.RDD转化操作 2.常见的转化操作 3.RDD行动操作 4.常见的行动操作 Spark…

力扣-数组题

1. 两数之和 找出map中是否有target-nums[i]&#xff0c; class Solution { public:vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int, int> hash;for(int i 0 ;i < nums.size(); i){if(hash.find(target - nums[i]) ! hash…

Service Mesh:如何为您的微服务架构带来可靠性和灵活性

在云原生架构中&#xff0c;Service Mesh 技术成为了微服务架构中不可或缺的一环。本文灸哥将和你一起探讨 Service Mesh 技术的原理、功能和实践&#xff0c;帮助架构师和开发人员更好地理解和应用这一关键技术。 1、Service Mesh 技术概述 Service Mesh 又称为服务网格&…

【STM32】HAL库 CubeMX 教程 --- 高级定时器 TIM1 定时

实验目标&#xff1a; 通过CUbeMXHAL&#xff0c;配置TIM1&#xff0c;1s中断一次&#xff0c;闪烁LED。 一、常用型号的TIM时钟频率 1. STM32F103系列&#xff1a; 所有 TIM 的时钟频率都是72MHz&#xff1b;F103C8不带基本定时器&#xff0c;F103RC及以上才带基本定时器。…

代码随想录 回溯算法-分割

目录 131.分割回文串 93.复原IP地址 131.分割回文串 131. 分割回文串 中等 给你一个字符串 s&#xff0c;请你将 s 分割成一些子串&#xff0c;使每个子串都是 回文串 。返回 s 所有可能的分割方案。 回文串 是正着读和反着读都一样的字符串。 示例 1&#xff1a; 输…

【PHP+代码审计】PHP基础——变量和常量的定义和使用

&#x1f36c; 博主介绍&#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 hacker-routing &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【应急响应】 【Java、PHP】 【VulnHub靶场复现】【面试分析】 &#x1f389;点赞➕评论➕收…

【算法沉淀】刷题笔记:并查集 带权并查集+实战讲解

&#x1f389;&#x1f389;欢迎光临&#x1f389;&#x1f389; &#x1f3c5;我是苏泽&#xff0c;一位对技术充满热情的探索者和分享者。&#x1f680;&#x1f680; &#x1f31f;特别推荐给大家我的最新专栏《数据结构与算法&#xff1a;初学者入门指南》&#x1f4d8;&am…

使用modinfo对比内核版本号

加载内核&#xff0c;出现版本不一样 cat /proc/verison查看内核板本 模块版本&#xff1a;显示模块的版本号。 $ modinfo [OPTIONS] [MODULE] 参数说明-F, --field <field>: 指定要显示的字段&#xff0c;可以使用逗号分隔多个字段。-k, --kernel <kernel>: 指定…

如何解决微服务的数据一致性分发问题?

介绍 系统架构微服务化以后,根据微服务独立数据源的思想,每个微服务一般具有各自独立的数据源,但是不同微服务之间难免需要通过数据分发来共享一些数据,这个就是微服务的数据分发问题。Netflix/Airbnb等一线互联网公司的实践[参考附录1/2/3]表明,数据一致性分发能力,是构…

OpenHarmony教程指南—ArkUI中组件、通用、动画、全局方法的集合

介绍 本示例为ArkUI中组件、通用、动画、全局方法的集合。 本示例使用 Tabs容器组件搭建整体应用框架&#xff0c;每个 TabContent内容视图 使用 div容器组件 嵌套布局&#xff0c;在每个 div 中使用 循环渲染 加载此分类下分类导航数据&#xff0c;底部导航菜单使用 TabCont…

基于springboot+vue的企业员工薪酬关系系统

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

【Spring底层原理高级进阶】Spring Batch清洗和转换数据,一键处理繁杂数据!Spring Batch是如何实现IO流优化的?本文详解!

&#x1f389;&#x1f389;欢迎光临&#xff0c;终于等到你啦&#x1f389;&#x1f389; &#x1f3c5;我是苏泽&#xff0c;一位对技术充满热情的探索者和分享者。&#x1f680;&#x1f680; &#x1f31f;持续更新的专栏《Spring 狂野之旅&#xff1a;从入门到入魔》 &a…

猫毛过敏又不想扔掉猫怎么办?如何养猫?热门宠物空气净化器分享

养了猫咪一年多&#xff0c;忽然发现自己患上了过敏性鼻炎和结膜炎&#xff0c;就是那种一靠近猫咪就会不断打喷嚏、流鼻涕、流眼泪的症状。有时候还会感到眼睛发痒&#xff0c;发红。有没有什么好的方法治疗过敏性鼻炎呢&#xff1f; 医生建议&#xff0c;从根本上解决问题需…

【C++ 编程指南】

C 编程指南 ■ C环境安装■ C 基本语法■ 预定义宏■ # 和 ## 运算符■ C 引用■ C 命名空间■ 定义命名空间■ using 指令■ 嵌套的命名空间 ■ String类■ 类■ 类的static静态成员 ■ C 继承■ 继承类型 public、protected 或 private■ 访问控制和继承■ 多继承■ 数据抽象…

微信小程序-生命周期

页面生命周期 onLoad: 页面加载时触发的方法&#xff0c;在这个方法中可以进行页面初始化的操作&#xff0c;如获取数据、设置页面状态等。 onShow: 页面显示时触发的方法&#xff0c;在用户进入页面或从其他页面返回该页面时会调用此方法。可以在此方法中进行页面数据刷新、动…

医药行业五大难题深度剖析:CRM解决方案助力突围

医疗行业关系着民生、经济乃至战备&#xff0c;是国民经济的重要组成部分。虽然近20年来我国医疗行业年均增长率维持在15%之上&#xff0c;但行业发展仍存在诸多问题。引进CRM管理系统可能是一个行之有效的解决方法。文中将为您整理医疗行业目前的五大挑战&#xff0c;以及CRM如…

MPLS(多协议标签交换)-基础原理与配置

标签交换--包交换&#xff08;路由数据传递) 数据包在进入到的 MPLS 的域内后.转发该数据包时&#xff0c;最初在包交换仅支持原始交换时 将在第2层和3层中间压入标签号&#xff0c;使得域内的路由器在基于 2.5 层的标签号仅需要查询本地的一张(LFIB 表(标签转发信息数据库) 标…

01背包问题 刷题笔记

思路 dp 用f[i][j]来表示当体积为j时 考虑前i件物品可以获得的 最大值 记住f[i][j]本身是个价“价值” 考虑两种状态 是否将第i件物品放入背包里面 将背包的体积从小到大递增来进行考虑 首先 考虑条件 如果当前增加的体积放不下下一件物品 则该体积 可以获得的最大值可以直接…

c++复习

基础 内存分区 栈&#xff1a; 存放函数的局部变量、函数参数、返回地址等&#xff0c;由编译器自动分配和释放。 堆&#xff1a; 动态申请的内存空间&#xff0c;就是由 malloc 分配的内存块&#xff0c;由程序员控制它的分配和释放&#xff0c;如果程序执行结束还没有释放…