C 函数查找像素的xy位置
因为我之前的问题肯定不是很清楚。我无法实现问题的精确解决方案。我一直在研究一个函数,它返回X/Y坐标中像素的字节偏移量。为此,我有:C 函数查找像素的xy位置,c,bitmap,bmp,C,Bitmap,Bmp,因为我之前的问题肯定不是很清楚。我无法实现问题的精确解决方案。我一直在研究一个函数,它返回X/Y坐标中像素的字节偏移量。为此,我有: dword bmp_find_xy (dword xp, dword yp) { dword w = 50; // to clarify thats the real widh of the sample image i use dword bpx = (3*8); // using 3*8 as a reminder. dword off
dword bmp_find_xy (dword xp, dword yp)
{
dword w = 50; // to clarify thats the real widh of the sample image i use
dword bpx = (3*8); // using 3*8 as a reminder.
dword offset = (2+sizeof(BMP)+sizeof(DIB)); // this is the offset 54.
dword pitch = w * 3; // determining the widh of pixels. Pitch variable
dword row = w * 3; // determining the widh of pixels. Row variable.
dword pixAddress; // result variable
if(pitch % 4 != 0) pitch += 4 - (pitch % 4); // finding the pitch (row+padding)
pixAddress = (offset) + pitch * yp + ((xp * bpx) / 8); // finding the address
return pixAddress;
}
所以问题不会像“我做错了什么/为什么我会收到奇怪的错误”。问题是。。我做得对吗?在第一次测试中,它似乎起了作用。但我不知怎么的不确定。一旦确认这是正确的方法。。我将删除该问题。您的代码似乎为我提供了正确的结果。然而,它本身是不一致的
- 在行(yp)寻址中,假设每个像素有3个字节
- 在列(xp)寻址中,假设每个像素有3*8位
dword width = 50; // image width
dword channels = 3; // number of color channels
dword bpp = 8; // depth in bits
dword single = (channels*bpp)/8; // size of a pixel in bytes
dword offset = (2+sizeof(BMP)+sizeof(DIB)); // this is the offset 54.
dword rowsize = width*single; // size of a row in memory
if (rowsize % 4 != 0)
rowsize += 4 - (rowsize % 4); // account for padding
dword pixAddress; // result variable
pixAddress = offset + yp*rowsize + xp*single; // finding the address
return pixAddress;
此外,您还可以从标题中读取宽度、通道和bpp
接下来,如果您先获取行中第一个像素的地址,然后让它在该行中迭代(而不是每次都重新计算整个内容),那么代码将更快。下面是一个典型的任务在所有像素上运行的示例。请注意,我使用的编码风格与原始问题中的不同
unsigned char maxGreen = 0;
for (int y = 0; y < height; y++) {
unsigned char *row = bitmap.getRowPtr(y);
for (int x = 0; x < width; x++) {
unsigned char *pixel = row + bitmap.getColumnOffset(x);
if (pixel[2] > maxGreen)
maxGreen = pixel[2];
}
}
// maxGreen holds the maximum value in the green channel observed in the image
无符号字符maxGreen=0;
对于(int y=0;ymaxGreen)
maxGreen=像素[2];
}
}
//maxGreen保留图像中观察到的绿色通道中的最大值
如您所见,在本例中,在getRowPtr()函数中,每行只需进行一次偏移、填充等计算。每像素我们只需要在getColumnCoffset()函数中进行偏移计算(简单的乘法)。
当分解每个像素需要进行多少计算时,这会使示例更快
最后,我不会自己编写代码来读取BMP!使用一个图书馆 您的代码似乎给了我正确的结果。然而,它本身是不一致的
- 在行(yp)寻址中,假设每个像素有3个字节
- 在列(xp)寻址中,假设每个像素有3*8位
dword width = 50; // image width
dword channels = 3; // number of color channels
dword bpp = 8; // depth in bits
dword single = (channels*bpp)/8; // size of a pixel in bytes
dword offset = (2+sizeof(BMP)+sizeof(DIB)); // this is the offset 54.
dword rowsize = width*single; // size of a row in memory
if (rowsize % 4 != 0)
rowsize += 4 - (rowsize % 4); // account for padding
dword pixAddress; // result variable
pixAddress = offset + yp*rowsize + xp*single; // finding the address
return pixAddress;
此外,您还可以从标题中读取宽度、通道和bpp
接下来,如果您先获取行中第一个像素的地址,然后让它在该行中迭代(而不是每次都重新计算整个内容),那么代码将更快。下面是一个典型的任务在所有像素上运行的示例。请注意,我使用的编码风格与原始问题中的不同
unsigned char maxGreen = 0;
for (int y = 0; y < height; y++) {
unsigned char *row = bitmap.getRowPtr(y);
for (int x = 0; x < width; x++) {
unsigned char *pixel = row + bitmap.getColumnOffset(x);
if (pixel[2] > maxGreen)
maxGreen = pixel[2];
}
}
// maxGreen holds the maximum value in the green channel observed in the image
无符号字符maxGreen=0;
对于(int y=0;ymaxGreen)
maxGreen=像素[2];
}
}
//maxGreen保留图像中观察到的绿色通道中的最大值
如您所见,在本例中,在getRowPtr()函数中,每行只需进行一次偏移、填充等计算。每像素我们只需要在getColumnCoffset()函数中进行偏移计算(简单的乘法)。
当分解每个像素需要进行多少计算时,这会使示例更快
最后,我不会自己编写代码来读取BMP!使用一个图书馆 您的代码似乎给了我正确的结果。然而,它本身是不一致的
- 在行(yp)寻址中,假设每个像素有3个字节
- 在列(xp)寻址中,假设每个像素有3*8位
dword width = 50; // image width
dword channels = 3; // number of color channels
dword bpp = 8; // depth in bits
dword single = (channels*bpp)/8; // size of a pixel in bytes
dword offset = (2+sizeof(BMP)+sizeof(DIB)); // this is the offset 54.
dword rowsize = width*single; // size of a row in memory
if (rowsize % 4 != 0)
rowsize += 4 - (rowsize % 4); // account for padding
dword pixAddress; // result variable
pixAddress = offset + yp*rowsize + xp*single; // finding the address
return pixAddress;
此外,您还可以从标题中读取宽度、通道和bpp
接下来,如果您先获取行中第一个像素的地址,然后让它在该行中迭代(而不是每次都重新计算整个内容),那么代码将更快。下面是一个典型的任务在所有像素上运行的示例。请注意,我使用的编码风格与原始问题中的不同
unsigned char maxGreen = 0;
for (int y = 0; y < height; y++) {
unsigned char *row = bitmap.getRowPtr(y);
for (int x = 0; x < width; x++) {
unsigned char *pixel = row + bitmap.getColumnOffset(x);
if (pixel[2] > maxGreen)
maxGreen = pixel[2];
}
}
// maxGreen holds the maximum value in the green channel observed in the image
无符号字符maxGreen=0;
对于(int y=0;ymaxGreen)
maxGreen=像素[2];
}
}
//maxGreen保留图像中观察到的绿色通道中的最大值
如您所见,在本例中,在getRowPtr()函数中,每行只需进行一次偏移、填充等计算。每像素我们只需要在getColumnCoffset()函数中进行偏移计算(简单的乘法)。
当分解每个像素需要进行多少计算时,这会使示例更快
最后,我不会自己编写代码来读取BMP!使用一个图书馆 您的代码似乎给了我正确的结果。然而,它本身是不一致的
- 在行(yp)寻址中,假设每个像素有3个字节
- 在列(xp)寻址中,假设每个像素有3*8位
dword width = 50; // image width
dword channels = 3; // number of color channels
dword bpp = 8; // depth in bits
dword single = (channels*bpp)/8; // size of a pixel in bytes
dword offset = (2+sizeof(BMP)+sizeof(DIB)); // this is the offset 54.
dword rowsize = width*single; // size of a row in memory
if (rowsize % 4 != 0)
rowsize += 4 - (rowsize % 4); // account for padding
dword pixAddress; // result variable
pixAddress = offset + yp*rowsize + xp*single; // finding the address
return pixAddress;
此外,您还可以从标题中读取宽度、通道和bpp
接下来,如果您先获取行中第一个像素的地址,然后让它在该行中迭代(而不是每次都重新计算整个内容),那么代码将更快。H