Stefan Oltmann
12/01/2022, 12:34 AMfun main() {
OpenCV.loadLocally()
val image = Imgcodecs.imread("/Users/sol/Pictures/Testfotos/photo_1.jpg")
val targetImage = Mat(image.height(), image.width(), image.type())
Imgproc.cvtColor(image, targetImage, Imgproc.COLOR_RGB2GRAY)
Imgcodecs.imwrite("photo_1_opencv.png", targetImage)
}
And this is the way I do it in SKIA:
fun main() {
val file = "/Users/sol/Pictures/Testfotos/photo_1.jpg"
val image = Image.makeFromEncoded(File(file).readBytes())
val surface = Surface.makeRasterN32Premul(image.width, image.height)
surface.canvas.drawImageRect(
image,
Rect(0f, 0f, image.width.toFloat(), image.height.toFloat()),
Paint().apply {
colorFilter = ColorFilter.makeMatrix(
ColorMatrix(
0.299000F, 0.587000F, 0.114000F, 0F, 0F,
0.299000F, 0.587000F, 0.114000F, 0F, 0F,
0.299000F, 0.587000F, 0.114000F, 0F, 0F,
0F, 0F, 0F, 1F, 0F
)
)
}
)
val newImage = surface.makeImageSnapshot()
File("photo_1_skia.png").writeBytes(
newImage.encodeToData()!!.bytes
)
}
According to https://docs.opencv.org/4.x/de/d25/imgproc_color_conversions.html this should be the right color matrix.
But the result differs. The SKIA version is a bit brighter. I want it to have the same pixel values.
What is my mistake here?Stefan Oltmann
12/01/2022, 8:06 AMczuckie
12/01/2022, 8:44 AMStefan Oltmann
12/01/2022, 8:45 AMczuckie
12/01/2022, 8:45 AMczuckie
12/01/2022, 8:46 AMStefan Oltmann
12/01/2022, 8:51 AMczuckie
12/01/2022, 8:52 AMStefan Oltmann
12/01/2022, 8:53 AMMichael Paus
12/01/2022, 11:18 AMStefan Oltmann
12/01/2022, 3:13 PMfun main() {
val file = "/Users/sol/Pictures/Testfotos/photo_1.jpg"
val image = Image.makeFromEncoded(File(file).readBytes())
val colorInfo = ColorInfo(
colorType = ColorType.RGBA_8888,
alphaType = ColorAlphaType.UNPREMUL, // <-- has no effect
colorSpace = ColorSpace.sRGB
)
val imageInfo = ImageInfo(
colorInfo = colorInfo,
width = image.width,
height = image.height
)
val surface = Surface.makeRaster(imageInfo)
surface.canvas.drawImageRect(
image,
Rect(0f, 0f, image.width.toFloat(), image.height.toFloat()),
Paint().apply {
colorFilter = ColorFilter.makeMatrix(
ColorMatrix(
0.299000F, 0.587000F, 0.114000F, 0F, 0F,
0.299000F, 0.587000F, 0.114000F, 0F, 0F,
0.299000F, 0.587000F, 0.114000F, 0F, 0F,
0F, 0F, 0F, 1F, 0F
)
)
}
)
val newImage = surface.makeImageSnapshot()
File("photo_1_skia.png").writeBytes(
newImage.encodeToData()!!.bytes
)
}
Stefan Oltmann
12/01/2022, 3:18 PMColorType.GRAY_8
does not change the result. If I change the color type and remove the colorFilter, the result is different, but still not the same as OpenCV.Stefan Oltmann
12/01/2022, 3:31 PMStefan Oltmann
12/02/2022, 8:03 AMImgproc.COLOR_RGB2GRAY
must be changed to Imgproc.COLOR_BGR2GRAY
because OpenCV does load images BGR instead of RGB. That's wild.