获取图像中的像素值

时间:2022-12-17 23:21:52

I am calculating the RGB values of pixels in my captured photo. I have this code

我正在计算拍摄照片中像素的RGB值。我有这个代码

func getPixelColorAtLocation(context: CGContext, point: CGPoint) -> Color {

    self.context = createARGBBitmapContext(imgView.image!)

    let data = CGBitmapContextGetData(context)
    let dataType = UnsafePointer<UInt8>(data)

    let offset = 4 * ((Int(imageHeight) * Int(point.x)) + Int(point.y))
    var color = Color()
    color.blue = dataType[offset]
    color.green = dataType[offset + 1]
    color.red = dataType[offset + 2]
    color.alpha = dataType[offset + 3]
    color.point.x = point.x
    color.point.y = point.y

But I am not sure what this line means in the code.

但我不确定这行代码中的含义。

let offset = 4 * ((Int(imageHeight) * Int(point.x)) + Int(point.y))

Any help?? Thanks in advance

任何帮助?提前致谢

1 个解决方案

#1


0  

Image is the set of pixels. In order to get the pixel at (x,y) point, you need to calculate the offset for that set.

图像是像素集。为了获得(x,y)点处的像素,您需要计算该集合的偏移量。

If you use dataType[0], it has no offset 'cos it points to the place where the pointer is. If you used dataType[10], it would mean you took 10-th element from the beginning where the pointer is.

如果使用dataType [0],则它没有偏移'因为它指向指针所在的位置。如果你使用了dataType [10],那就意味着你从指针所在的开头拿了第10个元素。

Due to the fact, we have RGBA colour model, you should multiply by 4, then you need to get what offset by x (it will be x), and by y (it will be the width of the image multiplied by y, in order to get the necessary column) or:

由于事实,我们有RGBA颜色模型,你应该乘以4,然后你需要得到x的偏移(它将是x)和y(它将是图像的宽度乘以y,在为了获得必要的列)或:

offset = x + width * y
// offset, offset + 1, offset + 2, offset + 3  <- necessary values for you

Imagine, like you have a long array with values in it.

想象一下,就像你有一个带有值的长数组。

It will be clear if you imagine the implementation of two-dimensional array in the form of one-dimensional array. It would help you, I hope.

如果你想象以一维数组的形式实现二维数组,那将是很清楚的。我希望它会对你有所帮助。

#1


0  

Image is the set of pixels. In order to get the pixel at (x,y) point, you need to calculate the offset for that set.

图像是像素集。为了获得(x,y)点处的像素,您需要计算该集合的偏移量。

If you use dataType[0], it has no offset 'cos it points to the place where the pointer is. If you used dataType[10], it would mean you took 10-th element from the beginning where the pointer is.

如果使用dataType [0],则它没有偏移'因为它指向指针所在的位置。如果你使用了dataType [10],那就意味着你从指针所在的开头拿了第10个元素。

Due to the fact, we have RGBA colour model, you should multiply by 4, then you need to get what offset by x (it will be x), and by y (it will be the width of the image multiplied by y, in order to get the necessary column) or:

由于事实,我们有RGBA颜色模型,你应该乘以4,然后你需要得到x的偏移(它将是x)和y(它将是图像的宽度乘以y,在为了获得必要的列)或:

offset = x + width * y
// offset, offset + 1, offset + 2, offset + 3  <- necessary values for you

Imagine, like you have a long array with values in it.

想象一下,就像你有一个带有值的长数组。

It will be clear if you imagine the implementation of two-dimensional array in the form of one-dimensional array. It would help you, I hope.

如果你想象以一维数组的形式实现二维数组,那将是很清楚的。我希望它会对你有所帮助。