查找图像上的最大颜色时出现问题
Problem finding the max color on an image
这是在图像中查找最大颜色的代码,但它 return 总是错误的坐标。
代码搜索并找到图像的最大值和 return 二维数组,但是当他找到最大颜色时,它会创建一个十字和 return 带有十字的图像只是为了知道最大颜色的位置在哪里.
#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;
}
这里是图书馆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);
}
enter image description here
头文件用于声明,所有定义都在您编译的.c
文件中。
用x_max
和y_max
的地址调用createImage()
。
unsigned char** createImage(inputImage,ROWS, COLUMNS, &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];
outputImage[i][j] = inputImage[i][j];
if (outputImage[i][j] > max) {
// outputImage[i][j] = max;
max = outputImage[i][j] = max; // update max value
*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;
}
- 在单色图像中,尤其是在 X-Ray 图像中,您会在不止一处发现 max-value(白色)像素。
这是在图像中查找最大颜色的代码,但它 return 总是错误的坐标。 代码搜索并找到图像的最大值和 return 二维数组,但是当他找到最大颜色时,它会创建一个十字和 return 带有十字的图像只是为了知道最大颜色的位置在哪里.
#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;
}
这里是图书馆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);
}
enter image description here
头文件用于声明,所有定义都在您编译的
.c
文件中。用
x_max
和y_max
的地址调用createImage()
。
unsigned char** createImage(inputImage,ROWS, COLUMNS, &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];
outputImage[i][j] = inputImage[i][j];
if (outputImage[i][j] > max) {
// outputImage[i][j] = max;
max = outputImage[i][j] = max; // update max value
*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;
}
- 在单色图像中,尤其是在 X-Ray 图像中,您会在不止一处发现 max-value(白色)像素。