如何旋转图像用C

如何旋转图像用C

本文介绍了如何旋转图像用C 90度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图将一个黑白图像,然后旋转在C 90度,但我相当新的节目,这是我不得不远。

 #包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;诠释主(){
FILE *第一;
FILE *第二;
FILE *第三;
INT计数器;
焦炭℃;
INT宽度,高度,pixelmax,平均值;
INT pixelred,pixelgreen,pixelblue,黑[300] [300] [3];
INT I,J,定时器= 0;
INT K,F = 0;
第一FOPEN =(blackbuck.ppm,R);
第二=的fopen(blacktrout.ppm,W);

这将跳过code的前几行

 为(计数器= 1;计数器3;;计数器++){
做{
    C = GETC(第一);
}而(C ='\\ n'!);
    }
的fscanf(第一%D,&安培;宽度和放大器;高度);
的fscanf(第一%d个,&安培; pixelmax);

在程序的这一部分,我把像素为黑色和白色,采取他们的平均,这是比较容易的部分。

 为(i = 0,J = 0; I<宽度;我++,定时器++){
的fscanf(第一%D%D,&安培; pixelred,&安培; pixelgreen,&安培; pixelblue);
平均=(pixelred + pixelgreen + pixelblue)/ 3;
黑色的[I] [J] [0] =平均值;
黑色的[I] [j]的[1] =平均值;
黑色的[I] [J] [2] =平均值;
fprintf中(第二,%D%d个\\ N,黑色的[I] [j]的[0],黑色的[I] [j]的[1],黑色的[I] [J] [2]);如果(ⅰ==(宽度-1)及与放大器; J&下;高度){
I = 0;
J ++;
}
}
FCLOSE(第一);
FCLOSE(第二);
第三=的fopen(blackflip.ppm,W);

这是我在哪里完全失去了一部分,我不知道如何在我的3D阵列90度旋转像素商店。任何帮助吗?一个很好的解释说明会走很长的路是新手程序员喜欢自己。谢谢!

 的(... ???)
}
}
返回0;


解决方案

的基本逻辑是这样的:

  original_x = original_height-1-rotated_y
original_y = rotated_x
rotated_image(rotated_x,rotated_y)= original_image(original_x,original_y)

让我们说,(0,0)是左上角,我们是想向左旋转90度。然后将旋转后的图像的左上角是等同于原始图像的右上角,它是在(original_height-1,0)。

随着你在图像的顶部,增加中,x,则是从沿着右侧的原始图像采集像素,增加Y,所以你的旋转图像的x是像原来图像的y

当你走在rotated_image,增加Y,你正将越来越多的original_image的左边,这就是为什么我们从减去rotated_y original_height-1,以获得original_x坐标。

要注意的另一件事是,旋转后的图像的宽度和高度都从原始图片颠倒。

I've been trying to turn an image black and white, and then rotate it 90 degrees in C but i'm fairly new to programming, this is what i have to far.

#include<stdio.h>
#include<string.h>

int main () {
FILE* first;
FILE* second;
FILE* third;
int counter;
char c;
int width, height, pixelmax, average;
int pixelred, pixelgreen, pixelblue, black[300][300][3];
int i, j, timer=0;
int k, f=0;
first=fopen("blackbuck.ppm","r");
second=fopen("blacktrout.ppm","w");

this skips the first few lines of code

for(counter=1;counter<3;counter++){
do{
    c=getc(first);
}while(c != '\n');
    }
fscanf(first,"%d%d", &width,&height);
fscanf(first,"%d", &pixelmax);

in this part of the program, i turn the pixels to black and white by taking their average, this is the easy part.

for(i=0, j=0; i<width;i++, timer++){
fscanf(first,"%d%d%d",&pixelred,&pixelgreen,&pixelblue);
average=(pixelred+pixelgreen+pixelblue)/3;
black[i][j][0]=average;
black[i][j][1]=average;
black[i][j][2]=average;
fprintf(second,"%d %d %d\n",black[i][j][0],black[i][j][1],black[i][j][2]);

if (i==(width-1)&& j<height){
i=0;
j++;
}
}
fclose(first);
fclose(second);
third=fopen("blackflip.ppm","w");

This is the part where i am completely lost, i have no idea how to rotate the pixels stores in my 3d array 90 degrees. Any help please? A well explained explanation would go a long way for a newbie programmer like myself. thanks!

for(...???)
}
}


return 0;
解决方案

The basic logic is like this:

original_x = original_height-1-rotated_y
original_y = rotated_x
rotated_image(rotated_x,rotated_y) = original_image(original_x,original_y)

Let's say that (0,0) is the upper-left corner, and we are wanting to rotate left 90 degrees. Then the upper-left corner of the rotated image is equivalent to the upper-right corner of the original image, which is at (original_height-1,0).

As you go across the top of the image, increasing, x, you are grabbing pixels from the original image along the right side, increasing y, so the x of your rotated image is like the y of the original image.

As you go down in the rotated_image, increasing y, you are moving more to the left of the original_image, which is why we're subtracting rotated_y from original_height-1 to get the original_x coordinate.

Another thing to notice is that the width and height of the rotated image are reversed from the orignal image.

这篇关于如何旋转图像用C 90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:09