[英]Cropping an image array
我試圖通過創建一個由用戶分配的變量來裁剪圖像,然后從圖像的Width和Height的最大值中減去該值。 問題是當我對減法運算符執行結果而不是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");
}
}
我對此很迷失,不勝感激。 :)
您分配了新創建的newheight
和newwidth
,它們是在聲明/分配它們的塊中創建的。 因為當該塊結束時它們消失了,所以留下了原始版本,而這些原始版本從未分配給您!
您正在將%s
用於xcrop
和ycrop
。 您需要使用%d
。 換句話說,這條線
scanf("%s", &xcrop);
應該
scanf("%d", &xcrop);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.