本文目錄一覽:
- 1、Java處理bmp圖像,怎樣操作BMP位圖的數據
- 2、java可以做圖像處理和機器視覺嗎
- 3、《JAVA數字圖像處理》pdf下載在線閱讀全文,求百度網盤雲資源
- 4、新手學習使用Java,嘗試着做一個項目使用Java做一個視頻圖像的處理。
- 5、關於JAVA的圖片處理問題
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數字圖像處理》pdf下載在線閱讀全文,求百度網盤雲資源
《JAVA數字圖像處理》百度網盤pdf最新全集下載:
鏈接:
?pwd=f8sq 提取碼: f8sq
簡介:在開始本書內容之前,筆者假設你已經有了面向對象語言編程的基本概念,了解Java語言的基本語法與特徵,原因在於本書的所有源代碼都是基於Java語言實現的,而且是基於Java開發環境運行與演示所有圖像處理算法的。本書第1章到第3章是為了幫助讀者了解與掌握Java圖形與GUI編程的基本知識與概念而寫的。本章主要介紹Java GUI編程中基本的圖形知識,針對GU1編程,Java語言提供了兩套幾乎並行的API,分別是Swing與AWT。早期的Java GUJ編程中主要使用AWT的相關組件,但是AWT的功能並不是十分強大,而且嚴重依賴本地接口。於是在Java 1.3及後續版本中引入了Swing工具實現GUl編程,Swing中的組件大多數都是基於純Java語言實現的,而不是通過本地組件實現的,所以它們是輕量級的GUI組件,同時Swing對圖形與圖像的支持操作也有很大的提高與增強。如何區分AWT組件與Swing組件?一個簡單而且相當直觀的方法是看Class的名稱,Swing的組件大多數帶有大寫的前綴字母J。
新手學習使用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的圖片處理問題
public static boolean write(RenderedImage im, String formatName, File output) throws IOException
使用支持給定格式的任意 ImageWriter 將一個圖像寫入 File。如果已經有一個 File 存在,則丟棄其內容。
參數:im – 要寫入的 RenderedImage。
formatName – 包含格式非正式名稱的 String。
output – 將在其中寫入數據的 File。
返回:如果沒有找到合適的 writer,則返回 false。
拋出: IllegalArgumentException – 如果任何參數為 null。
IOException – 如果在寫入過程中發生錯誤。
說白了,就是按指定的formatName把圖片存到file(或OutputStream)中。formatName是已註冊的、可以保存圖片的writer的非正式名稱,比如“jpeg”,“tiff”。如果想知道到底有哪些writer在你的機器上被註冊了,用ImageIO.getWriterFormatNames(),返回類型是String[] 。同樣的,還有讀取圖片的reader,對應的是ImageIO.getReaderFormatNames()。
最後要說的是,這個方法是保存圖片,和上傳沒有關係。你可能是要上傳圖片後再保存吧!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/182468.html