设置颜色变量

时间:2016-11-27 18:50:29

标签: colors imagej-macro

我需要比较颜色。我想将颜色设置为变量,然后将其与使用getPixel获得的值进行比较。

但是,以下情况不起作用。似乎ImageJ不知道basecolor中的值是一种颜色。

  basecolor = 0xFFFFFF;
  rightpixel = getPixel(x, y);
  if (rightpixel == basecolor)  count++;   

1 个答案:

答案 0 :(得分:0)

您的问题出在getPixel中,它不会产生用十六进制写的颜色。 我在ImageJ上向你展示你最好的朋友:内置的宏函数代码 https://imagej.nih.gov/ij/developer/macro/functions.html 哪些文档内置像素函数,如getPixel()。

对于getPixel(),声明"请注意,RGB图像中的像素包含需要使用移位和遮罩提取的红色,绿色和蓝色分量。有关如何执行此操作的示例,请参阅拾色器工具宏。 ",并且拾色器工具宏告诉我们如何从颜色"位"到RGB。

因此,如果您想比较颜色,请执行以下操作:

basecolor=newArray(0,0,0);
rightpixel = getPixel(x,y);

//from the Color Picker Tool macro
//converts what getPixel returns into RGB (values red, green and blue)
if (bitDepth==24) {
    red = (v>>16)&0xff;  // extract red byte (bits 23-17)
    green = (v>>8)&0xff; // extract green byte (bits 15-8)
    blue = v&0xff;       // extract blue byte (bits 7-0)
}

//compare the color with your color
if(red==basecolor[0] && green==basecolor[1] && blue==basecolor[2]){
    print("Same Color");
    count++;
}

//you can also work with hex by converting the rgb to hex and then 
//comparing the strings like you did