求一个JAVA方法 要求计算RGB32位模式的颜色转换为灰度 小弟对色彩和图形不是很懂。谢谢

求一个JAVA方法 要求计算RGB32位模式的颜色转换为灰度 小弟对色彩和图形不是很懂。谢谢,第1张

public static final byte[] getPixels(Image img)
{
if (img == null)
return null ;
int width = imggetWidth(null) ;
int height = imggetHeight(null) ;
BufferedImage bi = new BufferedImage(width,height,BufferedImageTYPE_BYTE_GRAY) ;
bigetGraphics()drawImage(img,0,0,null) ;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
ImageIOwrite(bi, "png", bos);
} catch (IOException ex)
{
}
return bostoByteArray() ;
}

下面这个是自己写的,先获取阈值再灰度化,对应标准OSTU的流程
filename=' 填入你的名'
I=imread(filename);
I_gray=rgb2gray(I);
I_double=double(I_gray);%转化为双精度
[wid,len]=size(I_gray);
colorlevel=256; %灰度级
hist=zeros(colorlevel,1);%直方图
%计算直方图
for i=1:height
for j=1:width
m=I_gray(i,j)+1;
hist(m)=hist(m)+1;
end
end
hist=hist/(heightwidth);%直方图归一化
miuT=0;
for m=1:colorlevel
miuT=miuT+(m-1)hist(m);
end
xigmaB2=0;
for mindex=1:colorlevel
threshold=mindex-1;
omega1=0;
omega2=0;
for m=1:threshold-1
omega1=omega1+hist(m);
end
omega2=1-omega1;
miu1=0;
miu2=0;
for m=1:colorlevel
if m<threshold
miu1=miu1+(m-1)hist(m);
else
miu2=miu2+(m-1)hist(m);
end
end
miu1=miu1/omega1;
miu2=miu2/omega2;
xigmaB21=omega1(miu1-miuT)^2+omega2(miu2-miuT)^2;
xigma(mindex)=xigmaB21;
if xigmaB21>xigmaB2
finalT=threshold;
xigmaB2=xigmaB21;
end
end
threshT=finalT;
for i=1:height
for j=1:width
if I_double(i,j)>finalT
bin(i,j)=1;
else
bin(i,j)=0;
end
end
end
figure(1);imshow(uint8(bin));

把照片变成灰色方法

需要工具:ps

1ps打开文件,点击图层面板下面的新建按钮新建图层。

2将拾色器前景色调为灰色,点击编辑,选择填充。

3将填充上灰色的图层不透明度调为合适数值,就变灰了。

有可能是两个包有相同的方法,但功能和参数有所不同,要修改的是,你把所有import都删除掉,再重新导包,按Ctrl+Shift+O快捷键,所需要的包就导进去了,如果有两种选择的时候,如果还有重复,那么你可能有两个相同属性和方法的包在项目的Lib里面的,只是版本不同,那么你又把一个删除。再编译就OK了


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/13323987.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-31
下一篇2025-08-31

发表评论

登录后才能评论

评论列表(0条)

    保存