gpt4 book ai didi

java - 为什么 C++ 和 Java 版本的结果不同?

转载 作者:太空宇宙 更新时间:2023-11-04 15:02:14 25 4
gpt4 key购买 nike

我在 C++ ndk 中实现了 Java 图像过滤器代码,但结果不同。两者的值都是 70。

Java 版本:

public Bitmap applyContrastEffect(Bitmap src, double value) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// get contrast value
double contrast = Math.pow((100 + value) / 100, 2);
// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
// apply filter contrast for every channel R, G, B
R = Color.red(pixel);
R = (int) (((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (R < 0) {
R = 0;
} else if (R > 255) {
R = 255;
}

G = Color.red(pixel);
G = (int) (((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (G < 0) {
G = 0;
} else if (G > 255) {
G = 255;
}

B = Color.red(pixel);
B = (int) (((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (B < 0) {
B = 0;
} else if (B > 255) {
B = 255;
}
int val = Color.argb(A, R, G, B);
// set new pixel color to output bitmap
bmOut.setPixel(x, y, val);
}
}

// return final image
return bmOut;
}

C++ 版本:

int* ContrastFilter::procImage() {
double value = 70;
double contrast = pow((100 + value) / 100, 2);
int A, R, G, B;

for (int i = 0; i < width * height; ++i) {
//__android_log_print(ANDROID_LOG_VERBOSE, "Contrast Filter","The before color is %d", this->pixels[i]);
Color color(this->pixels[i]);
A = color.alpha();

R = color.R();
R = (int) (((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (R < 0) {
R = 0;
} else if (R > 255) {
R = 255;
}

G = color.G();
G = (int) (((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (G < 0) {
G = 0;
} else if (G > 255) {
G = 255;
}
B = color.B();
B = (int) (((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (B < 0) {
B = 0;
} else if (B > 255) {
B = 255;
}

this->pixels[i] = ARGB2Color(A, R, G, B);
}

return this->pixels;

Java C++ 接口(interface):

public static Bitmap changeToContrast(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap returnBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

int[] returnPixels = NativeFilterFunc.contrastFilter(pixels, width,
height);
returnBitmap.setPixels(returnPixels, 0, width, 0, 0, width, height);

return returnBitmap;

}

Java 结果:

enter image description here

C++ 结果:

enter image description here

我不明白为什么结果不同。我调试的时候看到有些像素值不一样,为什么计算公式一样会出现这种情况?

最佳答案

R = Color.red(pixel);
...
G = Color.red(pixel);
...
B = Color.red(pixel);

您正在为 Java 代码中的每种颜色取红色值。您可能想要调整它以获取正确的值,而不是像这样:

R = Color.red(pixel);
...
G = Color.green(pixel);
...
B = Color.blue(pixel);

旁注:根据 Java 约定,变量名应以小写 字母开头。

关于java - 为什么 C++ 和 Java 版本的结果不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30070239/

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