- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从灰度图像获取二维矩阵并对其进行修改,然后返回到灰度图像。
但是当尝试将图像转换为矩阵时,像素返回黑色值。
如何找到解决此问题的方法?
您可以在代码下方找到图片
Java 代码:
public class MyImageProcessing {
private SampleModel sampleModel;
public int[][] compute(File file)
{
try
{
BufferedImage img= ImageIO.read(file);
Raster raster=img.getData();
sampleModel = raster.getSampleModel();
int w=raster.getWidth(),h=raster.getHeight();
int pixels[][]=new int[w][h];
for (int x=0;x<w;x++)
{
for(int y=0;y<h;y++)
{
pixels[x][y]=raster.getSample(x,y,0);
}
}
return pixels;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public void getImage(int pixels[][])
{
int w=pixels.length;
int h=pixels[0].length;
WritableRaster raster= Raster.createWritableRaster(sampleModel, new Point(0,0));
for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
raster.setSample(i,j,0,pixels[i][j]);
}
}
BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY);
image.setData(raster);
File output=new File("C:\\Users\\salam\\Pictures\\3.png");
try {
ImageIO.write(image,"png",output);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
原始图像:
从矩阵检索图像:
最佳答案
尝试一下此代码。
如果这不是您所需要的,那么也许您可以从我的文章中学到的是,代码应该始终整洁、可读、正确缩进和注释,所有这些都增加了程序的可理解性。
注意:在转换为灰度图像时,您的图像会返回几乎相同的黑白图像,因为您的图像是黑白的,非常类似于二值图像。
我还可以建议 OpenCV ( https://opencv.org/ ),它是 BSD 许可证下的,它简单、强大并且可用于 Java - 尽管我记得在 Ubuntu 16.04 上安装它时它是一个 PITA .
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
* This class converts images to grayscale color.
*/
public class GrayscaleConverter {
/**
* Creates a new grayscaled BufferedImage object from the given source image
* by averaging each pixels RGB value.
*
* @param inputImageAbsPath the absolute path of the image file, including its name and extension.
* @return a BufferedImage object.
*/
private BufferedImage compute(String inputImageAbsPath) {
System.out.println("... Converting source image to gray scale.");
BufferedImage img = null; // image file
// Read the source image or throw an exception
try {
img = ImageIO.read(new File(inputImageAbsPath));
} catch(Exception e) {
e.printStackTrace();
}
// Get the image width and height dimensions
int width = img.getWidth();
int height = img.getHeight();
// Convert to grayscale by looping over pixels, beginning at top-most left coordinate (0,0)
for (int y = 0; y < height; y++) { // y = rows
for (int x = 0; x < width; x++) { // x = columns
// Get the pixel value at this (x,y) coordinate
int p = img.getRGB(x,y);
// Extract the alpha, R, G, B values from pixel p
int a = (p>>24) & 0xff; // Shift bits and unsign
int r = (p>>16) & 0xff;
int g = (p>>8) & 0xff;
int b = p & 0xff;
// Calculate average color (grayscale it)
int avg = (r+g+b)/3;
// Replace RGB value with avg
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
img.setRGB(x, y, p);
}
}
return img;
}
/**
* Saves the converted grayscale image. This method builds the save path from the provided file name,
* file extension, and absolute path of the folder that you want to save the image in.
*
* @param path the absolute path of the folder that you would like to save the image inside.
* @param imageName the name you would like to save the image with.
* @param imageFileType the image file extension, without the dot (.) preceding the image file type.
* @param image the BufferedImage object returned from the compute method.
*/
private void saveImage(String path, String imageName, String imageFileType, BufferedImage image) {
// Save or throw exception
try {
System.out.println("... Saving grayscale image to "
+ path.concat("\\").concat(imageName).concat(".").concat(imageFileType)); // save path displayed to user
ImageIO.write(image,
imageFileType,
new File(path.concat("\\").concat(imageName).concat(".").concat(imageFileType)));
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("... Image saved.");
}
// Driver
public static void main(String args[]) throws IOException {
/*
* Tested for .png and .jpg files. Both worked successfully.
*/
// Test
System.out.println("Testing GrayscaleConverter.\n");
String input = "*source images absolute file path including name and extension*";
String outputPath = "*absolute path to folder where you will save grayscale image in*";
String outputFileName = "*save image with this name*";
String outputFileType = "*save image with this file extension (no dot (.) e.g. png or jpg)*";
GrayscaleConverter gsc = new GrayscaleConverter();
BufferedImage convertedImage = gsc.compute(input);
gsc.saveImage(outputPath, outputFileName, outputFileType, convertedImage );
System.out.println("\nTest complete.");
}
}
我用 .png 和 .jpg 图像文件测试了我的程序,它有效。祝你好运。
关于java - 灰度图像到矩阵和矩阵到灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52869288/
我正在尝试学习 Knockout 并尝试创建一个照片 uploader 。我已成功将一些图像存储在数组中。现在我想回帖。在我的 knockout 码(Javascript)中,我这样做: 我在 Jav
我正在使用 php 编写脚本。我的典型问题是如何在 mysql 中添加一个有很多替代文本和图像的问题。想象一下有机化学中具有苯结构的描述。 最有效的方法是什么?据我所知,如果我有一个图像,我可以在数据
我在两个图像之间有一个按钮,我想将按钮居中到图像高度。有人可以帮帮我吗? Entrar
下面的代码示例可以在这里查看 - http://dev.touch-akl.com/celebtrations/ 我一直在尝试做的是在 Canvas 上绘制 2 个图像(发光,然后耀斑。这些图像的链接
请检查此https://jsfiddle.net/rhbwpn19/4/ 图像预览对于第一篇帖子工作正常,但对于其他帖子则不然。 我应该在这里改变什么? function readURL(input)
我对 Canvas 有疑问。我可以用单个图像绘制 Canvas ,但我不能用单独的图像绘制每个 Canvas 。- 如果数据只有一个图像,它工作正常,但数据有多个图像,它不工作你能帮帮我吗? va
我的问题很简单。如何获取 UIImage 的扩展类型?我只能将图像作为 UIImage 而不是它的名称。图像可以是静态的,也可以从手机图库甚至文件路径中获取。如果有人可以为此提供一点帮助,将不胜感激。
我有一个包含 67 个独立路径的 SVG 图像。 是否有任何库/教程可以为每个路径创建单独的光栅图像(例如 PNG),并可能根据路径 ID 命名它们? 最佳答案 谢谢大家。我最终使用了两个答案的组合。
我想将鼠标悬停在一张图片(音乐专辑)上,然后播放一张唱片,所以我希望它向右移动并旋转一点,当它悬停时我希望它恢复正常动画片。它已经可以向右移动,但我无法让它随之旋转。我喜欢让它尽可能简单,因为我不是编
Retina iOS 设备不显示@2X 图像,它显示 1X 图像。 我正在使用 Xcode 4.2.1 Build 4D502,该应用程序的目标是 iOS 5。 我创建了一个测试应用(主/细节)并添加
我正在尝试从头开始以 Angular 实现图像 slider ,并尝试复制 w3school基于图像 slider 。 下面我尝试用 Angular 实现,谁能指导我如何使用 Angular 实现?
我正在尝试获取图像的图像数据,其中 w= 图像宽度,h = 图像高度 for (int i = x; i imageData[pos]>0) //Taking data (here is the pr
我的网页最初通过在 javascript 中动态创建图像填充了大约 1000 个缩略图。由于权限问题,我迁移到 suPHP。现在不用标准 标签本身 我正在通过这个 php 脚本进行检索 $file
我正在尝试将 python opencv 图像转换为 QPixmap。 我按照指示显示Page Link我的代码附在下面 img = cv2.imread('test.png')[:,:,::1]/2
我试图在这个 Repository 中找出语义分割数据集的 NYU-v2 . 我很难理解图像标签是如何存储的。 例如,给定以下图像: 对应的标签图片为: 现在,如果我在 OpenCV 中打开标签图像,
import java.util.Random; class svg{ public static void main(String[] args){ String f="\"
我有一张 8x8 的图片。 (位图 - 可以更改) 我想做的是能够绘制一个形状,给定一个 Path 和 Paint 对象到我的 SurfaceView 上。 目前我所能做的就是用纯色填充形状。我怎样才
要在页面上显示图像,你需要使用源属性(src)。src 指 source 。源属性的值是图像的 URL 地址。 定义图像的语法是: 在浏览器无法载入图像时,替换文本属性告诉读者她们失去的信息。此
**MMEditing是基于PyTorch的图像&视频编辑开源工具箱,支持图像和视频超分辨率(super-resolution)、图像修复(inpainting)、图像抠图(matting)、
我正在尝试通过资源文件将图像插入到我的程序中,如下所示: green.png other files 当我尝试使用 QImage 或 QPixm
我是一名优秀的程序员,十分优秀!