2016-11-27 138 views
0

我需要比較顏色。我想將一種顏色設置爲一個變量,然後將其與使用getPixel獲得的值進行比較。設置顏色變量

但是,以下不起作用。看起來像ImageJ並不知道basecolor中的值是一種顏色。

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

回答

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