gpt4 book ai didi

c - 从reg-green-blue图像到c中的灰度图像

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:11 24 4
gpt4 key购买 nike

我正在编写代码来转换 RGB 格式的图像

RGB RGB RGBRGB RGB RGBRGB RGB RGBRGB RGB RGB

试图转换为灰度 img,但它不起作用。我得到一张没有着色的照片,但不像预期的那样。更像是黑白而不是灰色。

struct Img {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};

void convernt(Img *image)
{
int x,y,i, iRow;
char* p = image->data;
int width=image->sizeX;
int height=image->sizeY;
int rowSize = width*3; // number of bytes in one row
int iPix;
for ( y = 0; y < height; ++y)
{
iRow = y*rowSize ; // index of first pixel in row y
for ( x = 0; x < rowSize; x+=3)
{
iPix = iRow + x*3; // index of pixel
double con=0.2989*p[iPix]+ 0.5870*p[iPix+1]+0.1140*p[iPix+2];
p[iPix] = con;
p[iPix+1] = con;
p[iPix+2] = con;

}
}
}

我希望代码与这个相同但更短应该做完全一样的我想我错过了什么

#define NUM_OF_COLORS   256

// perform histogram equalization algorithm on an image
void convert(Img *image)
{

int i,j;

double* orgHist = (double*)malloc(NUM_OF_COLORS*sizeof(double));
unsigned char* conversionVector = (unsigned char*)malloc(NUM_OF_COLORS);

//clear the vector
for (i=0; i<NUM_OF_COLORS; i++) {
orgHist[i] = 0;
}

//get the histogram of the image
for (j=0; j<image->sizeY; j++) {
for (i=0; i<image->sizeX; i++) {
orgHist[(unsigned char)image->data[3*i + 3*j*image->sizeX + 1]]++;
}
}

//calculate the accumulated histogram of the image
for (i=1; i<NUM_OF_COLORS; i++) {
orgHist[i] = orgHist[i] + orgHist[i-1];
}

//normalize the histogram
for (i=0; i<NUM_OF_COLORS; i++) {
orgHist[i] = orgHist[i]/(image->sizeX*image->sizeY);
conversionVector[i] = 0;
}


// preform the histogram equalization algorithm
i = 0;
j = 0;
while (i<NUM_OF_COLORS) {

if ((((double)j+1)/256) < orgHist[i]) {
j++;
} else {
conversionVector[i] = j;
i++;
}
}

// apply the conversion vector on the image
for (i=0; i<image->sizeX; i++) {
for (j=0; j<image->sizeY; j++) {
image->data[3*i + 3*j*image->sizeX + 1] =
conversionVector[(unsigned char)image->data[3*i + 3*j*image->sizeX + 1]];
}
}

// copy G values to R&B
for (i = 0; i < image->sizeX*image->sizeY; i++) {
image->data[3*i] = image->data[3*i+1];
image->data[3*i+2]= image->data[3*i+1];
}

free(orgHist);
free(conversionVector);
}

最佳答案

要么在 for 循环中将 x 递增 3,要么将 x*3 添加到指数。但是不要两者都。:

for (  x = 0; x < rowSize; x+=3)
// ^^^ either x++ ...
{
iPix = iRow + x*3; // index of pixel
// ^^ ... or iRow + x;
...
}

因为 rowSize 是字节数而不是像素数,所以调整您的代码如下:

for (  x = 0; x < rowSize; x+=3 )
{
iPix = iRow + x; // index of pixel
double con = 0.2989*p[iPix] + 0.5870*p[iPix+1] + 0.1140*p[iPix+2];
p[iPix] = (char)con;
p[iPix+1] = (char)con;
p[iPix+2] = (char)con;
}

这里是大图像的解决方案:

#define NUM_OF_COLORS   256

unsigned char rTable[NUM_OF_COLORS];
unsigned char gTable[NUM_OF_COLORS];
unsigned char bTable[NUM_OF_COLORS];

void initTables()
{
for ( int c = 0; c < NUM_OF_COLORS; c ++ )
{
rTable[c] = (unsigned char)(0.2989*c);
gTable[c] = (unsigned char)(0.5870*c);
bTable[c] = (unsigned char)(0.1140*c);
}
}

void convernt(struct Img *image)
{
unsigned char* p = (unsigned char*)image->data;
int width=image->sizeX;
int height=image->sizeY;
int rowSize = width*3; // number of bytes in one row
for ( int y = 0; y < height; ++y)
{
int iRow = y*rowSize ; // index of first pixel in row y
for ( int x = 0; x < rowSize; x+=3 )
{
int iPix = iRow + x; // index of pixel
unsigned char col = rTable[p[iPix]] + gTable[p[iPix+1]] + bTable[p[iPix+2]];
p[iPix] = col;
p[iPix+1] = col;
p[iPix+2] = col;
}
}
}

关于c - 从reg-green-blue图像到c中的灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34679039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com