C++ 正确翻转/镜像图像的像素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2647376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:12:35  来源:igfitidea点击:

Correct flip/mirror of pixels of an image?

c++

提问by codefail

http://tinypic.com/r/fwubzc/5

http://tinypic.com/r/fwubzc/5

That shows what a flip should be and what a mirror should be.

这表明翻转应该是什么,镜子应该是什么。

Code for both types of mirrors:

两种类型镜像的代码:

void mirrorLeftRight()
{
    for (int x = 0; x < width/2; x++) {
            for (int y = 0; y < height; y++) {
                int temp = pixelData[x][y];
                pixelData[x][y]=pixelData[width-x][y]
                pixelData[width-x][y]=temp;
            }
    }
}

void mirrorUpDown()
{
    for (int x = 0; x < width; x++) {
            for (int y = 0; y < height/2; y++) {
                int temp = pixelData[x][y];
                pixelData[x][y]=pixelData[x][height-y]
                pixelData[x][height-y]=temp;
            }
    }
}

Does this seem right for mirrors?

这看起来适合镜子吗?

And for flip, just a matter of using widthand heightw/o dividing by 2?

而对于翻转,只是使用widthheightw/o 除以 2 的问题?

回答by interjay

You need to use width-1-xinstead of width-x, and height-1-yinstead of height-y. Otherwise for x==0 you'll try to index [width], which is outside the array.

您需要使用width-1-x而不是width-x,而height-1-y不是height-y。否则,对于 x==0,您将尝试对数组外的 [width] 进行索引。

回答by Hyman

It shouldn't work since you are swappingpixels while you just have to override the right part of the image with the left part. Same thing applies to the mirrorUpDown.

它不应该工作,因为您正在交换像素,而您只需要用左侧部分覆盖图像的右侧部分。同样的事情适用于mirrorUpDown.

If you swap them you obtain a flip, if you overwrite them you obtain a mirror.

如果你交换它们,你会得到一个翻转,如果你覆盖它们,你会得到一面镜子。

  • mirrorLeftRight: take pixels from left half and use them to overwrite right part
  • mirrorUpDown: take pixels from upper part and use them to overwrite lower one
  • flip: in this case you don't overwrite but you swap pixels (source half it's not influent in this case)
  • mirrorLeftRight:从左半部分取像素并用它们覆盖右部分
  • mirrorUpDown:从上部取像素并用它们覆盖下部像素
  • 翻转:在这种情况下,您不会覆盖,但会交换像素(源一半在这种情况下不受影响)

回答by Adriaan Stander

The code above seems more like the correct way to Flip, not mirror.

上面的代码看起来更像是正确的翻转方式,而不是镜像。

Mirror I would guess that you not switch the pixels, but rather copy from one side to the other.

Mirror 我猜你不是切换像素,而是从一侧复制到另一侧。

With mirror I would guess that you need to change

用镜子我猜你需要改变

int temp = pixelData[x][y]; 
pixelData[x][y]=pixelData[width-x][y] 
pixelData[width-x][y]=temp;

to something like this only

仅此而已

pixelData[x][y]=pixelData[width-x][y]