scale an image using nearest neighbor interpolation 近邻插值代码


/****************************************************************************
* Func: scale_pnm *
* *
* Desc: scale an image using nearest neighbor interpolation *
* *
* Params: buffer - pointer to image in memory *
* fileout - name of output file *
* rows - number of rows in image *
* cols - number of columns in image *
* x_scale - scale factor in X direction *
* y_scale - scale factor in Y direction *
* type - graphics file type (5 = PGM 6 = PPM) *
****************************************************************************/
void scale_pnm(image_ptr buffer, char *fileout, int rows, int cols,
float x_scale, float y_scale, int type)
{
unsigned long x,y; /* loop indices for columns and rows */
unsigned long index; /* index into line buffer */
unsigned long source_index; /* address of source pixel in image buffer */
unsigned char *line_buff; /* output line buffer */
int new_rows, new_cols; /* values of rows and columns for new image */
unsigned line; /* number of pixels in one scan line */
FILE *fp; /* output file pointer */
unsigned long X_Source, Y_Source; /* x and y address of source pixel */
pixel_ptr color_buff; /* pointer to a color image in memory */

/* open new output file */
if((fp=fopen(fileout, "wb")) == NULL)
{
printf("Unable to open %s for output\n",fileout);
exit(1);
}

new_cols = cols * x_scale;
new_rows = rows * y_scale;

/* print out the portable bitmap header */
fprintf(fp, "P%d\n%d %d\n255\n", type, new_cols, new_rows);

if(type == 5) /* PGM file */
line = new_cols;
else /* PPM file */
{
line = new_cols * 3;
color_buff = (pixel_ptr) buffer;
}

line_buff = (unsigned char *) malloc(line);

for(y=0; y<new_rows; y++)
{
index = 0;
for(x=0; x<new_cols; x++)
{
X_Source = (unsigned long)((x / x_scale) + 0.5);
Y_Source = (unsigned long)((y / y_scale) + 0.5);
source_index = Y_Source * cols + X_Source;
if(type == 5) /* PGM */
line_buff[index++] = buffer[source_index];
else /* PPM */
{
line_buff[index++] = color_buff[source_index].r;
line_buff[index++] = color_buff[source_index].g;
line_buff[index++] = color_buff[source_index].b;
}
}
fwrite(line_buff, 1, line, fp);
}
fclose(fp);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nearest neighbor matching with ratio test is a simple and effective technique to filter out incorrect matches between feature descriptors. It works by comparing the distances between a descriptor in the first image and its two nearest neighbors in the second image. If the ratio of the distances is below a certain threshold, the match is considered correct. The ratio test is used to ensure that the nearest neighbor match is significantly closer to the descriptor than the second nearest neighbor. The threshold value for the ratio test is usually set between 0.7 and 0.8, depending on the specific application. Here is an example of how to perform nearest neighbor matching with ratio test in MATLAB: ```matlab % Load feature descriptors for two images descriptors1 = load('descriptors1.mat'); descriptors2 = load('descriptors2.mat'); % Find nearest neighbors using knnsearch idx = knnsearch(descriptors2, descriptors1, 'K', 2); % Apply ratio test threshold = 0.8; matches = []; for i = 1:size(descriptors1, 1) ratio = norm(descriptors1(i,:) - descriptors2(idx(i,1),:)) / norm(descriptors1(i,:) - descriptors2(idx(i,2),:)); if ratio < threshold matches = [matches; i, idx(i,1)]; end end % Visualize matches figure; showMatchedFeatures(image1, image2, points1(matches(:,1),:), points2(matches(:,2),:)); ``` In this example, the knnsearch function is used to find the two nearest neighbors of each descriptor in the second image for each descriptor in the first image. Then, for each descriptor in the first image, the ratio of the distances between the descriptor and its two nearest neighbors in the second image is computed. If the ratio is below the threshold, the match is considered correct and added to the matches matrix. Finally, the matched keypoints are visualized using the showMatchedFeatures function.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值