R语言实现图像查重

版权声明:商业目的引用该文章请联系我,个人博客引用该文章请注明来源,谢谢 https://blog.csdn.net/slvayf/article/details/82797653

(商业目的引用该文章请联系我,个人博客引用该文章请注明来源,谢谢)

通过三个脚本实现图像查重(需要提前导入R的jpeg库)

R脚本路径:

D:\Computer Science\Programming\R\ImageProcessing

图像存储路径(图像名称任意):

D:\Computer Science\Programming\R\ImageProcessing\data

程序不难,没写注释,简单说下思路:

        确定一个平面,平面只有两个参数:width和height,并且不大于所有图像中最小边界(可以理解为,平面也是一张图像,它的width是所有图像中最小的,height也是最小的,所以就可以确定地说:以所有图像的左顶点为锚点,该平面一定可以完整覆盖到所有图像上,覆盖的地方称为投影),这种方案下,所有图像中不宜有尺寸过小的。然后根据图像数量在平面投影上随机确定一部分点,总的趋势是图像越多,投影上的点就越密集,并且不同图像上所有点的位置都是相同的,之后对所有图像中的点进行RGB计算。计算流程:对一张图像上的点,单个进行RGB计算(点的RGB计算看程序吧,很简单),然后对所有点进行归并,最终比对不同图像上归并出来的那个值就可以判断图像是否相同,可以在data下加入几张尺寸不同的纯白和纯黑图像,观察输出的结果。关于程序的改进:可以在最终RGB归并值的比对结果出来后,再加入对图像尺寸是否相同的判断。

1_ImagePoint.r(该脚本实现在所有图像中找点的动作)

setwd("D:/Computer Science/Programming/R/ImageProcessing/data")
print("working directory has been switched to: D:/Computer Science/Programming/R/ImageProcessing/data")

v <- list.files(getwd())
print("____________")
print("pic name:")
print(v)
print("____________")

num = length(v)
print("pic count:")
print(num)
print("____________")

library(jpeg)

orgpic=readJPEG(v[1])
v4res <- dim(orgpic)
a = v4res[1]
b = v4res[2]

for(i in 1:num) {
orgpic=readJPEG(v[i])
v4res <- dim(orgpic)
	if(a>v4res[1]){
	a=v4res[1]
	}
	if(b>v4res[2]){
	b=v4res[2]
	}
}

res4a <- sample(1:a, size = num)
res4b <- sample(1:b, size = num)

rownames = c("point")
colnames = c("X", "Y")

for(i in 1:num) {
M <- matrix(c(res4a[i],res4b[i]), ncol = 2,byrow = TRUE, dimnames = list(rownames, colnames))
print(M)
}
print("____________")

paintfun <- function(aa,bb){
plot(x = res4a,y = res4b,
   xlab = "Image_X",
   ylab = "Image_Y",
   xlim = c(1,aa),
   ylim = c(1,bb),		 
   main = "Discrete Point"
)
}

2_ImageCalculation.r(该脚本实现点的RGB计算动作)

rgbsum <- c(0,0)
resget <- c(0,0)

for(i in 1:num) {
print(i)
a = 0
orgpic=readJPEG(v[i])
		for(j in 1:num) {
		v4rgb <- orgpic[res4a[j],res4b[j],]
		print(v4rgb)
		rgbsum[j] = (v4rgb[1]+v4rgb[2]+v4rgb[3])/3
		print (rgbsum[j])
		print("__")
		a = a+rgbsum[j]
		}
print(a)
resget[i]=a
print("____________")
}

picfun <- function(x){
  for(i in 1:x) {
	print(resget[i])
  }
}

picfun(num)
print("____________")

3_ImageMerging.r(该脚本实现点的归并及相同图像的文件名输出)

扫描二维码关注公众号,回复: 3720150 查看本文章
c <- table(unlist(resget))
print(c)

c<-1:num

mergfun <- function(){
	for(i in c){
		if(!i%in%c)
		{
			next
		}
		#当前点的RGB的平均数,在RGB向量里面相同元素的下标
		cnew <- which(resget==resget[i])
		c<-c[!c%in%cnew]
		
		print(v[cnew])
	}
}
mergfun()

猜你喜欢

转载自blog.csdn.net/slvayf/article/details/82797653