Java實現提取圖片邊緣的示例代碼
更新時間:2023年06月30日 16:13:52 作者:萬能的小裴同學
這篇文章主要為大家詳細介紹了如何利用Java實現提取圖片邊緣的功能,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下
Java提取圖片邊緣
實現代碼
package qrcodeServer;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ServerMain {
static final String rootpath="E:\\desktop\\";
static final int Y=30;
static boolean isboard(BufferedImage ash,int now_x,int now_y,int w,int h,int[][] kernel1,int[][] kernel2)
{
int tmp=kernel1.length/2;
int x=now_x-tmp,y=now_y-tmp;
int ken1=0,ken2=0;
for(int i=x;i<x+kernel1.length;i++)
{
for(int j=y;j<y+kernel1.length;j++)
{
if(i<0||j<0||i>=w||j>=h)
{
return true;
}
ken1+=RGBtoAsh(ash.getRGB(i, j))*kernel1[i-x][j-y];
ken2+=RGBtoAsh(ash.getRGB(i, j))*kernel2[i-x][j-y];
}
}
ken1=ken1<0?-ken1:ken1;
ken2=ken2<0?-ken2:ken2;
return (ken1+ken2)/2>Y;
}
public static int RGBtoAsh(int rgb)
{
int tmp=0;
for(int i=0;i<3;i++)
{
tmp+=(rgb>>(i*8))&0xFF;
}
return tmp/3;
}
public static int AshtoRGB(int ash)
{
int tmp=0xFF000000;
for(int i=0;i<3;i++)
{
tmp|=(ash<<(i*8));
}
return tmp;
}
public static void main(String[] args) {
try {
BufferedImage bimg=ImageIO.read(new File(rootpath+"1.jpg"));
int w=bimg.getWidth(),h=bimg.getHeight();
BufferedImage bimgot=new BufferedImage(w, h, Image.SCALE_DEFAULT);
BufferedImage tmp=new BufferedImage(w, h, Image.SCALE_DEFAULT);
int kernel1[][]= {{1,1,1,1},
{0,0,0,0},
{0,0,0,0},
{-1,-1,-1,-1}};
int kernel2[][]= {{-1,0,0,1},
{-1,0,0,1},
{-1,0,0,1},
{-1,0,0,1}
};
for(int i=0;i<bimg.getWidth();i++)
for(int j=0;j<bimg.getHeight();j++)
{
if(isboard(bimg, i, j, w, h, kernel1,kernel2))
bimgot.setRGB(i,j,0);
else
bimgot.setRGB(i,j,0xFFFFFFFF);
}
ImageIO.write(bimgot, "jpg",new File(rootpath+"out.jpg"));
} catch (FileNotFoundException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
}下面是效果演示

到此這篇關于Java實現提取圖片邊緣的示例代碼的文章就介紹到這了,更多相關Java提取圖片邊緣內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
深入淺析drools中Fact的equality?modes
這篇文章主要介紹了drools中Fact的equality?modes的相關知識,本文通過圖文實例代碼相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

