在图像上找到最大颜色的问题
这是查找图像中最大颜色的代码,但它总是返回错误的坐标。 代码搜索并找到最大值并返回图像的二维数组,但是当他找到最大颜色时,它会创建一个十字并返回带有十字的图像,只是为了知道最大颜色的位置在哪里。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dip.h"
#define INPUT_FILENAME "chest339x339.raw"
#define OUTPUT_FILENAME "task_4_chest339x339_out.raw"
#define ROWS 339
#define COLUMNS 339
#define GREYLEVELS 256
#define OUTPUT_ROWS ROWS
#define OUTPUT_COLUMNS COLUMNS
unsigned char** createImage(unsigned char** inputImage, int rows, int columns, int x_max, int y_max ) ;
int main(void) {
unsigned char **inputImage,
**outputImage;
int x_max=0,y_max=0;
FILE *inputFile,
*outputFile;
inputImage = allocateImage(ROWS, COLUMNS);
inputImage = loadImage(INPUT_FILENAME, ROWS, COLUMNS);
outputImage = createImage(inputImage,ROWS, COLUMNS,x_max,y_max);
saveImage( OUTPUT_FILENAME,outputImage ,ROWS, COLUMNS);
deallocateImage(inputImage, ROWS);
deallocateImage(outputImage, ROWS);
printf("The coordinates are (x,y)=(%d,%d) .\n",x_max,y_max);
}
unsigned char** createImage(unsigned char** inputImage, int rows, int columns, int x_max, int y_max ) {
int i,j;
float temp=0.0;
unsigned char **outputImage = allocateImage(rows, columns);
int max = -1 ;
for( i= 0; i< rows; i++ ) {
for( j= 0; j< columns; j++ ) {
outputImage[i][j] = inputImage[i][j];
if ( outputImage[i][j] > max ) {
outputImage[i][j] = max;
x_max = i;
y_max = j;
}
printf("The coordinates are %d .\n",outputImage[i][j]);
}
}
printf("The coordinates are %d (x,y)=(%d,%d) .\n",max,x_max,y_max);
outputImage[x_max-1][j] = 0;
outputImage[x_max+1][j] = 0;
outputImage[i][y_max-1] = 0;
outputImage[i][y_max+1] = 0;
return outputImage;
}
这是库
#include <stdio.h>
#include <stdlib.h>
unsigned char **allocateImage(int rows, int columns)
{
unsigned char **image = (unsigned char **)malloc(rows * sizeof(unsigned char *));
int i;
for (i = 0; i < rows; i++)
{
image[i] = (unsigned char *)malloc(columns * sizeof(unsigned char));
}
return image;
}
void deallocateImage(unsigned char **image, int rows)
{
int i;
for (i = 0; i < rows; i++)
{
free(image[i]);
}
free(image);
}
unsigned char **loadImage(char *filename, int rows, int columns)
{
FILE *inputFile;
unsigned char **image = allocateImage(rows, columns);
int i,j;
if ( ((inputFile = fopen(filename,"rb")) != NULL))
{
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
fscanf(inputFile, "%c", &image[i][j]);
}
}
}
else
{
printf("Error loading image.");
}
fclose(inputFile);
return image;
}
void saveImage(char *filename, unsigned char **image, int rows, int columns)
{
FILE *outputFile;
int i,j;
if ( ((outputFile = fopen(filename,"wb")) != NULL))
{
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
fprintf(outputFile, "%c", image[i][j]);
}
}
}
else
{
printf("Error saving image.");
}
fclose(outputFile);
}
Here is the code for to find the max color in image but it returns always wrong coordinates.
The code search and find the max and return the 2D array of image but when he finds the max color it creates a cross and return the image with the cross just to know where is the position of max color.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dip.h"
#define INPUT_FILENAME "chest339x339.raw"
#define OUTPUT_FILENAME "task_4_chest339x339_out.raw"
#define ROWS 339
#define COLUMNS 339
#define GREYLEVELS 256
#define OUTPUT_ROWS ROWS
#define OUTPUT_COLUMNS COLUMNS
unsigned char** createImage(unsigned char** inputImage, int rows, int columns, int x_max, int y_max ) ;
int main(void) {
unsigned char **inputImage,
**outputImage;
int x_max=0,y_max=0;
FILE *inputFile,
*outputFile;
inputImage = allocateImage(ROWS, COLUMNS);
inputImage = loadImage(INPUT_FILENAME, ROWS, COLUMNS);
outputImage = createImage(inputImage,ROWS, COLUMNS,x_max,y_max);
saveImage( OUTPUT_FILENAME,outputImage ,ROWS, COLUMNS);
deallocateImage(inputImage, ROWS);
deallocateImage(outputImage, ROWS);
printf("The coordinates are (x,y)=(%d,%d) .\n",x_max,y_max);
}
unsigned char** createImage(unsigned char** inputImage, int rows, int columns, int x_max, int y_max ) {
int i,j;
float temp=0.0;
unsigned char **outputImage = allocateImage(rows, columns);
int max = -1 ;
for( i= 0; i< rows; i++ ) {
for( j= 0; j< columns; j++ ) {
outputImage[i][j] = inputImage[i][j];
if ( outputImage[i][j] > max ) {
outputImage[i][j] = max;
x_max = i;
y_max = j;
}
printf("The coordinates are %d .\n",outputImage[i][j]);
}
}
printf("The coordinates are %d (x,y)=(%d,%d) .\n",max,x_max,y_max);
outputImage[x_max-1][j] = 0;
outputImage[x_max+1][j] = 0;
outputImage[i][y_max-1] = 0;
outputImage[i][y_max+1] = 0;
return outputImage;
}
here is the library h
#include <stdio.h>
#include <stdlib.h>
unsigned char **allocateImage(int rows, int columns)
{
unsigned char **image = (unsigned char **)malloc(rows * sizeof(unsigned char *));
int i;
for (i = 0; i < rows; i++)
{
image[i] = (unsigned char *)malloc(columns * sizeof(unsigned char));
}
return image;
}
void deallocateImage(unsigned char **image, int rows)
{
int i;
for (i = 0; i < rows; i++)
{
free(image[i]);
}
free(image);
}
unsigned char **loadImage(char *filename, int rows, int columns)
{
FILE *inputFile;
unsigned char **image = allocateImage(rows, columns);
int i,j;
if ( ((inputFile = fopen(filename,"rb")) != NULL))
{
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
fscanf(inputFile, "%c", &image[i][j]);
}
}
}
else
{
printf("Error loading image.");
}
fclose(inputFile);
return image;
}
void saveImage(char *filename, unsigned char **image, int rows, int columns)
{
FILE *outputFile;
int i,j;
if ( ((outputFile = fopen(filename,"wb")) != NULL))
{
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
fprintf(outputFile, "%c", image[i][j]);
}
}
}
else
{
printf("Error saving image.");
}
fclose(outputFile);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
头文件用于声明,所有定义都应在您编译的
.c
文件中。使用
x_max
&的地址调用createImage()
y_max
。Header files are used for declarations, all definitions are expected in
.c
file which you compile.Call
createImage()
with the addresses ofx_max
&y_max
.