gpt4 book ai didi

在c中将图像的一个区域更改为 "color"

转载 作者:行者123 更新时间:2023-11-30 17:25:21 24 4
gpt4 key购买 nike

void region_set( uint8_t array[], 
unsigned int cols,
unsigned int rows,
unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom,
uint8_t color )
{
for (int x = 0; x < ((right-left)*(bottom-top)); x++)
{
array[x] = color;
}
}

这段代码的作用是给某个图像数组区域内的每个像素设置颜色。该区域包括 [left, right-1](含)中的所有列以及 [top, Bottom-1](含)中的所有行。我尝试测试该程序,但是当我测试它时,我的图像最终将图像的整个宽度设置为颜色。当我尝试更改顶部、左侧、右侧或底部时,它仅更改了图像的高度。但左右应该改变图像的宽度。我的想法是(右-左)*(下-上),这将影响该区域中的所有像素。

我用来执行此操作的测试是:

  region_set (img, 256, 256, 100, 80, 156, 160, 128);

我不知道为什么我的图像总是将整个宽度设置为颜色,而更改左、上、右或下仅更改高度。有人可以帮助我吗?

最佳答案

您需要将 2D X/Y 坐标转换为图像数组中的 1D 索引。然后,使用两个嵌套循环来迭代所需区域就很简单了,例如(代码未测试):

for (int y = top; y <= bottom; ++y)
{
for (int x = left; x <= right; ++x)
{
int i = x + y * cols;
array[i] = color;
}
}

关于在c中将图像的一个区域更改为 "color",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27083192/

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