9Mcode comment |
|
|
|
|
|
|
- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -
|
|
|
กระทู้ #2442 [Java] (จาก IP: 125.26.182.105)
แปลงฺBufferedImageให้ไปเป็นฺค่าBinaryยังไงอ่ะครับพี่ช่วยหน่อยนะครับ
ตอนนี้ผมทำProject เกี่ยวกับ ImageProcessing อยู่อ่ะครับต้องการที่จะแปลงค่ารูปภาพให้ไปอยู่ในฐานสองเพื่อจะเอาค่าไปคำนวณด้านต่างๆเช่น หาค่า Histogram อะไรประมาณนี้อ่ะครับพี่ช่วยผมหน่อยนะครับ ขอบคุณมากครับ
|
จากคุณ
:
Nut / unji_nut@hotmail.com [2009-01-19 01:11:52]
|
|
ความคิดเห็น #27299 (จาก IP: 125.26.182.105)
ลืมไปครับขอเป็น JAVA นะครับ |
จากคุณ
:
Nut / unji_nut@hotmail.com [2009-01-19 01:13:28]
|
|
ความคิดเห็น #27300 (จาก IP: 202.142.217.162)
/**Converts the colors in the BufferedImage to their nearest color in * the colorPallet using a modified Euclidean Distance. The user can specify * whether Floyd-Steinberg dithering should occur during this process. * At the end of this method the image will have only those colors present * in the given color pallet. * @param image the image to posterize * @param colorPallet the argb values of the colors in your color pallet * @param dither specifyes whether to use dithering during the posterization * process. */ public static void posterize(BufferedImage image, int[] colorPallet, boolean dither) { //cache the color components of each color in the pallet. This //is because these are used often in the lifetime of the for loop int[][] cpComp= new int[colorPallet.length][3]; for(int k = 0; k < colorPallet.length; k++) { cpComp[k][0] = red(colorPallet[k]); cpComp[k][1] = green(colorPallet[k]); cpComp[k][2] = blue(colorPallet[k]); } for(int y = 0; y < image.getHeight(); y++) { for(int x = 0; x < image.getWidth(); x++) { int pixel = image.getRGB(x,y); //the color components of the pixel. int pixelR = red(pixel); int pixelG = green(pixel); int pixelB = blue(pixel); //find the closest color in the colorPallet to the pixel int minimumDistance = 255*255 + 255*255 + 255*255 + 1; int ncIndex = -1; //index of the nearest color in the color pallet for (int j = 0; j < colorPallet.length; j++) { int rDiff = pixelR - cpComp[j][0]; int gDiff = pixelG - cpComp[j][1]; int bDiff = pixelB - cpComp[j][2]; int distance = (int) (rDiff * rDiff * .299 + gDiff * gDiff * .587 + bDiff * bDiff * .114); if (distance < minimumDistance) { minimumDistance = distance; ncIndex = j; } } //set the current pixel to the nearest color image.setRGB(x, y, colorPallet[ncIndex]); if(!dither) continue; //error distribution to the surrouding pixels for(int i = 0; i < 4; i++) { int xCoor = i==0?x+1: i==1?x-1: i==2?x: x+1; int yCoor = i==0?y:y+1; if(xCoor < 0 || xCoor >= image.getWidth() || yCoor >= image.getHeight()) continue; double factor = i==0?7.0/16: i==1?3.0/16: i==2?5.0/16: 1.0/16; int p = image.getRGB(xCoor,yCoor); int newRed = (int) Math.round(red(p) + factor * (pixelR - cpComp[ncIndex][0])); int newGreen = (int) Math.round(green(p) + factor* (pixelG - cpComp[ncIndex][1])); int newBlue = (int) Math.round(blue(p)+ factor* (pixelB - cpComp[ncIndex][2])); if(newRed < 0) newRed = 0; if(newRed > 255) newRed = 255; if(newGreen < 0) newGreen = 0; if(newGreen > 255) newGreen = 255; if(newBlue < 0) newBlue = 0; if(newBlue > 255) newBlue = 255; image.setRGB(xCoor, yCoor, (255<< 24)+ (newRed << 16) + (newGreen << 8) + newBlue); } } } } /**Convenience method that returns the red component in an argb integer.*/ public static int red(int argb) {return (argb >> 16) & 0xff;} /**Convenience method that returns the green component in an argb integer.*/ public static int green(int argb) {return (argb >> 8) & 0xff;} /**Convenience method that returns the blue component in an argb integer.*/ public static int blue(int argb) {return (argb ) & 0xff;} |
จากคุณ
:
sup98 [2009-01-19 10:20:03]
|
|
ความคิดเห็น #27301 (จาก IP: 202.142.217.162)
BufferedImage image = ImageIO.read(...); BufferedImage fgImage = //foreground image new BufferedImage(image.getWidth(),image.getHeight(), BufferedImage.TYPE_INT_RGB); for(int x = 0; x < fgImage.getWidth(); x++) { for(int y = 0; y < fgImage.getHeight(); y++) { int rgbSrc = image.getRGB(x, y); if(rgbSrc == rgbForeground) { fgImage.setRGB(x,y,rgbForeground); //remain in color }else{ int r = (rgbSrc>>16)&0xff; int g = (rgbSrc>> 8)&0xff; int b = (rgbSrc )&0xff; int lum = (int) (.299 * r + .587 * g + .144 * b); //set to grayscale fgImage.setRGB(x,y,(255<<24)|(lum<<16)|(lum<<8)|lum); } } } |
จากคุณ
:
sup98 [2009-01-19 10:21:46]
|
|
ความคิดเห็น #27302 (จาก IP: 125.26.182.105)
ขอบคุณมากครับพี่.... |
จากคุณ
:
Nut / unji_nut@hotmail.com [2009-01-19 16:20:52]
|
|
|
- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -
|
|
|
|
|
|
|