算法描述
成都创新互联是一家专注于网站设计、成都做网站与策划设计,北安网站建设哪家好?成都创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:北安等地区。北安做网站价格咨询:18982081108
procedure ImageMatching
{
输入FirstImage;
输入SecondImage;
//获得两幅图象的大小
Height1=GetImageHeight(FirstImage);
Height2=GetImageHeight(SecondImage);
Width1=GetImageWidth(FirstImage);
Width2=GetImageWidth(SecondImage);
// 从第二幅图象取网格匹配模板
SecondImageGrid = GetSecondImageGrid(SecondImage);
// 粗略匹配,网格在第一幅图象中先从左向右移动,再从下到上移动,每次移动一个网格间距,Step_Width 或Step_Height,当网格移出重叠区域后结束
y=Heitht1-GridHeight;
MinValue = MaxInteger;
While ( yHeight1-OverlapNumber)//当网格移出重叠部分后结束
{
x=Grid_Width/2; //当网格位于第一幅图象的最左边时,A点的横坐标。
While ( x(Width1-Grid_Width/2) )
{
FirstImageGrid=GetImgaeGrid(FirstImgaeGrid, x, y);
differ=CaculateDiff(FirstImgaeGrid, SecondImageGrid);//计算象素值差的平
//方和
if (differMinValue)
{
BestMatch_x=x;
BestMatch_y=y;
MinValue = differ;
}
x= x+Step_width;
}
y=y-Step_Height;
}
//精确匹配
Step_Width= Step_Width/2;
Step_Height= Step_Height/2;
While ( Step_Height0 Step_Width0)//当水平步长和垂直步长均减为零时结束
{
if(Step_Height==0)//当仅有垂直步长减为零时,将其置为1
Step_Height=1;
If(Step_Width==0) //当仅有水平步长减为零时,将其置为1
Step_Width=1;
temp_x = BestMatch_x;
temp_y = BestMatch_y;
for ( i= -1; i1; i++)
for( j= -1; j1; j++)
{
if ((i=0j!=0)|(i!=0j=0))
{
FirstImageGrid=GetImgaeGrid(FirstImgaeGrid,
temp_x+i*Step_Width, temp_y +j*Step_Height);
differ=CaculateDiff(FirstImgaeGrid, SecondImageGrid);
if (differMinValue)
{
BestMatch_x=x;
BestMatch_y=y;
MinValue = differ;
}
}
}
Step_Height = Step_Height /2;
Step_Width = Step_Width/2;
}
}
不懂的可以问我,相互交流
#首先在D盘写一个文件"temp.html",如下内容
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
title图片转文本/title
meta http-equiv="content-type" content="text/html; charset=gbk"
style type="text/css"
body {
font-family: 宋体; line-height: 0.8em; letter-spacing: 0px; font-size: 8px;
}
/style
/head
body
${content}
/body
/html
#在D盘放一个图片(放小一点的)"a.jpg"
#运行如下JAVA代码:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
public class Test {
/** 此处设置灰度字符,此处只用十个字符,可以设置更多 */
private static char[] cs = new char[] { '.', ',', '*', '+', '=', '', '$', '@', '#', ' ' };
public static void main(String[] args) throws IOException {
// 读取图片
BufferedImage bfedimage = ImageIO.read(new File("D:\\a.jpg"));
// 图片转字符串后的数组
char[][] css = new char[bfedimage.getWidth()][bfedimage.getHeight()];
for (int x = 0; x bfedimage.getWidth(); x++) {
for (int y = 0; y bfedimage.getHeight(); y++) {
int rgb = bfedimage.getRGB(x, y);
Color c = new Color(rgb);
// 得到灰度值
int cc = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
css[x][y] = cs[(int) ((cc * 10 - 1) / 255)];
}
}
// 取得模板HTML
String temp = readFile(new File("D:\\temp.html"),"gbk");
StringBuffer sb = new StringBuffer();
// 开始拼接内容
for (int y = 0; y css[0].length; y++) {
for (int x = 0; x css.length; x++) {
sb.append(css[x][y]);
}
sb.append("\r\n");
}
System.out.println(sb.toString());
// 生成文件
String content = toHTML(sb.toString());
String filecontent = replaceStrAllNotBack(temp, "${content}", content);
writeFile(new File("D:\\content.html"), filecontent, "gbk");
}
public static String toHTML(String s) {
s = s.replaceAll("", "");
s = s.replaceAll(" ", " ");
s = s.replaceAll("", "");
s = s.replaceAll("", "");
s = s.replaceAll("\"", """);
s = s.replaceAll("\\\r\\\n", "br/");
s = s.replaceAll("\\\r", "br/");
s = s.replaceAll("\\\n", "br/");
return s;
}
public static String replaceStrAllNotBack(String str, String strSrc, String strDes) {
StringBuffer sb = new StringBuffer(str);
int index = 0;
while ((index = sb.indexOf(strSrc, index)) != -1) {
sb.replace(index, index + strSrc.length(), strDes);
index += strDes.length();
}
return sb.toString();
}
/**
* 读文件(使用默认编码)
*
* @param file
* @return 文件内容
* @throws IOException
*/
public static String readFile(File file, String charset) throws IOException {
InputStreamReader fr = new InputStreamReader(new FileInputStream(file), charset);
StringBuffer sb = new StringBuffer();
char[] bs = new char[1024];
int i = 0;
while ((i = fr.read(bs)) != -1) {
sb.append(bs, 0, i);
}
fr.close();
return sb.toString();
}
/**
* 写文件
*
* @param file
* @param string
* 字符串
* @param encoding
* 编码
* @return 文件大小
* @throws IOException
*/
public static int writeFile(File file, String string, String encoding) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
byte[] bs = string.getBytes(encoding);
fos.write(bs);
return bs.length;
} finally {
fos.close();
}
}
}
#打开"D:\content.html"文件看效果吧。
有什么问题可以联系我。
String类的方法:
①利用运算符"+"
②public String concat(String str)进行字符串的拼接操作
StringBuffer的方法:
①public StringBuffer append(String str)将str添加到当前字符串缓冲区的字符序列的末尾
②public StringBuffer insert(int offset,String str)在当前字符串缓冲区的字符序列的下标
索引offset插入str。如果offset等于旧长度,则str添加在字符串缓冲区的尾部
如图所示