资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

灰度拉伸算法java代码,灰度值拉伸的计算公式

新手学习使用Java,尝试着做一个项目使用Java做一个视频图像的处理。

Java图像处理技巧四则

创新互联建站长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为和林格尔企业提供专业的成都做网站、成都网站建设,和林格尔网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。

下面代码中用到的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 根据图片的rgb求灰度值

public int filterRGB(int x, int y, int rgb) {

int a = rgb 0xff000000;//将最高位(24-31)的信息(alpha通道)存储到a变量

int r = (rgb 16) 0xff;//取出次高位(16-23)红色分量的信息

int g = (rgb 8) 0xff;//取出中位(8-15)绿色分量的信息

int b = rgb 0xff;//取出低位(0-7)蓝色分量的信息

rgb = (r * 77 + g * 151 + b * 28) 8; // NTSC luma,算出灰度值

return a | (rgb 16) | (rgb 8) | rgb;//将灰度值送入各个颜色分量

}

有大侠知道java或是js或是css中怎么将单通道灰度图片与三通道灰度图片吗?有没有什么相关的算法啊?

int ImageStretchByHistogram(IplImage *src1,IplImage *dst1)

/*************************************************

Function: 通过直方图变换进行图像增强,将图像灰度的域值拉伸到0-255

src1: 单通道灰度图像

dst1: 同样大小的单通道灰度图像

*************************************************/

{

assert(src1-width==dst1-width);

double p[256],p1[256],num[256];

memset(p,0,sizeof(p));

memset(p1,0,sizeof(p1));

memset(num,0,sizeof(num));

int height=src1-height;

int width=src1-width;

long wMulh = height * width;

//statistics

for(int x=0;xsrc1-width;x++)

{

for(int y=0;ysrc1- height;y++){

uchar v=((uchar*)(src1-imageData + src1-widthStep*y))[x];

num[v]++;

}

}

//calculate probability

for(int i=0;i256;i++)

{

p[i]=num[i]/wMulh;

}

//p1[i]=sum(p[j]); j=i;

for(int i=0;i256;i++)

{

for(int k=0;k=i;k++)

p1[i]+=p[k];

}

// histogram transformation

for(int x=0;xsrc1-width;x++)

{

for(int y=0;ysrc1- height;y++){

uchar v=((uchar*)(src1-imageData + src1-widthStep*y))[x];

((uchar*)(dst1-imageData + dst1-widthStep*y))[x]= p1[v]*255+0.5;

}

}

return 0;

}

void CCVMFCView::OnImageAdjustContrast()

{

if(workImg-nChannels1)

OnColorToGray();

Invalidate();

dst=cvCreateImage(cvGetSize(workImg),workImg-depth,workImg-nChannels);

ImageStretchByHistogram(workImg,dst);

m_dibFlag=imageReplace(dst,workImg);

Invalidate();

}

这个是C++代码格式的,你可以参考一下思路

如何用java编程实现提取一张图像每个像素点的灰度,并且按顺序输出为一串数字代码?

用个HashMap存储信息到文件,可以的话那么代码如下:

import java.util.*

import java.io.*

public class Test1

在下有个matlab中的灰度线性拉伸的程序,一直调不出结果,各位大侠帮忙解决下呀,非常感谢!!!

你读入的是灰度图像jpg,但你却对它进行了灰度转换A=rgb2gray(A);如果读入彩色图像你的程序就可以运行出来。

A=imread('C:\Users\Administrator\Desktop\11.bmp');% 线性灰度线性变换(拉伸)程序

a=20,b=250,c=50,d=200;

A=rgb2gray(A);

figure;imshow(A);

X=A;

[p,q]=size(X);  % 获取输入图像的行列数

Y=zeros(p,q);

for i=1:p

for j=1:q

  if X(i,j)a

      Y(i,j)=c;

  else

      if X(i,j)b

      Y(i,j)=((d-c)/(b-a))*(X(i,j)-a)+c;

  else

      Y(i,j)=d;

      end

  end

end

end

Y=uint8(Y);

figure;imshow(Y);


名称栏目:灰度拉伸算法java代码,灰度值拉伸的计算公式
URL链接:http://cdkjz.cn/article/hsopdd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220