精华内容
下载资源
问答
  • 最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻差值最近邻...
  • knn最近邻算法Data science is hot and it seems that everybody is working on some sort of project involving the latest state-of-the-art (SOTA) algorithm. Of course with good reason as in many cases we ...

    knn最近邻算法

    Data science is hot and it seems that everybody is working on some sort of project involving the latest state-of-the-art (SOTA) algorithm. Of course with good reason as in many cases we can use data to give very reasonable prediction, almost in any field. While there is a lot of focus lately on SOTA algorithms, the simpler methods are sometimes forgotten.

    数据科学炙手可热,似乎每个人都在从事某种涉及最新技术(SOTA)算法的项目。 当然有充分的理由,因为在许多情况下,我们几乎可以在任何领域使用数据进行非常合理的预测。 尽管近来有很多关于SOTA算法的关注,但有时却忘记了更简单的方法。

    Want to get started with Python? Start here!

    想开始使用Python吗? 从这里开始

    Recently, I played around with a k-nearest-neighbor (KNN) algorithm and I was amazed how powerful it can be. The technique itself is used in many other fields. For example, I used it to identify the same particles in consecutive frames of a high-speed recording for one of my research projects during my Ph.D. The coordinates of a particle are known and we look in the next frame at the closest particles around that position. Of course, when there are multiple particles very close by, you can get into trouble. For that, you can make use of higher order information from multiple frames such as the velocity or acceleration vector. For KNN in machine learning, we generally do not have temporal data therefore, we only use its first order, which is the simplest form.

    最近,我尝试了一种k最近邻(KNN)算法,并且惊讶于它的强大功能。 该技术本身已在许多其他领域中使用。 例如,在我攻读博士学位期间,我用它来识别高速记录连续帧中的相同粒子。 粒子的坐标是已知的,我们在下一帧中查看该位置附近最接近的粒子。 当然,当附近有多个粒子时,您可能会遇到麻烦。 为此,您可以利用多个帧中的高阶信息,例如速度或加速度矢量。 对于机器学习中的KNN,我们通常没有时间数据,因此,我们仅使用其一阶形式,这是最简单的形式。

    When we want to use KNN to classify new data, i.e. make a prediction, we use the already known data (or labeled data) as a kind of look-up table. We select data that is similar to the new data we want to predict and select the most prominent class from that selection. So we compare an unknown example to an already known dataset. There is no training, no layers, no weights. The only parameter is k and specifies the amount of neighbors to take into consideration when predicting the class. For example, to classify a kind of fruit, we can select the five most similar examples from the dataset. Now we say that the most prominent class of those five selected examples is probably also the class we want to predict. If we have found three apples and two pears, we would predict an apple as well.

    当我们要使用KNN对新数据进行分类(即进行预测)时,我们将已经知道的数据(或标记的数据)用作一种查找表。 我们选择与我们要预测的新数据相似的数据,然后从该选择中选择最突出的类别。 因此,我们将未知示例与已知数据集进行比较。 没有训练,没有层次,没有重量。 唯一的参数是k ,它指定在预测类别时要考虑的邻居数量。 例如,要对一种水果进行分类,我们可以从数据集中选择五个最相似的示例。 现在我们说,这五个选定示例中最突出的类别可能也是我们要预测的类别。 如果我们发现了三个苹果和两个梨,那么我们也会预测一个苹果。

    Now we come to another problem: how do we select the most similar examples from a list of features. When we have a single feature, e.g. height this would be very easy. We simply calculate the difference and select the k closest matches. But what to do when we also have weight and width? We have to quantify the difference for each feature and aggregate the result to a single value. Fortunately, there are many ways to do this. One of the most common is the Euclidean distance, which can be seen as the shortest straight line between two points.

    现在,我们遇到另一个问题:如何从功能列表中选择最相似的示例。 当我们只有一个特征(例如高度)时,这将非常容易。 我们只计算差值并选择k个最接近的匹配项。 但是当我们也有体重宽度时该怎么办? 我们必须量化每个功能的差异并将结果汇​​总为一个值。 幸运的是,有很多方法可以做到这一点。 欧几里德距离是最常见的距离之一,可以看作是两点之间的最短直线。

    Image for post
    Euclidean distance: the magic introduced by Pythagoras! (My own diagram skills)
    欧氏距离:毕达哥拉斯引入的魔力! (我自己的图表技巧)

    The Euclidean distance works for many features (or dimensions), however there is one short-coming that counts for KNN in general. The features have to be numeric and to calculate a distance, the numbers have to represent meaning. The height feature has meaning attached to the number. The larger the number, the larger the object. The smaller the number, the smaller the object. This makes the distance, i.e. the difference between two heights meaningful as it is the difference in height. Now let's take color as a feature. Color is not numeric (at least we use names, not wave-lengths) and therefore, we need to convert it to a categorical unit to create numbers. Now, a value of green is represented by 0, red by 1, etc. While the numbers indicate which color is attached to the object, the value itself has no real meaning. If we have an object that has color value 100 and another object with color value 50, these numbers do not mean anything, other than that the color is different. Therefore, a difference (or distance) between these numbers is meaningless and useless for KNN.

    欧几里得距离适用于许多特征(或尺寸),但是对于KNN来说,通常存在一个缺点。 这些要素必须是数字并计算距离,数字必须表示含义。 高度功能具有附加到数字的含义。 数字越大,对象越大。 数字越小,物体越小。 这使得距离,即两个高度之间的差有意义,因为它是高度差。 现在,让我们以颜色为特色。 颜色不是数字(至少我们使用名称而不是波长),因此,我们需要将其转换为分类单位以创建数字。 现在,绿色值由0表示,红色由1表示,依此类推。虽然数字表示对象附着的颜色,但该值本身并不具有实际意义。 如果我们有一个对象的颜色值是100,而另一个对象的颜色值是50,则这些数字没有任何意义,只是颜色不同。 因此,这些数字之间的差异(或距离)对于KNN来说是没有意义的,也没有用。

    While real categorical variables are not usable, not everything is lost. We can for example use binary indicators such as is_green to indicate if an object is green. Still, I would only use these if I am sure that the feature adds to the prediction.

    尽管无法使用真正的分类变量,但不会丢失所有内容。 例如,我们可以使用诸如is_green之类的二进制指示器来指示对象是否为绿色。 不过,只有在我确定该功能可以添加到预测中时,我才使用这些功能。

    创建算法 (Create the algorithm)

    We will create an KNN algorithm and use it on the famous Titanic dataset and predict who has survived the tragic disaster. Therefore, all the function will have some kind of link with that dataset. To create an KNN prediction algorithm we have to do the following steps:

    我们将创建一个KNN算法,并将其用于著名的泰坦尼克号数据集,并预测谁在悲剧性灾难中幸存下来。 因此,所有功能都将与该数据集建立某种链接。 要创建KNN预测算法,我们必须执行以下步骤:

    1. calculate the distance between the unknown point and the known dataset.2. select the k nearest neighbors for from that dataset.3. make a prediction

    1.计算未知点和已知数据集之间的距离。2。 从该数据集中选择k个最近的邻居3。 做出预测

    Image for post
    Simple GIF showing how KNN works (created myself / code available in Github)
    显示GNN工作原理的简单GIF(创建自己/ Github中提供的代码)

    As seen in the previous figure, we only have to apply Pythagoras to calculate the Euclidean distance:

    如上图所示,我们只需应用毕达哥拉斯即可计算出欧几里得距离:

    def euclidean_distance(vector1, vector2):
        return np.sqrt(np.sum((vector1 - vector2)**2))
    
    
    # test function
    vec1 = np.array([3, 0])
    vec2 = np.array([0, 4])
    
    
    # this is the 3:4:5 triangle and therefore, it should return 5 (Long live Pythagoras)
    euclidean_distance(vec1, vec2)

    Next we need to make a loop to calculate and compare all the distance of the dataset with the point we want to predict:

    接下来,我们需要做一个循环来计算和比较数据集的所有距离与我们要预测的点:

    # Another implementation using Pandas
    def get_nearest_neighbor(vector, dataset, number_of_vectors=1, ignore_cols=['Survived'], not_count_duplicates=False):
        ds = dataset.copy()
        ds['distance'] = ds.loc[:, ~ds.columns.isin(ignore_cols)].apply(
            lambda x: euclidean_distance(x, vector), axis=1)
        if not_count_duplicates:
            distances = sorted(ds.distance.unique())[:number_of_vectors]
            return ds.loc[ds.distance <= max(distances)].drop('distance', axis=1)
        return ds.sort_values('distance', ascending=True).head(number_of_vectors).drop('distance', axis=1)
            
    # test function
    dataset = pd.DataFrame([
        {'a': 1, 'b': 1, 'Survived': 1},
        {'a': 2, 'b': 2, 'Survived': 1},
        {'a': 3, 'b': 3, 'Survived': 0},
        {'a': 4, 'b': 4, 'Survived': 0},
        {'a': 5, 'b': 5, 'Survived': 0},
    ])
    vector = pd.Series({'a': 2.5, 'b': 2.5})
    
    
    # should be (2,2) and (3,3) (if keeping track of duplicates)
    get_nearest_neighbor(vector, dataset)

    Now we have a function that gives us a list of k nearest neighbors. The final step is to take the most prominent class from that list and use that as a prediction. This is easily solved by taking the mean and rounding it. Feel free to check if this is true.

    现在我们有了一个函数,该函数为我们提供了k个最近邻居的列表。 最后一步是从列表中选出最杰出的班级,并将其用作预测。 这可以通过取平均值并四舍五入来轻松解决。 随时检查是否正确。

    def predict(vector, dataset, number_of_neighbors=1, y='Survived'):
        neighbors = get_nearest_neighbor(vector, dataset, number_of_neighbors)
        return round(neighbors[y].mean())
    
    
    # test function
    print(predict(vector, dataset))
    print(predict(pd.Series({'a': 4.5, 'b': 4.5}), dataset))

    准备数据集(Prepare the dataset)

    Before we can test the algorithm we need to prepare the data. This is reading in the data and filling in the missing values in a smart way. We will not be using the Embarked and Cabin feature, so we do not have to bother about those. There is a single Fare missing, which we will fill in using the median of that Pclass. There is quite some Age data missing. We will be using the title to make a good guess for each missing age.

    在测试算法之前,我们需要准备数据。 这是在读取数据并以一种聪明的方式填写缺失的值。 我们将不会使用“登船和机舱”功能,因此我们不必为这些而烦恼。 缺少一个票价,我们将使用该Pclass的中位数来填写。 缺少一些年龄数据。 我们将使用标题为每个缺少的年龄做出很好的猜测。

    import numpy as np
    import pandas as pd
    from sklearn.preprocessing import LabelEncoder, StandardScaler
    
    
    train = pd.read_csv('../datasets/titanic/train.csv')
    test = pd.read_csv('../datasets/titanic/test.csv')
    
    
    train['set'], test['set'] = 'train', 'test'
    combined = pd.concat([train, test])
    
    
    # Fill in missing fare
    pclass = combined.loc[combined.Fare.isnull(), 'Pclass'].values[0]
    median_fare = combined.loc[combined.Pclass== pclass, 'Fare'].median()
    combined.loc[combined.Fare.isnull(), 'Fare'] = median_fare
    
    
    ## Fill in missing ages
    # Select everything before the . as title
    combined['Title'] = combined['Name'].str.extract('([A-Za-z]+)\.', expand=True)
    title_reduction = {'Mr': 'Mr', 'Mrs': 'Mrs', 'Miss': 'Miss', 
                       'Master': 'Master', 'Don': 'Mr', 'Rev': 'Mr',
                       'Dr': 'Mr', 'Mme': 'Miss', 'Ms': 'Miss',
                       'Major': 'Mr', 'Lady': 'Mrs', 'Sir': 'Mr',
                       'Mlle': 'Miss', 'Col': 'Mr', 'Capt': 'Mr',
                       'Countess': 'Mrs','Jonkheer': 'Mr',
                       'Dona': 'Mrs'}
    combined['Title'] = combined['Title'].map(title_reduction)
    for title, age in combined.groupby('Title')['Age'].median().iteritems():
        combined.loc[(combined['Title']==title) & (combined['Age'].isnull()), 'Age'] = age

    Next we create a couple of additional features (features). One that I recently saw on Kaggle is family_survivability. It assumes that families help each other and that if others in your family survive, you are more likely to also have survived. The idea comes from Shunjiang Xu / Blood is thicker than water and I think it is pretty amazing. Furthermore, we will add the family size, change Sex to a binary indicator, and scale all variables and split it back to train/test.

    接下来,我们创建几个附加功能(功能)。 我最近在Kaggle上看到的一个是family_survivability。 它假设家人互相帮助,并且如果家人中的其他人能够生存,那么您也更有可能生存下来。 这个想法来自Shunjiang Xu /血液比水浓稠,我认为这非常惊人。 此外,我们将增加家庭人数,将性别更改为二进制指标,并缩放所有变量并将其拆分回训练/测试。

    def other_family_members_survived(dataset, label='family_survival'):
        """
        Check if other family members survived
          -> 0 other did not survive
          -> 1 at least one other family member survived
          -> 0.5 unknown if other members survived or person was alone
        
        Parameters
        ----------
        dataset : DataFrame
          The sub-dataframe containing the family
        """
        ds = dataset.copy()
        if len(dataset) == 1:
            ds[label] = 0.5
            return ds
        result = []
        for ix, row in dataset.iterrows():
            survived_fraction = dataset.drop(ix)['Survived'].mean()
            if np.isnan(survived_fraction):
                result.append(0.5)
            elif survived_fraction == 0:
                result.append(0)
            else:
                result.append(1)
        ds[label] = result
        return ds
     
    # Get family_survival from surname
    combined['surname'] = combined['Name'].apply(lambda x: x.split(",")[0])
    combined = combined.groupby(['surname', 'Fare']).apply(other_family_members_survived).reset_index(drop=True)
    # Fill in missing from Tickets
    combined = combined.groupby(['Ticket']).apply(lambda x: other_family_members_survived(x, label='family_survival_ticket')).reset_index(drop=True)
    combined.loc[combined['family_survival'] == 0.5, 'family_survival'] = combined.loc[combined['family_survival'] == 0.5, 'family_survival_ticket']
    
    
    # Get family size
    combined['family_size'] = combined['Parch'] + combined['SibSp']
    
    
    # Make binary
    combined['Sex'] = LabelEncoder().fit_transform(combined['Sex'])
    
    
    # Bin Age and Fare
    combined['Age'] = pd.qcut(combined['Age'], 4, labels=False)
    combined['Fare'] = pd.qcut(combined['Fare'], 5, labels=False)
    
    
    # Scale classes
    selected = ['Pclass', 'Sex', 'Age', 'Fare', 'family_size', 'family_survival']
    scaler  = StandardScaler()
    scaler.fit(combined[selected])
    combined[selected] = scaler.transform(combined[selected])
    
    
    # Split back to train/test
    train = combined.loc[combined['set'] == 'train'].drop('set', axis=1).reset_index(drop=True)
    test = combined.loc[combined['set'] == 'test'].drop(['set', 'Survived'], axis=1).reset_index(drop=True)

    作出预测(Make predictions)

    The algorithm is ready, the data is prepared, we are ready to make some predictions!

    算法已经准备好,数据已经准备好了,我们已经可以做出一些预测了!

    def predict_dataset(dataset, number_of_neighbors=1):
        ds = dataset.copy()
        def predict_row(vector, dataset):
            subset = dataset.loc[~(dataset.index==vector.name)]
            if vector.name % 100 == 0:
                print(vector.name)
            return int(predict(vector, subset, number_of_neighbors))
        ds['predicted'] = ds.loc[:, ds.columns.isin(selected)].apply(
            lambda x: predict_row(x, ds), axis=1)
        return ds
    
    
    ds = predict_dataset(train, number_of_neighbors=10)
    
    
    print('Accuracy:', sum(ds['Survived'] == ds['predicted']) / len(ds))

    It takes pretty long, because our algorithm is quite slow. Put if all is correct, you should get an accuracy of 83.5%. The only parameter we can tweak is the the number of neighbors to take in consideration. I played around a bit and 10 seems to give the best results. Now lets make a Kaggle submission dataset!

    因为我们的算法很慢,所以需要很长时间。 正确地说,您应该获得83.5%的准确度。 我们可以调整的唯一参数是要考虑的邻居数量。 我玩了大约10个,似乎取得了最好的结果。 现在,让我们创建一个Kaggle提交数据集!

    def predict_testset(test_dataset, train_dataset, number_of_neighbors=1):
        ds = test_dataset.copy()
        select = selected + ['Survived']
        
        def predict_row(vector, dataset):
            if vector.name % 100 == 0:
                print(vector.name)
            return int(predict(vector, dataset[select], number_of_neighbors))
    
    
        ds['Survived'] = ds.loc[:, ds.columns.isin(selected)].apply(
            lambda x: predict_row(x, train_dataset), axis=1)
        
        return ds
    
    
    final_test = predict_testset(test, train, number_of_neighbors=10)
    result = final_test[['PassengerId', 'Survived']].copy()
    result.to_csv('results.csv', index=False)

    Submitting this to Kaggle will result in a score of 81.3%. Slightly lower than the training set but still quite high. And we managed this by only using a simple KNN algorithm.

    提交给Kaggle将会得到81.3%的分数。 略低于训练集,但仍然很高。 而且,我们仅使用简单的KNN算法进行了管理。

    最后的想法 (Some last thoughts)

    The algorithm we created was powerful and very effective. The only downside is that our implementation is very slow. For each vector, we need to calculate and compare the distance with the complete dataset. Sci-kit learn comes with smarter implementations that use Tree-based approaches to minimize the amount of calculation required and making it tremendously faster.

    我们创建的算法功能强大且非常有效。 唯一的缺点是我们的实施非常缓慢。 对于每个向量,我们需要计算距离并将其与完整的数据集进行比较。 Sci-kit学习带有更智能的实现,这些实现使用基于树的方法来最大程度地减少所需的计算量并大大加快计算速度。

    Here we used KNN as a classifier, however, it works very similar to regression as well. With regression, the result is continuous, therefore it makes sense to, for example, average nearest neighbors when making a prediction.

    这里我们使用KNN作为分类器,但是它的工作原理也非常类似于回归。 通过回归,结果是连续的,因此例如在进行预测时对平均最近邻居有意义。

    I hope you had as much fun as I had. If you have any questions, feel free to contact me through LinkedIn.

    我希望你和我一样开心。 如有任何疑问,请随时通过LinkedIn与我联系。

    All code is available on my Github and on Kaggle.

    所有代码都可以在我的GithubKaggle上找到

    I would very much appreciate any upvote of my Kaggle notebooks.

    非常感谢我对Kaggle笔记本的任何赞扬

    翻译自: https://towardsdatascience.com/k-nearest-neighbor-knn-algorithm-3d16dc5c45ef

    knn最近邻算法

    展开全文
  • I am trying to write my own function for scaling up an input image by using the Nearest-neighbor interpolation algorithm. The bad part is I am able to see how it works but cannot find the algorithm it...

    I am trying to write my own function for scaling up an input image by using the Nearest-neighbor interpolation algorithm. The bad part is I am able to see how it works but cannot find the algorithm itself. I will be grateful for any help.

    Here's what I tried for scaling up the input image by a factor of 2:

    function output = nearest(input)

    [x,y]=size(input);

    output = repmat(uint8(0),x*2,y*2);

    [newwidth,newheight]=size(output);

    for i=1:y

    for j=1:x

    xloc = round ((j * (newwidth+1)) / (x+1));

    yloc = round ((i * (newheight+1)) / (y+1));

    output(xloc,yloc) = input(j,i);

    end

    end

    Here is the output after Mark's suggestion

    KnNpF.png

    解决方案

    A while back I went through the code of the imresize function in the MATLAB Image Processing Toolbox to create a simplified version for just nearest neighbor interpolation of images. Here's how it would be applied to your problem:

    %# Initializations:

    scale = [2 2]; %# The resolution scale factors: [rows columns]

    oldSize = size(inputImage); %# Get the size of your image

    newSize = max(floor(scale.*oldSize(1:2)),1); %# Compute the new image size

    %# Compute an upsampled set of indices:

    rowIndex = min(round(((1:newSize(1))-0.5)./scale(1)+0.5),oldSize(1));

    colIndex = min(round(((1:newSize(2))-0.5)./scale(2)+0.5),oldSize(2));

    %# Index old image to get new image:

    outputImage = inputImage(rowIndex,colIndex,:);

    Another option would be to use the built-in interp2 function, although you mentioned not wanting to use built-in functions in one of your comments.

    EDIT: EXPLANATION

    In case anyone is interested, I thought I'd explain how the solution above works...

    newSize = max(floor(scale.*oldSize(1:2)),1);

    First, to get the new row and column sizes the old row and column sizes are multiplied by the scale factor. This result is rounded down to the nearest integer with floor. If the scale factor is less than 1 you could end up with a weird case of one of the size values being 0, which is why the call to max is there to replace anything less than 1 with 1.

    rowIndex = min(round(((1:newSize(1))-0.5)./scale(1)+0.5),oldSize(1));

    colIndex = min(round(((1:newSize(2))-0.5)./scale(2)+0.5),oldSize(2));

    Next, a new set of indices is computed for both the rows and columns. First, a set of indices for the upsampled image is computed: 1:newSize(...). Each image pixel is considered as having a given width, such that pixel 1 spans from 0 to 1, pixel 2 spans from 1 to 2, etc.. The "coordinate" of the pixel is thus treated as the center, which is why 0.5 is subtracted from the indices. These coordinates are then divided by the scale factor to give a set of pixel-center coordinates for the original image, which then have 0.5 added to them and are rounded off to get a set of integer indices for the original image. The call to min ensures that none of these indices are larger than the original image size oldSize(...).

    outputImage = inputImage(rowIndex,colIndex,:);

    Finally, the new upsampled image is created by simply indexing into the original image.

    展开全文
  • 利用最近邻插值法实现图像的缩小与放大.....................................................................................
  • C语言-PTA 最近邻

    2020-10-16 08:43:16
    PTA 最近邻

    一、题目

    在这里插入图片描述

    二、算法思路

    这道题的思路不是很难,就是后两个测试点对应的范围比较大,需要优化一下代码来提高效率。
    原本我使用的是c++写的代码,后两个测试点过不去,但换成了c进行提交的时候就过了。
    如果你也出现了后两个测试点过不去的情况,不妨试试用其他语言编写提交。

    题目给了我们一个非降序列,其实这个题意就是说已经给了我们一组升序的数,所以我们可以省去不必要的排序操作。
    我是直接使用的两个变量,一个存最小差值,一个是最近邻。
    如果输入的是第一个数时,就进行初始化。
    若当前输入的值更接近x的值时,就替换掉flag和value

    三、代码

    在这里插入图片描述

    //作者:小狐狸
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int n,x;
        int i;
        int flag;//最近邻
        int value;//最小差值
        scanf("%d%d",&n,&x);
        for(i=1;i<=n;i++)
        {
            int tmp;//临时变量
            scanf("%d",&tmp);
            if(i==1)//初始化最小差值、最近邻
            {
                flag = tmp;
                value = abs(tmp-x);
            }
            else if(value>abs(tmp-x))//出现更小的差值时
            {
                flag = tmp;
                value = abs(tmp-x);
            }
        }
        printf("%d\n",flag);
        return 0;
    }
    
    展开全文
  • 主要介绍了python 图像插值 最近邻、双线性、双三次实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
  • 最近邻插值算法 python实现

    千次阅读 2020-03-18 01:36:03
    最近邻插值法在放大图像时补充的像素是最近邻的像素的值。由于方法简单,所以处理速度很快,但是放大图像画质劣化明显,常常含有锯齿边缘。 最近邻插值法算法原理 ↑ 二. 最近邻插值法算法流程: 插值法放大...

    一. 最近邻插值法放大图像:

            最近邻插值法在放大图像时补充的像素是最近邻的像素的值。由于方法简单,所以处理速度很快,但是放大图像画质劣化明显,常常含有锯齿边缘。

    最近邻插值法算法原理 ↑


    二. 最近邻插值法算法流程:

            插值法放大图像的第一步都是相同的,计算新图的坐标点像素值对应原图中哪个坐标点的像素值来填充,计算公式为:

            srcX = dstX * (srcWidth/dstWidth)

            srcY = dstY * (srcHeight/dstHeight)

            其中,src表示旧图,dst表示新图。新图的坐标(dstX,dstY)对应于旧图的坐标(srcX,srcY)。 srcWidth/dstWidth 和 srcHeight/dstHeight 分别表示宽和高的放缩比。

            那么问题来了,通过这个公式算出来的 srcX,scrY (旧图坐标)有可能是小数,但是坐标点是不存在小数的,都是整数,得想办法把它转换成整数才行。

            不同插值法的区别就体现在 srcX,scrY 是小数时,怎么变成整数去取原图像中的像素值。

            最近邻插值法:看名字就很直白,四舍五入选取最接近的整数。这样的做法会导致像素的变化不连续,在新图中会产生锯齿。


    三. 实验 python实现最近邻插值算法

    from PIL import Image
    import matplotlib.pyplot as plt
    import numpy as np 
    import math
    
    # 最近邻插值算法
    # dstH为新图的高;dstW为新图的宽
    def NN_interpolation(img,dstH,dstW):
        scrH,scrW,_=img.shape
        retimg=np.zeros((dstH,dstW,3),dtype=np.uint8)
        for i in range(dstH-1):
            for j in range(dstW-1):
                scrx=round(i*(scrH/dstH))
                scry=round(j*(scrW/dstW))
                retimg[i,j]=img[scrx,scry]
        return retimg
    
    im_path='../paojie.jpg'
    image=np.array(Image.open(im_path))
    
    image1=NN_interpolation(image,image.shape[0]*2,image.shape[1]*2)
    image1=Image.fromarray(image1.astype('uint8')).convert('RGB')
    image1.save('out.png')

    四. 实验结果:

    原图 ↑

    最近邻插值后图像 ↑

            可以看到新图中产生了锯齿状的边缘。


    五. 参考内容:

            https://www.cnblogs.com/wojianxin/p/12515061.html

            https://www.jianshu.com/p/4954643f7514

    展开全文
  • 而对于亚马逊这样的电商巨头,如果想要知道某个顾客最有可能购买的 12 个产品,就可以运行一个 12-最近邻算法(但亚马逊实际运行的算法要远比一个简单的 12-最近邻算法复杂)。 群组 我们再来看一下群组,这个功能...
  • 最近邻插值法
  • 最近邻法实现cifar10分类

    千次阅读 2016-12-29 08:53:56
    最近邻法实现cifar10分类本笔记用于记录学习过程,如有疑问,欢迎交流:目录用 [TOC]来生成目录:最近邻法实现cifar10分类目录 定义Nearest Neighbor 代码定义定义: 输入一个元素为像素值的数组,然后给它分配一个...
  • '''每一列先减去最小值,然后除以每一列数据差值''' retData = (data_set - minVals) / ranges return retData, ranges, minVals def predict(x_train,y_train,x_test): knn = KNeighborsClassifier(n_...
  • N维数组的快速最近邻插值(Nearest Neighbour),Python实现 这份代码实现的是numpy.ndarray的快速最近邻插值(放缩)。而这个方法貌似并没有直接的API。 目标 我们由3*3的数组如下: [[1 2 3] [4 5 6] [7 8 9]] ...
  • 图像缩放之最近邻插值

    千次阅读 2016-10-06 09:40:59
    图像缩放之最近邻插值
  • 2 Matlab实现最近邻插值、双线性插值、双三次插值3最近邻插值的原理简介4双线性插值的原理简介5双三次插值原理简介 1插值是什么?为啥要插值? 一句话来说就是插值用于图片缩放,为了解决图片缩放中可能出现小数坐标...
  • 最近邻分类器 作为我们的第一个方法——最近邻分类器。这个分类器与卷积神经网络无关,在实际中很少使用,但它可以让我们了解一个图像分类问题的基本方法。 示例图像分类数据集:CIFAR-10。一个流行的图像分类数据...
  • 最近邻插值、双线性插值

    千次阅读 2017-07-17 17:29:18
    最近邻插值: 公式如下: /*最近邻插值*/ /* 输入:src:输入源图 dst_rows:目标图的行数 dst_cols:目标图的列数 输出:目标图 */ Mat Neargest_Neighbor(Mat src,int dst_rows,int dst_cols) { Mat dst(dst...
  • 它可以看作是Nearest Neighbor Classifier最近邻分类器的加强版,无论是最近邻分类器还是k最近邻分类器,其原理都比较简单,其算法在CIFAR-10图像分类的效果上其正确率远低于人类识别图像的正确率(约94%),但也略...
  • 图像的灰度级差值

    千次阅读 2016-09-03 15:12:13
    灰度级差值是用来估计像素在图像像素间某一位置的取值的。 1)向前映射法  通过输入图像像素位置,计算输出图像对应像素的位置,将该位置...3)最近邻差值 向后映射时,输出图像的灰度等于离它所映射位置最近的输入
  • Matlab之最近邻插值'nearest'

    万次阅读 2017-12-11 21:52:50
    最近邻插值’nearest’ 什么是最近邻插值?我的理解是它就是用来对新的像素进行赋值的一种方法。 我为了能方便快速的弄清楚它对图像像素的具体操作,我用的是4×4像素的图像,通过imresize()函数来展示。 ...
  • KNN算法||最近邻||概述

    2019-07-22 09:34:50
    元学习论文总结||小样本学习论文总结 2017-2019年计算机视觉顶会文章收录 AAAI2017-2019 CVPR... 邻近算法,或者说K最近邻(kNN,k-NearestNeighbor)分类算法是数据挖掘分类技术中最简单的方法之一。所谓K最近邻,...
  • 最近邻插值 与 双线性插值算法 优化迭代 的 0.5 像素之差
  • 最近邻分类器(Nearest Neighbor Classifier)

    千次阅读 2018-09-29 08:07:34
    先从简单的方法开始说,先提一提最近邻分类器/Nearest Neighbor Classifier,不过事先申明,它和深度学习中的卷积神经网/Convolutional Neural Networks其实一点关系都没有,我们只是从基础到前沿一点一点推进,最近...
  • 1、最近邻插值:变换后的目标图像某点像素值等于源图像中与变换前相应点最近的点的像素值。具体操作为,设水平方向和垂直方向缩放的比例分别为w和h,那么目标图像中的点des(x,y)对应的源图像中的点src的坐标为(x0,y0...
  • 聪明的我,里面想到了最近邻插值、双线性插值的方法来做。 二、最近邻插值 所谓最近邻插值,就是当图像进行尺度缩放时,把直接找像素点最近的进行填充。比如第一个矩阵经过最近邻插值进行二倍的扩
  • 插值算法:最近邻插值、双线性插值 插值算法有很多种,这里列出关联比较密切的三种: 最近邻法(Nearest Interpolation):计算速度最快,但是效果最差。 双线性插值(Bilinear Interpolation):双线性插值是用原图像...
  • 最近邻插值法 在一维空间中,最近点插值就相当于四舍五入取整。在二维图像中,像素点的坐标都是整数,该方法就是选取离目标点最近的点。 使用下面的公式放大图像! I’ 为放大后图像, I为放大前图像, a为放大率,...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,839
精华内容 1,135
关键字:

最近邻差值