计算图像的局部三元模式? [英] Calculating the Local Ternary Pattern of an image?

查看:310
本文介绍了计算图像的局部三元模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算图像的局部三元模式。我的代码如下。我是否朝着正确的方向前进?

I am calculating the Local Ternary Pattern of an image. My code is given below. Am I going in the right direction or not?

function [ I3 ] = LTP(I2)
     m=size(I2,1);
     n=size(I2,2);
    for i=2:m-1
        for j=2:n-1
            J0=I2(i,j);
            I3(i-1,j-1)=I2(i-1,j-1)>J0;
        end
    end

I2 是LTP应用的图像。

I2 is the image LTP is applied to.

推荐答案

这不太正确。以下是给出3 x 3图像补丁和阈值 t 的LTP示例:

This isn't quite correct. Here's an example of LTP given a 3 x 3 image patch and a threshold t:

您指定的范围窗口中的像素为0表示阈值在 c - t c + t 之间,其中<​​code> c 是像素的中心强度。因此,因为此窗口中心的强度为34,所以范围介于 [29,39] 之间。任何超出 39 的值都会被赋值为1,而任何低于 29 的值都会被赋值-1。确定三元代码后,将代码分为上下两种模式。基本上,任何被赋值为-1的值都被赋值为0用于高级模式,而任何被赋值为-1的值都被赋予1用于较低模式。此外,对于较低的模式,原始窗口中任何1的值都会映射到0.最后的模式是从东部位置开始相对于中心(第2行,第3列)读取位模式,然后四处走动逆时针。因此,您可能应该修改您的功能,以便在图像中输出较低模式和较高模式。

The range that you assign a pixel in a window to 0 is when the threshold is between c - t and c + t, where c is the centre intensity of the pixel. Therefore, because the intensity is 34 in the centre of this window, the range is between [29,39]. Any values that are beyond 39 get assigned 1 and any values that are below 29 get assigned -1. Once you determine the ternary codes, you split up the codes into upper and lower patterns. Basically, any values that get assigned a -1 get assigned 0 for upper patterns and any values that get assigned a -1 get assigned 1 for lower patterns. Also, for the lower pattern, any values that are 1 from the original window get mapped to 0. The final pattern is reading the bit pattern starting from the east location with respect to the centre (row 2, column 3), then going around counter-clockwise. Therefore, you should probably modify your function so that you're outputting both lower patterns and upper patterns in your image.

让我们编写代码的更正版本。请记住,我不会给出优化版本。让我们得到一个基本算法,并且由你决定如何优化它。因此,请将您的代码更改为类似的内容,同时记住我上面谈到的所有内容。顺便说一下,你的功能没有正确定义。您不能使用空格来定义函数以及变量。它会将空格中的每个单词解释为变量或函数,而这不是你想要的。假设您的社区大小为3 x 3且图像为灰度,请尝试以下操作:

Let's write the corrected version of your code. Bear in mind that I will not give an optimized version. Let's get a basic algorithm working, and it'll be up to you on how you want to optimize this. As such, change your code to something like this, bearing in mind all of the stuff I talked about above. BTW, your function is not defined properly. You can't use spaces to define your function, as well as your variables. It will interpret each word in between spaces as variables or functions, and that's not what you want. Assuming your neighbourhood size is 3 x 3 and your image is grayscale, try something like this:

function [ ltp_upper, ltp_lower ] = LTP(im, t)

    %// Get the dimensions
    rows=size(im,1);
    cols=size(im,2);

    %// Reordering vector - Essentially for getting binary strings
    reorder_vector = [8 7 4 1 2 3 6 9];

    %// For the upper and lower LTP patterns
    ltp_upper = zeros(size(im));
    ltp_lower = zeros(size(im));

    %// For each pixel in our image, ignoring the borders...
    for row = 2 : rows - 1
        for col = 2 : cols - 1
            cen = im(row,col); %// Get centre

            %// Get neighbourhood - cast to double for better precision
            pixels = double(im(row-1:row+1,col-1:col+1));

            %// Get ranges and determine LTP
            out_LTP = zeros(3, 3);
            low = cen - t;
            high = cen + t;
            out_LTP(pixels < low) = -1;
            out_LTP(pixels > high) = 1;
            out_LTP(pixels >= low & pixels <= high) = 0;

            %// Get upper and lower patterns
            upper = out_LTP;
            upper(upper == -1) = 0;
            upper = upper(reorder_vector);

            lower = out_LTP;
            lower(lower == 1) = 0;
            lower(lower == -1) = 1;
            lower = lower(reorder_vector);

            %// Convert to a binary character string, then use bin2dec
            %// to get the decimal representation
            upper_bitstring = char(48 + upper);
            ltp_upper(row,col) = bin2dec(upper_bitstring);

            lower_bitstring = char(48 + lower);
            ltp_lower(row,col) = bin2dec(lower_bitstring);
       end
   end

让我们慢慢浏览一下这段代码。首先,我得到图像的尺寸,以便我可以迭代每个像素。另外,请记住我假设图像是灰度。一旦我这样做,我分配空间来存储我们图像中每个像素的上下LTP模式,因为我们需要将其输出给用户。我决定忽略边界像素,当我们考虑一个像素邻域时,如果窗口越界,我们忽略这些位置。

Let's go through this code slowly. First, I get the dimensions of the image so I can iterate over each pixel. Also, bear in mind that I'm assuming that the image is grayscale. Once I do this, I allocate space to store the upper and lower LTP patterns per pixel in our image as we will need to output this to the user. I have decided to ignore the border pixels where when we consider a pixel neighbourhood, if the window goes out of bounds, we ignore these locations.

现在,对于每个有效像素在图像的有效边界内,我们提取像素邻域。我将这些转换为双精度以允许负差异,以及更好的精度。然后我计算低和高范围,然后根据我们上面讨论的指南创建一个LTP模式。

Now, for each valid pixel that is within the valid borders of the image, we extract our pixel neighbourhood. I convert these to double precision to allow for negative differences, as well as for better precision. I then calculate the low and high ranges, then create a LTP pattern following the guidelines we talked about above.

一旦我计算了LTP模式,我创建了两个版本的LTP模式,上部下部其中上部模式的任何值-1都映射到0和1表示较低的模式。此外,对于较低的模式,原始窗口中1的任何值都映射为0.之后,我按照我布局的顺序提取出位 - 从东边开始,逆时针方向。这是 reorder_vector 的目的,因为这将允许我们提取那些确切的位置。这些位置现在将成为一维向量。

Once I calculate the LTP pattern, I create two versions of the LTP pattern, upper and lower where any values of -1 for the upper pattern get mapped to 0 and 1 for the lower pattern. Also, for the lower pattern, any values that were 1 from the original window get mapped to 0. After, this, I extract out the bits in the order that I laid out - starting from the east, go counter-clockwise. That's the purpose of the reorder_vector as this will allow us to extract those exact locations. These locations will now become a 1D vector.

这个1D向量很重要,因为我们现在需要将此向量转换为字符串,以便我们可以使用 bin2dec 转换价值成十进制数。上部和下部LTP的这些数字是最终用于输出的数字,我们将它们放在两个输出变量的相应位置。

This 1D vector is important, as we now need to convert this vector into character string so that we can use bin2dec to convert the value into a decimal number. These numbers for the upper and lower LTPs are what are finally used for the output, and we place those in the corresponding positions of both output variables.

此代码未经测试,因此如果它不符合您的规格,您可以自行调试。

This code is untested, so it'll be up to you to debug this if it doesn't work to your specifications.

祝您好运!

这篇关于计算图像的局部三元模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆