【问题标题】:Cropping an image array裁剪图像数组
【发布时间】:2014-04-29 06:47:43
【问题描述】:

我尝试通过创建一个由用户分配的变量然后从图像的宽度和高度的最大值中减去该变量来裁剪图像。 问题是当我做减法运算符而不是 HEIGHT - 输入 = 数字时,我得到一个奇怪的数字 -180000...

这是我的代码

#define _CRT_SECURE_NO_DEPRECATE 1
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//#include "functions.h"
#define _CRT_SECURE_NO_WARNINGS 1
#define HEIGHT 128
#define WIDTH 256
int image02[HEIGHT][WIDTH][3];
int image03[HEIGHT][WIDTH][3];
int x, y;

FILE*RED = NULL;
FILE*BLUE = NULL;
FILE*GREEN = NULL;
FILE*REDRead = NULL;
FILE*BLUERead = NULL;
FILE*GREENRead = NULL;
FILE*Write1 = NULL;
char cropans [30];
char red [30];
char green [30];
char blue [30];
char ppm [30];
char rotans [30];
int xcrop;
int ycrop;
int newheight;
int newwidth;

void main()
{

    printf("Type in filenames of the image files, including extensions\nThe program will exit if it cannot read any files\n");  //this is the UI//
    printf("Red channel data:  ");  
        scanf("%s", &red);
        REDRead = fopen(red, "r");  
        if (REDRead == NULL)
        {
            printf("\nCannot read red image data\n");
            exit(0);
        }
    printf("Green channel data:  ");
        scanf("%s", &green);
        GREENRead = fopen(green, "r");
        if (GREENRead == NULL)
        {
            printf("\nCannot read green image data\n");
            exit(0);
        }
    printf("Blue channel data:  ");
        scanf("%s", &blue); 
        BLUERead = fopen(blue, "r");
        if (BLUERead == NULL)
        {
            printf("\nCannot read blue image data\n");
            exit(0);
        }


    printf("\nNow type in the name of the file you want to create, with extension:\n");
        scanf("%s", &ppm);
    printf("\nWould you like to rotate a channel, or create an image without rotating any channels?(y/n)\n");
        scanf("%s", &rotans);
    printf("\nWould you like to crop the image? (y/n)"); 
        scanf("%s", &cropans); 
        if (strcmp( cropans, "y") == 0)
            {
            printf("\ntype number of pixels to crop from x axis\n");
            scanf("%s", &xcrop);
            printf("\ntype number of pixels to crop from y axis\n");
            scanf("%s", &ycrop);            
            }
        else if(strcmp( cropans, "n") == 0);
        {
            printf("hi");
        }
    printf("got input correctly\n");


    //Zeroing the seperate arrays//
    for (y = 0; y<HEIGHT; y++)                                  
    {
        for (x = 0; x<WIDTH; x++)                               
        {
            image02[y][x][0] = 0;                                   
            image02[y][x][1] = 0;                                   
            image02[y][x][2] = 0;                                   
        }
    }


    for (y = 0; y<HEIGHT; y++)                                      
    {
        for (x = 0; x<WIDTH; x++)                                   
        {
            image03[y][x][0] = 0;                                   
            image03[y][x][1] = 0;                                   
            image03[y][x][2] = 0;                                   
        }
    }

    {
        int newwidth = (WIDTH - xcrop);
        int newheight = (HEIGHT - ycrop);
    }
    //printing to file part//
    //no rotation//

    if(strcmp( rotans, "n") == 0)
        {
            for (y = 0; y<HEIGHT; y++)
            {
                for (x = 0; x<WIDTH; x++)
                    {
                    fscanf(REDRead,"%d", &image03[y][x][0]);
                    fscanf(GREENRead, "%d",&image03[y][x][1]);
                    fscanf(BLUERead,"%d", &image03[y][x][2]);
                    }
            }   

        Write1 = fopen(ppm, "w");
        printf("got here 1");
        fprintf(Write1,"P3\n %d %d \n 255\n", WIDTH, HEIGHT);
        for (y = 0; y<(HEIGHT); y++)
        {
            for (x = 0; x<(WIDTH); x++)
                {
                fprintf(Write1,"%d %d %d ", image03[y][x][0], image03[y][x][1], image03[y][x][2]);
                }
        }
        printf("got here 1");
        }

else if (strcmp( rotans, "y") == 0)
    {
        for (y = 0; y<HEIGHT; y++)
        {
            for (x = 0; x<WIDTH; x++)
            {
                fscanf(REDRead,"%d", &image02[y][x][0]);
                fscanf(BLUERead,"%d", &image02[y][x][2]);
            }
        }

        for (y = HEIGHT; y>0; y--)
        {
            for (x = WIDTH; x>0; x--)
            {
                fscanf(GREENRead,"%d", &image02[y][x][1]);
            }
        }

        Write1 = fopen(ppm, "w");
        printf("got here 1");
        fprintf(Write1,"P3\n %d %d \n 255\n", WIDTH, HEIGHT);
        for (y = 0; y<HEIGHT; y++)
        {
            for (x = 0; x<WIDTH; x++)
            {
            fprintf(Write1,"%d %d %d ", image02[y][x][0], image02[y + 11][x + 11][1], image02[y][x][2]);
            }
        }
        printf("got here 1");
    }

}

我很迷茫,任何帮助将不胜感激。 :)

【问题讨论】:

    标签: c arrays image int crop


    【解决方案1】:

    您分配给新创建的newheightnewwidth,它们是在您声明/分配它们的块中创建的。因为当该块结束时它们会消失,所以您会留下原始版本,而这些版本从未被分配!

    【讨论】:

      【解决方案2】:

      您将%s 用于xcropycrop。您需要使用%d。也就是说,这一行

      scanf("%s", &xcrop);
      

      应该是

      scanf("%d", &xcrop);
      

      【讨论】:

        猜你喜欢
        • 2011-05-05
        • 2017-05-22
        • 2018-12-13
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 2023-04-02
        • 2020-09-06
        相关资源
        最近更新 更多