在Java OpenCV中,不使用任何方法将图像转换为灰度图

2023年 8月 28日 72.7k 0

将彩色图像转换为灰度。

  • 获取每个像素的红绿蓝值

  • 获取这 3 种颜色的平均值。

  • 用平均值替换 RGB 值。

  • 根据修改后的颜色创建新的像素值。

  • 将新值设置为像素。

示例

import java.io.File;
import java.io.IOException;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Color2Grey {
public static void main(String args[])throws IOException {
//Reading the image
File file= new File("D:Imagescar.jpg");
BufferedImage img = ImageIO.read(file);
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
//Retrieving the values of a pixel
int pixel = img.getRGB(x,y);
//Creating a Color object from pixel value
Color color = new Color(pixel, true);
//Retrieving the R G B values
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
//Finding the average of the red green blue values
int average = (red+green+blue)/3;
//Creating new Color object
color = new Color(average, average, average);
//Setting new Color object to the image
img.setRGB(x, y, color.getRGB());
}
}
//Saving the modified image
file = new File("D:Imagesgrey_image.jpg");
ImageIO.write(img, "jpg", file);
System.out.println("Done...");
}
}

登录后复制

输入

在Java OpenCV中,不使用任何方法将图像转换为灰度图

输出

以上就是在Java OpenCV中,不使用任何方法将图像转换为灰度图的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论