- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试编译以下代码,但发现仅对于我输入用于测试的所有图像,图像才会变成全黑。
附注我是一个Java新手,除了内置包之外,我不允许使用任何包
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
public class ImageProcessor
{
public static BufferedImage convert(Image img)
{
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bi.getGraphics();
bg.drawImage(img, 0, 0, null);
bg.dispose();
return bi;
}
public static BufferedImage toGrayScale(Image img)
{
// Convert image from type Image to BufferedImage
BufferedImage bufImg = convert(img);
// Scan through each row of the image
for(int j=0; j < bufImg.getHeight(); j++)
{
// Scan through each columns of the image
for(int i=0; i < bufImg.getWidth(); i++)
{
// Returns an integer pixel in the default RGB color model
int values=bufImg.getRGB(i,j);
// Convert the single integer pixel value to RGB color
Color oldColor = new Color(values);
int red = oldColor.getRed(); // get red value
int green = oldColor.getGreen(); // get green value
int blue = oldColor.getBlue(); // get blue value
// Convert RGB to gray scale using formula
// gray = 0.299 * R + 0.587 * G + 0.114 * B
double grayVal = 0.299*red + 0.587*green + 0.114*blue;
// Assign each channel of RGB with the same value
Color newColor = new Color((int)grayVal, (int)grayVal, (int)grayVal);
// Get back the integer representation of RGB color
// and assign it back to the original position
bufImg.setRGB(i, j, newColor.getRGB());
}
}
// return back the resulting image in BufferedImage type
return bufImg;
}
public static BufferedImage histEqualization(Image img)
{
//Convert image to BufferedImage
img = ImageProcessor.toGrayScale(img);
BufferedImage bufImg = convert(img);
//Getting information of each pixel;
int[][] intensity = new int[bufImg.getWidth()][ bufImg.getHeight()];
int[] counter = new int[256];
for(int j=0; j < bufImg.getHeight();j++)
for(int i=0; i < bufImg.getWidth();i++)
{
int values=bufImg.getRGB(i,j);
Color oldColor = new Color(values);
intensity[i][j] = oldColor.getBlue();
counter[intensity[i][j]]++;
}
//BEGIN OF Histogram Equalization
//find out how many rows the table have
int row=0;
for(int i=0;i<256;i++)
if(counter[i]!=0)
row++;
//Find out the v column of the table
//table[row][0] = v column
//table[row][1] = c column
int temp=0;
int[][] table = new int[row][2];
for(int i=0;i<256;i++)
if(counter[i]!=0)
{
table[temp][0] = i;
temp++;
}
//Find out the c column of the table
for(int i=0;i<row;i++)
table[i][1] = counter[table[i][0]];
//C-> CS
int sum = 0;
for(int i=0;i<row;i++)
{
sum += table[i][1];
table[i][1] = sum;
}
//CS->NCS
int min = table[0][1], max = table[row-1][1];
for(int i=0;i<row;i++)
table[i][1] = Math.round((table[i][1]-min)/(max-min));
//Mapping
for(int j=0;j<bufImg.getHeight();j++)
for(int i=0;i<bufImg.getWidth();i++)
{
for(int k=0;k<row;k++)
if(intensity[i][j]==table[k][0])
intensity[i][j] = table[k][1];
Color newColor = new Color(intensity[i][j], intensity[i][j], intensity[i][j]);
bufImg.setRGB(i, j, newColor.getRGB());
}
return bufImg;
}
}
最佳答案
convert 方法可以简化为:
return new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY) ;
因为您只想要一个灰度(单 channel )图像。那么toGrayScale的方法是:
BufferedImage bufImg = convert(img);
byte[] bufferbyte = ((DataBufferByte) bufImg.getRaster().getDataBuffer()).getData() ;
for(int j=0, pos=0; j < bufImg.getHeight(); j++)
for(int i=0; i < bufImg.getWidth(); i++, pos++)
{
int red = bufImg().getSample(i, j, 0) ;
int green = bufImg().getSample(i, j, 1) ;
int blue = bufImg().getSample(i, j, 2) ;
// gray = 0.299 * R + 0.587 * G + 0.114 * B
bufferbyte[pos] = (byte)(int)(0.299*red + 0.587*green + 0.114*blue) ;
}
return bufImg ;
我使用 Raster() 来获取 RBG 值,但直接访问 DataBuffer 会快得多(就像缓冲区字节一样)。
然后就可以做直方图均衡了,但是第一行是错误的,应该删除。
关于java - java中的直方图均衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13997996/
如何检查枚举是否等于可能情况之一。有没有比以下更好的解决方案: [.orderedAscending, .orderedSame].contains(Calendar.current.compare(
我正在尝试使用 openCV 和 C++ 来均衡 HSV 图像的直方图。我知道有些带有 openCV 的库可以为我执行此操作,但我想手动尝试以了解该方法。 我假设均衡将在 HSV 图像的 V chan
我有一个包含多个列的表格。这些列可以包含不同大小的内容,因此高度会有所不同。 我想做的是调整单个元素的高度,使它看起来像这样: . .table { display: flex; ba
:) 我有一个小问题: 我有一个 div,在里面有两个 div,一个挨着另一个。左边的一个在调整大小时改变其高度以保持其与宽度的关系,我希望右边的一个与左边的高度相同;我在文档末尾的 jQuery 中
我正在尝试在 YAML 中创建三列布局。我正在使用此框架的网格功能来定位列(导航、内容、侧边栏)。 我的问题:如何将所有三个 div 的高度设置为相同的大小。 YAML 应该提供 ym-equaliz
这个问题在这里已经有了答案: How do I keep two side-by-side div elements the same height? (24 个答案) 关闭 5 年前。 有没有一种
好像我的 JavaScript 没有听我的。我只是想将所有 anchor 的高度设置为等于这些 anchor 的最高高度。 HTML {{products.title}}
我想对同一主题的两个半脸彩色图像进行均衡,然后将它们合并。它们每个都有不同的色调饱和度和亮度值....使用 opencv 我如何标准化/均衡每个半图像? 我尝试执行 cvEqualizeHist(v,
当一个div高度很大时,我想等于两个div的高度 例子: B ACD Div 2 的高度大于 div 1 最佳答案 我可能有一个可能的解决方案: http://jsfiddle.net/adaz/w
我正在尝试做类似的事情(我已经删除了不必要的东西) Models.Parent.findAll({ where: { parent_id: {
我在 node 中使用 typescript,它在开发模式下运行良好,通过使用以下命令运行我的 index.ts 和 ts-node。 ts-node src/index.ts 但是当我将 types
我是一名优秀的程序员,十分优秀!