今天给各位分享java图像校正的知识,其中也会对图像水平校正进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、怎么用java代码放大或缩小图片不失真。2、java图像处理 – 图片上的数字字母圆滑处理方法3、如何使用java打开图像,然后以对图像的像素矩阵进行处理,然后以相同的质量因子保存为新图像4、新手学习使用Java,尝试着做一个项目使用Java做一个视频图像的处理。5、Java 图像识别 数字图像处理 从一张JPG图片中识别出若干黑色小方块6、Java处理bmp图像,怎样操作BMP位图的数据
怎么用java代码放大或缩小图片不失真。
放大图像不会导致失真,而缩小图像将不可避免的失真。
Java中也同样是这样。
但java提供了4个缩放的微调选项。
image.SCALE_SMOOTH
//平滑优先
image.SCALE_FAST//速度优先
image.SCALE_AREA_AVERAGING
//区域均值
image.SCALE_REPLICATE
//像素复制型缩放
image.SCALE_DEFAULT
//默认缩放模式
调用方法
Image
new_img=old_img.getScaledInstance(1024,
768,
Image.SCALE_SMOOTH);
得到一张缩放后的新图。
java图像处理 – 图片上的数字字母圆滑处理方法
抗锯齿的代码我倒是有一个,你试一下,输出图片第一行是不抗锯齿的,第二行是抗锯齿的。
public static void main(String[] args) throws IOException {
BufferedImage image = new BufferedImage(400, 200, BufferedImage.TYPE_4BYTE_ABGR_PRE);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, 400, 200);
g2d.setColor(Color.BLACK);
g2d.setFont(new Font(“Arial”, Font.PLAIN, 37));
g2d.drawString(“jjyygg789@163.com”, 10f, 40f);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);// 设置抗锯齿效果
g2d.drawString(“jjyygg789@163.com”, 10f, 80f);
File outputfile = new File(“D:\\TestGraphics.png”);
ImageIO.write(image, “PNG”, outputfile);
}
如何使用java打开图像,然后以对图像的像素矩阵进行处理,然后以相同的质量因子保存为新图像
/** A demonstration of various image processing filters */
public class ImageOps extends JPanel {
static final int WIDTH = 600, HEIGHT = 675; // Size of our example
public String getName() {
return “Image Processing”;
}
public int getWidth() {
return WIDTH;
}
public int getHeight() {
return HEIGHT;
}
Image image;
/** This constructor loads the image we will manipulate */
public ImageOps() {
image = new javax.swing.ImageIcon(“C:\\Sunset.jpg”).getImage();
}
// These arrays of bytes are used by the LookupImageOp image filters below
static byte[] brightenTable = new byte[256];
static byte[] thresholdTable = new byte[256];
static { // Initialize the arrays
for (int i = 0; i 256; i++) {
brightenTable[i] = (byte) (Math.sqrt(i / 255.0) * 255);
thresholdTable[i] = (byte) ((i 225) ? 0 : i);
}
}
// This AffineTransform is used by one of the image filters below
static AffineTransform mirrorTransform;
static { // Create and initialize the AffineTransform
mirrorTransform = AffineTransform.getTranslateInstance(127, 0);
mirrorTransform.scale(-1.0, 1.0); // flip horizontally
}
// These are the labels we’ll display for each of the filtered images
static String[] filterNames = new String[]{“Original”, “Gray Scale”,
“Negative”, “Brighten (linear)”, “Brighten (sqrt)”, “Threshold”,
“Blur”, “Sharpen”, “Edge Detect”, “Mirror”, “Rotate (center)”,
“Rotate (lower left)”};
// The following BufferedImageOp image filter objects perform
// different types of image processing operations.
static BufferedImageOp[] filters = new BufferedImageOp[]{
// 1) No filter here. We’ll display the original image
null,
// 2) Convert to Grayscale color space
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null),
// 3) Image negative. Multiply each color value by -1.0 and add 255
new RescaleOp(-1.0f, 255f, null),
// 4) Brighten using a linear formula that increases all color
// values
new RescaleOp(1.25f, 0, null),
// 5) Brighten using the lookup table defined above
new LookupOp(new ByteLookupTable(0, brightenTable), null),
// 6) Threshold using the lookup table defined above
new LookupOp(new ByteLookupTable(0, thresholdTable), null),
// 7) Blur by “convolving” the image with a matrix
new ConvolveOp(new Kernel(3, 3, new float[]{.1111f, .1111f,
.1111f, .1111f, .1111f, .1111f, .1111f, .1111f, .1111f,})),
// 8) Sharpen by using a different matrix
new ConvolveOp(new Kernel(3, 3, new float[]{0.0f, -0.75f, 0.0f,
-0.75f, 4.0f, -0.75f, 0.0f, -0.75f, 0.0f})),
// 9) Edge detect using yet another matrix
new ConvolveOp(new Kernel(3, 3, new float[]{0.0f, -0.75f, 0.0f,
-0.75f, 3.0f, -0.75f, 0.0f, -0.75f, 0.0f})),
// 10) Compute a mirror image using the transform defined above
new AffineTransformOp(mirrorTransform,
AffineTransformOp.TYPE_BILINEAR),
// 11) Rotate the image 180 degrees about its center point
new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,
64, 95), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
// 12) Rotate the image 15 degrees about the bottom left
new AffineTransformOp(AffineTransform.getRotateInstance(
Math.PI / 12, 0, 190),
AffineTransformOp.TYPE_NEAREST_NEIGHBOR),};
/** Draw the example */
public void paint(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
// Create a BufferedImage big enough to hold the Image loaded
// in the constructor. Then copy that image into the new
// BufferedImage object so that we can process it.
BufferedImage bimage = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_RGB);
Graphics2D ig = bimage.createGraphics();
ig.drawImage(image, 0, 0, this); // copy the image
// Set some default graphics attributes
g.setFont(new Font(“SansSerif”, Font.BOLD, 12)); // 12pt bold text
g.setColor(Color.green); // Draw in green
g.translate(10, 10); // Set some margins
// Loop through the filters
for (int i = 0; i filters.length; i++) {
// If the filter is null, draw the original image, otherwise,
// draw the image as processed by the filter
if (filters[i] == null) {
g.drawImage(bimage, 0, 0, this);
} else {
g.drawImage(filters[i].filter(bimage, null), 0, 0, this);
}
g.drawString(filterNames[i], 0, 205); // Label the image
g.translate(137, 0); // Move over
if (i % 4 == 3) {
g.translate(-137 * 4, 215); // Move down after 4
}
}
}
public static void main(String[] a) {
JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setContentPane(new ImageOps());
f.pack();
f.setVisible(true);
}
}
更多例子请到java2s.com
新手学习使用Java,尝试着做一个项目使用Java做一个视频图像的处理。
Java图像处理技巧四则
下面代码中用到的sourceImage是一个已经存在的Image对象
图像剪切
对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:
//import java.awt.*;
//import java.awt.image.*;
Image croppedImage;
ImageFilter cropFilter;
CropFilter =new CropImageFilter(25,30,75,75); //四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API
CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));
如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一个ImageProducer对象。
图像缩放
对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:
Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一个100X100的图像
Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。
//其它情况请参考API
灰度变换
下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个颜色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知道具体算法是什么样的,效果和上述不同。
/* GrayFilter.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class GrayFilter extends RGBImageFilter {
int modelStyle;
public GrayFilter() {
modelStyle=GrayModel.CS_MAX;
canFilterIndexColorModel=true;
}
public GrayFilter(int style) {
modelStyle=style;
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
if (modelStyle==GrayModel
else if (modelStyle==GrayModel
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}
/* GrayModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class GrayModel extends ColorModel {
public static final int CS_MAX=0;
public static final int CS_FLOAT=1;
ColorModel sourceModel;
int modelStyle;
public GrayModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=0;
}
public GrayModel(ColorModel sourceModel,int style) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=style;
}
public void setGrayStyle(int style) {
modelStyle=style;
}
protected int getGrayLevel(int pixel) {
if (modelStyle==CS_MAX) {
return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));
}
else if (modelStyle==CS_FLOAT){
return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);
}
else {
return 0;
}
}
public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}
public int getRed(int pixel) {
return getGrayLevel(pixel);
}
public int getGreen(int pixel) {
return getGrayLevel(pixel);
}
public int getBlue(int pixel) {
return getGrayLevel(pixel);
}
public int getRGB(int pixel) {
int gray=getGrayLevel(pixel);
return (getAlpha(pixel)24)+(gray16)+(gray8)+gray;
}
}
如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()。
色彩变换
根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class ReverseColorModel extends ColorModel {
ColorModel sourceModel;
public ReverseColorModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
}
public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}
public int getRed(int pixel) {
return ~sourceModel.getRed(pixel);
}
public int getGreen(int pixel) {
return ~sourceModel.getGreen(pixel);
}
public int getBlue(int pixel) {
return ~sourceModel.getBlue(pixel);
}
public int getRGB(int pixel) {
return (getAlpha(pixel)24)+(getRed(pixel)16)+(getGreen(pixel)8)+getBlue(pixel);
}
}
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class ReverseFilter extends RGBImageFilter {
public ReverseFilter() {
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
substituteColorModel(cm,new ReverseColorModel(cm));
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}
要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRed、getGreen、getBlue。
下面是上面的效果的一个总的演示程序。
/*GrayImage.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.color.*;
public class GrayImage extends JFrame{
Image source,gray,gray3,clip,bigimg;
BufferedImage bimg,gray2;
GrayFilter filter,filter2;
ImageIcon ii;
ImageFilter cropFilter;
int iw,ih;
public GrayImage() {
ii=new ImageIcon(\”images/11.gif\”);
source=ii.getImage();
iw=source.getWidth(this);
ih=source.getHeight(this);
filter=new GrayFilter();
filter2=new GrayFilter(GrayModel.CS_FLOAT);
gray=createImage(new FilteredImageSource(source.getSource(),filter));
gray3=createImage(new FilteredImageSource(source.getSource(),filter2));
cropFilter=new CropImageFilter(5,5,iw-5,ih-5);
clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));
bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);
MediaTracker mt=new MediaTracker(this);
mt.addImage(gray,0);
try {
mt.waitForAll();
} catch (Exception e) {
}
Java 图像识别 数字图像处理 从一张JPG图片中识别出若干黑色小方块
你需要关注的主要是这个类:java.awt.image.BufferedImage
可以查阅相关的API。
java图像处理技术在《java核心技术8 下卷》中有比较详细的介绍。
相关技术要求和注意事项:RGB标准、ICC配置特性、
建议如果进行像素识别的话可以选取关键点的识别方式、而且确认像素是否符合要求使用RGB的范围识别而非精确识别。
至于具体的识别操作过程,需要你详细定义开始识别的位置标准(规定的或者识别图像获取)、边界标准、大小(识别块得SIZE)、分组(给识别块确定属性)等
Java处理bmp图像,怎样操作BMP位图的数据
bmp图像文件数据分为三个部分:
1、前14个字节为文件信息头,在这部分信息中包含了位图信息标志、该bmp图像的大小和图像实际数据的相对偏移量这三部分有用的信息。
位图标志一定为“0x4D42”,否则,该文件不是bmp图像。
在VC++中,这14个字节对应一个数据类型,类型名为“BITMAPFILEHEADER”,它的定义为:
typedef struct tagBITMAPFILEHEADER {
WORD bfType; //位图信息标志
DWORD bfSize; //图像的大小
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits; //图像实际数据的相对偏移量
} BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;
可以设一个该类型的变量:BITMAPFILEHEADER bmfh,将bmp图像文件的前14字节数据读入这个变量中,然后通过判断bmfh.bfType == 0x4D42,确定是不是为bmp图像。
2、接下来40个字节为位图信息头,其中存储了该bmp图像的有关信息。这些信息包括:图像宽度(像素)、图像高度(像素)、图像长度(字节,仅仅是图像实际数据的长度,不包括各个信息头)、水平分辨率、垂直分辨率、每个像素的存储位数等信息。
其中,通过“每个像素的存储位数”这个信息可以知道图像的颜色:
如果“每个像素的存储位数”的值只有四种:为1,说明图像只有两种颜色(黑、白);为4,说明图像有16种颜色;为8,说明图像有256种颜色;为24,说明该图像为真彩色图像,颜色数为2^24。这四种取值对应四种bmp图像,也就是说,bmp图像只有这四种。
在这四种bmp图像种,前三种都需要在图像文件中包含调色板数据,分别存储三种图像的2、16、256种颜色。而最后一种bmp格式的图像不需要调色板,因为这种图像的“每个像素的存储位数”值为24,也就是说,存储一个像素值需要24位,正好可以存储一个像素的颜色(红、绿、蓝各8位)。
在VC++中,这40个字节的位图信息头也有一个数据类型,类型名为“BITMAPINFOHEADER”,它的定义为:
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth; //图像宽度(像素)
LONG biHeight; //图像高度(像素)
WORD biPlanes;
WORD biBitCount; //每个像素的存储位数
DWORD biCompression;
DWORD biSizeImage; //图像长度(字节,仅仅是图像实际数据的长度,不包括各个信息头)
LONG biXPelsPerMeter; //水平分辨率
LONG biYPelsPerMeter; //垂直分辨率
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
3、接下来若干个字节为调色板,只有前三种bmp图像有,第四种真彩色bmp图像没有这部分数据。
调色板是一个数组,每个数组元素有四字节,只有三个字节有用,另外一个没有。有用的三个字节存储一种颜色(红绿蓝各占一字节),这四个字节在VC++中定义为:
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
定义一个这种类型的数组即为调色板。数组的长度可由BITMAPINFOHEADER中的biBitCount推算出来。
4、上述三部分信息之后,即是实际的像素数据。一个像素的存储位数为1、4、8或16,正如前面所述。
如果是1位,对应的bmp图像应该有一个长度为2的调色板。这一位的值只能是0或1,用来指明该像素的颜色在调色板中的地址。
如果是4位,对应的bmp图像应该有一个长度为16的调色板。这4位的值有16种,同样指示该像素的颜色在调色板中的地址。
如果是8位,对应的bmp图像应该有一个长度为256的调色板。这8位的值有256种,同样指示该像素的颜色在调色板中的地址。
如果是24位,对应的bmp图像没有调色板,该像素的颜色由这24位数据直接表示。
bmp图像的数据就这几个部分。
任何一个bmp图像的像素都是由红绿蓝三种颜色组成(带调色板也好,不带调色板也好)。如果一个像素的红绿蓝三种色的值相等,那么该像素就是灰色的。灰度图是这样一种有严格规定的bmp图像:它是上述四种bmp图像的第三种,并且它的调色板的每个数组元素的红绿蓝三值都相同,所以灰度图的灰度种数是256。
若要保存图像,需要按顺序保存文件信息头、位图信息头、调色板(如果有)和图像的实际数据。程序可以这样写:
bool Write(CString FileName)
{
CFile file;
BITMAPFILEHEADER bmfh;
if(! (bmi pBits))
{
AfxMessageBox(“Data is not valid!”);
return FALSE;
}
//创建文件
if(!file.Open(FileName,CFile::modeCreate | CFile::modeWrite))
{
AfxMessageBox(“File creating fails!”);
return FALSE;
}
//填写文件信息头
bmfh.bfType = 0x4d42;
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
int nInfoSize = sizeof(BITMAPINFOHEADER) + GetPaletteSize() * sizeof(RGBQUAD);
bmfh.bfOffBits = sizeof(bmfh) + nInfoSize;
bmfh.bfSize = bmfh.bfOffBits + bmi-bmiHeader.biSizeImage;
//写文件
file.Write( (LPVOID)bmfh, sizeof(bmfh));
file.Write( (LPVOID)bmi, nInfoSize);
file.Write( (LPVOID)pBits, bmi-bmiHeader.biSizeImage);
return TRUE;
}
java图像校正的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于图像水平校正、java图像校正的信息别忘了在本站进行查找喔。