先决条件:
null
- Java中的图像处理——读写
- Java图像处理–获取和设置像素
- Java中的图像处理——彩色图像到灰度图像的转换
- Java中的图像处理——彩色图像到负片图像的转换
- Java中的图像处理——从彩色到红-绿-蓝的图像转换
在这一组中,我们将把彩色图像转换成深褐色图像。在乌贼墨图像中,图像的Alpha分量将与原始图像相同(因为Alpha分量表示透明度)。尽管如此,RGB仍将改变,其将通过以下公式计算。
newRed = 0.393*R + 0.769*G + 0.189*B newGreen = 0.349*R + 0.686*G + 0.168*B newBlue = 0.272*R + 0.534*G + 0.131*B
如果其中任何一个输出值大于255,只需将其设置为255即可。这些特定值是Microsoft推荐的深褐色色调值。
算法:
- 获取像素的RGB值。
- 使用上述公式计算新红、新绿、新蓝(取整数值)
- 根据以下条件设置像素的新RGB值:
- 如果newRed>255,则R=255,否则R=newRed
- 如果newGreen>255,则G=255,否则G=newGreen
- 如果新蓝>255,则B=255,否则B=新蓝
- 用我们为像素计算的新值替换R、G和B的值。
- 对图像的每个像素重复步骤1至步骤4。
实施:
JAVA
// Java program to demonstrate // colored to sepia conversion import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Sepia { public static void main(String args[]) throws IOException { BufferedImage img = null ; File f = null ; // read image try { f = new File( "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png" ); img = ImageIO.read(f); } catch (IOException e) { System.out.println(e); } // get width and height of the image int width = img.getWidth(); int height = img.getHeight(); // convert to sepia for ( int y = 0 ; y < height; y++) { for ( int x = 0 ; x < width; x++) { int p = img.getRGB(x, y); int a = (p >> 24 ) & 0xff ; int R = (p >> 16 ) & 0xff ; int G = (p >> 8 ) & 0xff ; int B = p & 0xff ; // calculate newRed, newGreen, newBlue int newRed = ( int )( 0.393 * R + 0.769 * G + 0.189 * B); int newGreen = ( int )( 0.349 * R + 0.686 * G + 0.168 * B); int newBlue = ( int )( 0.272 * R + 0.534 * G + 0.131 * B); // check condition if (newRed > 255 ) R = 255 ; else R = newRed; if (newGreen > 255 ) G = 255 ; else G = newGreen; if (newBlue > 255 ) B = 255 ; else B = newBlue; // set new RGB value p = (a << 24 ) | (R << 16 ) | (G << 8 ) | B; img.setRGB(x, y, p); } } // write image try { f = new File( "C:/Users/hp/Desktop/Image Processing in Java/GFG.png" ); ImageIO.write(img, "png" , f); } catch (IOException e) { System.out.println(e); } } } |
输出:
笔记 :此代码不会在联机IDE上运行,因为它需要磁盘上的映像。
本文由 普拉蒂克·阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END