The Basics Of Image Filtering

本文由作者Feymour/JapoTek撰写,旨在介绍图像过滤的基本概念与实践,包括滤波器的工作原理、不同类型的滤波器及其应用示例。通过简单的伪代码和实例,帮助读者理解并入门图像处理技术。

from:http://www.hugi.scene.org/online/coding/hugi%2016%20-%20cofilter.htm


The Basics Of Image Filtering

Feymour/JapoTek

Introduction

Hiya, readers! I've spent the last year working on bitmap images and image filtering, now I have the presumption to say that I've a good knowledge of the matter and I think it's time for you to become acquainted with this nice subject of Computer Graphics. In my humble opinion, Image Filtering offers quite a large amount of subjects to investigate on, in this little text I will only introduce you to this topic and help you to do the first step.

I will not begin from scratch, I assume you all know what an image is, what pixels are, a good knowledge of statistics and know how to work with bitmaps (at least how to blend two colours together, change the colours of a pixel, reach the [x;y] in a image and so on..., all basic stuff IMHO).

Anyway, I don't think this is advanced coding stuff, neither is it difficult or "esoteric": it's pretty basic and well-known by all the "experienced" coders, but I think it could be useful for the newbies of digital image processing.

What is a filter and different kinds of them

With the word "filter" I mean an operation on a bidimensional image that alters the colours or the positions of pixels. Starting from this brief definition we could seperate filters into three big groups:

        - Filters that work on the colour of pixels
        - Filters that work on the position of pixels
        - A mix of the two

To help you understand these categories I will give you some examples. Blur (smooth the image removing some noise, just like an unfocused photograph), sharpen (exalt the contour of the image), emboss (give a three dimensional feeling to the image) and so on, fit perfectly into the first category; pinch, punch, wave, twirl, ripple and those kind of operations are in the second one.

In this minimal little primer on image filtering I will explain only the first family and in particular the so called "matrix" filter, but there are tons of other kinds and other methods of filtering that I can talk about, if you really care about them.

The weighted filter

The weighted filter (also called, in a less accurate way, "matrix" filters) is a really common technique used in image manipulation, most people use it without knowing that is a "standard" method, i.e. more and more coders do a blur (smoothing) routine in the following fashion:

        for ( each pixel )
                current_pixel_colour = average of surrounding pixels' colours

Well, in fact this is nothing more than a optimized and less flexible version of a weighted filter. It is often called a "matrix" filter because it needs a table of numbers, similar to a matrix. Let's see how it works.

We have a table of numbers (could be of any size, square or rectangular, but the most common sizes are 3x3 or 5x5), a division factor and a bias value. Now we must "center" the values' grid on the current pixel (i.e. in a 3x3 grid the value at 2,2 corresponds to the pixel at the current position), and now we must multiply the value in the grid with the corresponding pixel on the image, do this for each pixel, and add them together. Ok, divide the result by the division factor and add the bias value. Now we can store the final pixel value in a new image (we must work on two different images!) at position [x,y]. As you can see, this is quite an heavy task, and it's really slow for using it in realtime applications, but starting from a weighted filter you can always get to a particular case and get rid of all the useless operations. (Using a 5x5 filter on a image we have to do 5*5=25 multiplications + 1 division + 25 adds + 1 add - the bias value - for each pixel or component if you're working in a hicolor-truecolor mode! But using an optimized version for a particular case we could always choose the right values and proportion which could help in the pursuit of speed and realtime applications of the filter.)

In a nutshell, the weighted filter works in this way: the numbers in the table are the weight of each colour in the final result, the higher they are higher the importance of the corresponding pixel colour in the final mixed colour.

From a statistical point of view we are doing a weighted average of colours, in fact often the division factor is a sum of the single weights. I hope you understand my point. Every pixel has a weight in the final mixing that is:

                FinalPixelWeight = PixelWeight / DivisionFactor

If the division factor is greater than the sum of the weights the image will get darker and viceversa. The bias value is a brightness correction factor, in some filters (i.e.: blur) the images tends to get darker, using a small bias value you will maintain the brightness of the initial image.

So, simple? Uh?

Some pseudocode

The following pseudo(C)code is of course untested, unoptimized and it doesn't worry about memory boundaries, data types and image colour space or pixel format. I think you'll be enough clever to code it by yourself.

        for (each pixel in the image)
        {
            gridCounter=0;
            final = 0;

            for(y2=-2; y2<=2; y2++)       // and for each pixel around our
                       for(x2=-2; x2<=2; x2++)    //  current pixel
                   {
                          // add to our total
                  final += source[x+x2][y+y2] * filterTable[gridCounter];
                  // go to the next value on the filter table
                      gridCounter++;
               }

            final /= divisionFactor;
                        final += biasValue;

            destination[x][y] = final;
                }

It's pretty much like that.

Example weighted filters

There are some examples of weighted filters values, I use a 5x5 grid but in some cases also a 3x3 table will do the work.

Smoothing filters:

        Soften (medium)

                0 0 0 0 0
                0 1 3 1 0
                0 3 9 3 0
                0 1 3 1 0       bias:   1
                0 0 0 0 0       divide: 25

        Soften (a lot)

                1 1 1 1 1
                1 1 1 1 1
                1 1 1 1 1
                1 1 1 1 1       bias: 0
                1 1 1 1 1       divide: 25

        Soften (a little)

                0  1  2  1  0
                1  3  10 3  1
                2  10 90 10 2
                1  3  10 3  1   bias:   1
                0  1  2  1  0   divide: maybe 154

Sharpen filters:

        Sharpen (low) [negative values are useful for creating contrast]

                 0  0  0  0  0
                 0 -1 -3 -1  0
                 0 -3 41 -3  0
                 0 -1 -3 -1  0  bias:    0
                 0  0  0  0  0  divide: 25

        Sharpen (medium)

                -1 -1 -1 -1 -1
                -1 -1 -1 -1 -1
                -1 -1 49 -1 -1
                -1 -1 -1 -1 -1  bias:    0
                -1 -1 -1 -1 -1  divide: 25

Emboss filter:

                 0 -1  0
                 1  1 -1    bias:   0
                 0  1  0    divide: 1

(You can change the direction by switching signs and the strength by increasing values.)

Last words Ok, I think that's enough for now. I hope you liked this text and learned something, excuse me if I didn't explain in a clear enough way or if my pseudo code doesn't run. I apologize for every inconvenience.

I wanna make it clear that this is only a little portion of the huge field of image filtering, I can't cover every topic and I think I can't even learn every little aspect of it. But I hope to have introduced you to this interesting matter.

Now, give a ring to your girlfriend, bring a good bottle of Italian wine and make love until tomorrow morning.

Life is too short to code image filtering routines, there's Photoshop, isn't there?

Best wishes,


内容概要:本文详细介绍了一个基于Java和Vue的联邦学习隐私保护推荐系统的设计与实现。系统采用联邦学习架构,使用户数据在本地完成模型训练,仅上传加密后的模型参数或梯度,通过中心服务器进行联邦平均聚合,从而实现数据隐私保护与协同建模的双重目标。项目涵盖完整的系统架构设计,包括本地模型训练、中心参数聚合、安全通信、前后端解耦、推荐算法插件化等模块,并结合差分隐私与同态加密等技术强化安全性。同时,系统通过Vue前端实现用户行为采集与个性化推荐展示,Java后端支撑高并发服务与日志处理,形成“本地训练—参数上传—全局聚合—模型下发—个性化微调”的完整闭环。文中还提供了关键模块的代码示例,如特征提取、模型聚合、加密上传等,增强了项目的可实施性与工程参考价值。 适合人群:具备一定Java和Vue开发基础,熟悉Spring Boot、RESTful API、分布式系统或机器学习相关技术,从事推荐系统、隐私计算或全栈开发方向的研发人员。 使用场景及目标:①学习联邦学习在推荐系统中的工程落地方法;②掌握隐私保护机制(如加密传输、差分隐私)与模型聚合技术的集成;③构建高安全、可扩展的分布式推荐系统原型;④实现前后端协同的个性化推荐闭环系统。 阅读建议:建议结合代码示例深入理解联邦学习流程,重点关注本地训练与全局聚合的协同逻辑,同时可基于项目架构进行算法替换与功能扩展,适用于科研验证与工业级系统原型开发。
源码来自:https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
磁共振成像(MRI)是一种非侵入性的医学影像技术,通过使用强磁场和无害的无线电波,生成高分辨率的人体内部图像。MRI的基本原理包括磁共振现象和信号处理。 首先,MRI利用强磁场产生静态磁场。这个强磁场使得人体内的原子(通常是氢)的原子核在磁场中定向,使其自旋沿磁场方向预先排列。 其次,MRI利用无线电频率的脉冲来激发人体内的原子核,使其从平衡状态中倾斜。 然后,MRI探测激发后原子核的归位,这个过程称为回波信号。原子核的归位过程会产生微弱的无线电信号。 接下来,MRI系统采集这些回波信号并进行信号处理。信号处理包括一个复杂的计算过程,其基本目标是确定回波信号的来源和特征。通过这种方式,系统可以构建数百个不同方向上的切片图像。 最后,计算机将切片图像组合在一起,形成三维结构,可以帮助医生观察和分析人体内部组织和器官的详细结构。 MRI是一种非常有用的影像技术,因为它能够提供清晰的高对比度图像,并且不涉及使用有害的X射线。它在医学诊断中广泛应用,特别是用于观察脑部、骨骼、关节、脊椎和内脏器官等结构。同时,MRI还可以用来检测肿瘤、损伤、疾病和其他异常,并且具有较高的准确性和灵敏度。 总之,MRI是一种基于磁共振原理的医学影像技术,可生成高分辨率、非侵入性的人体内部图像,对于医学诊断和研究具有重要意义。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值