在图像上找到最大颜色的问题

发布于 2025-01-19 07:18:46 字数 3817 浏览 4 评论 0原文

这是查找图像中最大颜色的代码,但它总是返回错误的坐标。 代码搜索并找到最大值并返回图像的二维数组,但是当他找到最大颜色时,它会创建一个十字并返回带有十字的图像,只是为了知道最大颜色的位置在哪里。

#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);
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2025-01-26 07:18:47
  1. 头文件用于声明,所有定义都应在您编译的 .c 文件中。

  2. 使用x_max &的地址调用createImage() y_max

unsigned char** createImage(inputImage,ROWS, COLUMNS, &x_max, &y_max);
  1. 然后函数接受地址和用所需的像素索引填充它们:
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;
}
  1. 在单色图像中,尤其是在 X 射线图像中,您会在多个位置找到最大值(白色)像素。
  1. Header files are used for declarations, all definitions are expected in .c file which you compile.

  2. Call createImage() with the addresses of x_max & y_max.

unsigned char** createImage(inputImage,ROWS, COLUMNS, &x_max, &y_max);
  1. 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;
}
  1. In monochromatic images, especially in X-Ray images, you'll find max-value(white) pixels at more than one place.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文