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);
}
CodePudding user response:
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
.
unsigned char** createImage(inputImage,ROWS, COLUMNS, &x_max, &y_max);
- Then function accepts addresses & fills them with desired pixel indexes :
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;
}
- In monochromatic images, especially in X-Ray images, you'll find max-value(white) pixels at more than one place.