I’m trying to use imageYUV420CenterCropToFloat32Tensor to convert an image in a YUV420 format, and I’m getting odd results. If I just try to convert the RGB tensor back directly to YUV, I get artifacts where the image seems interleaved with the image duplicated 4 times with some color changes.
I’m probably misunderstanding something about YUV420 encoding. Looking at the code here (pytorch/pytorch_vision_jni.cpp at master · pytorch/pytorch · GitHub 4). The y dimension seems to be halved twice, once in the (yBeforeRtn >> 1) and another time in a previous line (int uvRowStride = uRowStride >> 1). However, the x dimension isn’t halved at all.
My understanding is that both x and y dimensions are halved in 420 subsampling, so this struck me as odd. For fun, if I just take my YUV image and read the UV indices as this function does and then write back to the UV buffers as I expected the format to be, I get similar artifacts.
E.x:
for (i in 0 until (imageHeight shr 1)) {
  for (j in 0 until imageWidth) {
    uBuffer.put((j / 2) * uPixelStride + i * uRowStride, uBufferOrig!!.get(j * uPixelStride + i * uRowStride / 2))
    vBuffer.put((j / 2) * vPixelStride + i * vRowStride, vBufferOrig!!.get(j * vPixelStride + i * vRowStride / 2))
  }
}