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,


磁共振成像(MRI)是一种非侵入性的医学影像技术,通过使用强磁场和无害的无线电波,生成高分辨率的人体内部图像。MRI的基本原理包括磁共振现象和信号处理。 首先,MRI利用强磁场产生静态磁场。这个强磁场使得人体内的原子(通常是氢)的原子核在磁场中定向,使其自旋沿磁场方向预先排列。 其次,MRI利用无线电频率的脉冲来激发人体内的原子核,使其从平衡状态中倾斜。 然后,MRI探测激发后原子核的归位,这个过程称为回波信号。原子核的归位过程会产生微弱的无线电信号。 接下来,MRI系统采集这些回波信号并进行信号处理。信号处理包括一个复杂的计算过程,其基本目标是确定回波信号的来源和特征。通过这种方式,系统可以构建数百个不同方向上的切片图像。 最后,计算机将切片图像组合在一起,形成三维结构,可以帮助医生观察和分析人体内部组织和器官的详细结构。 MRI是一种非常有用的影像技术,因为它能够提供清晰的高对比度图像,并且不涉及使用有害的X射线。它在医学诊断中广泛应用,特别是用于观察脑部、骨骼、关节、脊椎和内脏器官等结构。同时,MRI还可以用来检测肿瘤、损伤、疾病和其他异常,并且具有较高的准确性和灵敏度。 总之,MRI是一种基于磁共振原理的医学影像技术,可生成高分辨率、非侵入性的人体内部图像,对于医学诊断和研究具有重要意义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值