gpt4 book ai didi

processing - 返回 0 到 255 之间的值,指示指定位置的图像阴影

转载 作者:行者123 更新时间:2023-12-04 08:41:13 24 4
gpt4 key购买 nike

我有一个单色(黑白)图像,我试图提取一个 0 到 255 之间的值,表示像素的阴影,0 是黑色,255 是白色。我按如下方式导入图像:

PImage img;

void setup() {
size(1000, 1000);
img = loadImage("myimage.jpg");
x_location = 50;
y_location = 230;
// add code to extract shade from img here
}
我正在尝试使用 get()提取阴影。不幸的是,当我尝试使用 img.get(x_location, y_location) 时它返回一个非常大的负数(大约 -1000000)。有没有办法确保 get()返回一些标准化(可以理解)的值?

最佳答案

这是非常有趣的。这是一些框架代码,可以帮助您获得所需的内容:

PImage img;
color currentColor;

void setup() {
size(656, 354);
currentColor = color(0);
img = loadImage("bean.jpeg");
}

void draw() {
background(0);
image(img, 0, 0);

fill(currentColor);
stroke(200);
strokeWeight(2);
ellipse(30, 30, 40, 40);
fill(0);
text("A " + alpha(currentColor), 10, 65);
text("R " + red(currentColor), 10, 75);
text("G " + green(currentColor), 10, 85);
text("B " + blue(currentColor), 10, 95);
}

void mouseClicked() {
currentColor = img.get(mouseX, mouseY);
}
看起来像这样:
Picking colors!
这里的神奇之处在于知道您可以提取作为颜色一部分的 ARGB 信息。然而,我也想知道为什么它是一个负数。 Here's why (强调我的):

Colors, in Processing, are stored actually in simple Java ints, 32-bitvalues. The color pseudo-type is actually replaced by Processing withint when generating Java code before compilation.

This type has a width of 32 bits, which is perfect as we can put 4channels of 8 bits each inside. 8 bits allow a range of of values from0 to 255 (included). The 4 channels are alpha (opacity), red, green,blue, the whole being often abbreviated as ARGB.

Low value of color channel means "low intensity", darkness. High valuemeans "high intensity", lightness. So if all channels are 0, we haveblack; if they are all at 255, we have white. Alpha channel isdifferent: 0 means low opacity, fully transparent, while 255 meanshigh opacity, normal opaque color.

In Java, numbers are always signed. In computing, negative numbers aremarked by setting the highest bit to 1. So, opaque colors, the mostcommon kind, the default if no opacity is given, is 0xFF = 255, thehigh bit is set to 1, the color value is negative. Hence the answer tothe first question...

The strange values are the result of combining all the values of thechannels. Let's take a simple yellowish color. Alpha is 255 (opaque),red is 250, green is 230 and blue is, say, 20. These values are 0xFF,0xFA, 0xE6 and 0x14. Combined to make an int, it gives 0xFFFAE614, ie.-334316. Hence a strange number, not very easy to decipher.


如果您的图像确实是灰度的,您可以使用 RGB 信息的任何部分。玩得开心!

关于processing - 返回 0 到 255 之间的值,指示指定位置的图像阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64561898/

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