gpt4 book ai didi

android - 灰度图像在 NDK/C++ 中失去亮度)Android

转载 作者:行者123 更新时间:2023-11-30 01:53:41 25 4
gpt4 key购买 nike

我正在传递带有 ARGB_8888 配置的位图。我能够对图像应用灰度效果,但应用后我失去了它的亮度。

我用谷歌搜索了很多,但发现了与我相同的实现。

这是我的原生实现::

JNIEXPORT void JNICALL Java_com_example_ndksampleproject_MainActivity_jniConvertToGray(JNIEnv * env, jobject  obj, jobject bitmapcolor,jobject bitmapgray)
{
AndroidBitmapInfo infocolor;
void* pixelscolor;
AndroidBitmapInfo infogray;
void* pixelsgray;
int ret;
int y;
int x;

LOGI("convertToGray");
if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return;
}

if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return;
}

LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infocolor.width,infocolor.height,infocolor.stride,infocolor.format,infocolor.flags);
if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("Bitmap format is not RGBA_8888 !");
return;
}

LOGI("gray image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infogray.width,infogray.height,infogray.stride,infogray.format,infogray.flags);
if (infogray.format != ANDROID_BITMAP_FORMAT_A_8) {
LOGE("Bitmap format is not A_8 !");
return;
}

if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
}

if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
}

LOGI("unlocking pixels height = %d",infocolor.height);

// modify pixels with image processing algorithm

for (y=0;y<infocolor.height;y++) {
argb * line = (argb *) pixelscolor;
uint8_t * grayline = (uint8_t *) pixelsgray;
for (x=0;x<infocolor.width;x++) {
grayline[x] = ((255-0.3 * line[x].red) + (255-0.59 * line[x].green) + (255-0.11*line[x].blue))/3;
}

pixelscolor = (char *)pixelscolor + infocolor.stride;
pixelsgray = (char *) pixelsgray + infogray.stride;
}

LOGI("unlocking pixels");
AndroidBitmap_unlockPixels(env, bitmapcolor);
AndroidBitmap_unlockPixels(env, bitmapgray);
}

结果::

enter image description here

如果您需要我这边的任何东西,请告诉我..请帮我解决这个问题,因为我在很多小时内都陷入了这个问题。非常感谢!

编辑::

应用 Mark Setchell 的建议后::

enter image description here

已编辑

如果你颠倒上面的图像,你会得到这个 - 这对我来说是正确的:

enter image description here

最佳答案

不要在计算 grayline[x] 的线上除以 3。您的答案已经正确加权,因为 0.3 + 0.59 + 0.11 = 1

grayline[x] = (255-0.3 * line[x].red) + (255-0.59 * line[x].green) + (255-0.11*line[x].blue);

关于android - 灰度图像在 NDK/C++ 中失去亮度)Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22931230/

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